package com.swiftcleaner.chovey.view.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.model.bean.SimilarBean;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：马海钊
 * 时间：2024/12/27 16:10
 * 功能：
 */
public class ItemSimilarAdapter extends RecyclerView.Adapter<ItemSimilarAdapter.ViewHolder> {
    private final List<SimilarBean> list;
    private final SimilarPhotosAdapter parentAdapter;

    public ItemSimilarAdapter(List<SimilarBean> list, SimilarPhotosAdapter parentAdapter) {
        this.parentAdapter = parentAdapter;
        this.list = new ArrayList<>(list); // 创建一个新的列表，避免外部修改
    }

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

    @Override
    public void onBindViewHolder(@NonNull ItemSimilarAdapter.ViewHolder holder, int position) {
        try {
            if (position >= 0 && position < list.size()) {
                SimilarBean similarBean = list.get(position);
                Glide.with(holder.imageView.getContext())
                        .load(similarBean.getUrl())
                        .error(R.mipmap.l_tu)
                        .placeholder(R.mipmap.l_tu)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(holder.imageView);
                holder.number.setText(formatFileSize(similarBean.getSize()));
                holder.checkBox.setChecked(similarBean.isSelect());
                holder.imageView.setOnClickListener(v -> {
                    similarBean.setSelect(!similarBean.isSelect());
                    holder.checkBox.setChecked(similarBean.isSelect());
                    parentAdapter.updateItem(similarBean, position); // 通知父适配器更新
                });
            } else {
                Log.e("ItemSimilarAdapter", "Position " + position + " is out of bounds. List size: " + list.size());
            }
        } catch (Exception e) {
            Log.e("ItemSimilarAdapter", "Exception in onBindViewHolder: " + e.getMessage(), e);
        }

    }

    @Override
    public int getItemCount() {
        int size = list == null ? 0 : list.size();
        return size;
    }

    private String formatFileSize(long size) {
        if (size <= 0) return "0 Bytes";
        final String[] units = new String[]{"Bytes", "KB", "MB", "GB", "TB"};
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView number;
        CheckBox checkBox;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.item_screen_shot_img);
            number = itemView.findViewById(R.id.item_screen_shot_number);
            checkBox = itemView.findViewById(R.id.item_screen_shot_checked);
        }
    }
}