package com.swiftcleaner.chovey.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.model.bean.SimilarBean;
import com.swiftcleaner.chovey.model.tool.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：马海钊
 * 时间：2024/12/25 14:00
 * 功能：
 */
public class SimilarPhotosAdapter extends RecyclerView.Adapter<SimilarPhotosAdapter.ViewHolder> {
    Context context;
    List<SimilarBean> list;

    public SimilarPhotosAdapter(Context context, List<SimilarBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setData(List<SimilarBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SimilarPhotosAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_similar_photo_recy_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SimilarPhotosAdapter.ViewHolder holder, int position) {
        SimilarBean similarBean = list.get(position);
        if (similarBean.getItems() != null) {
            holder.textView.setText("Group " + (position + 1));
            ItemSimilarAdapter adapter = new ItemSimilarAdapter(similarBean.getItems(), this);
            holder.recyclerView.setLayoutManager(new GridLayoutManager(holder.itemView.getContext(), 3));
            holder.recyclerView.setAdapter(adapter);
        } else {
            ToastUtil.showLongToast(context, "没有相似图片");
        }
    }

    public List<SimilarBean> getSelectedItems() {
        List<SimilarBean> selectedItems = new ArrayList<>();
        for (SimilarBean group : list) {
            for (SimilarBean item : group.getItems()) {
                if (item.isSelect()) {
                    selectedItems.add(item);
                }
            }
        }
        return selectedItems;
    }

    public void removeSelectedItems() {
        for (SimilarBean group : list) {
            group.getItems().removeIf(item -> item.isSelect());
        }
        list.removeIf(group -> group.getItems().isEmpty());
        notifyDataSetChanged();
    }
    public int calculateSelectedItemsSize() {
        int totalSize = 0;
        for (SimilarBean group : list) {
            for (SimilarBean item : group.getItems()) {
                if (item.isSelect()) {
                    totalSize += item.getSize(); // 假设 SimilarBean 有一个 getSize() 方法
                }
            }
        }
        return totalSize;
    }
    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        RecyclerView recyclerView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            recyclerView = itemView.findViewById(R.id.item_similar_photos_recy);
            textView = itemView.findViewById(R.id.item_similar_photos_text);
        }
    }
}