package com.swiftcleaner.chovey.view.adapter;

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.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.model.bean.CleanFileBean;

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

public class WhatsAppCleanAdapter extends RecyclerView.Adapter<WhatsAppCleanAdapter.AppInfoViewHolder> {
    private List<CleanFileBean> list;

    private OnItemClickListener listener;
    public WhatsAppCleanAdapter(List<CleanFileBean> list, OnItemClickListener listener) {
        this.list = list;
        this.listener = listener;
    }

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


    @Override
    public void onBindViewHolder(@NonNull AppInfoViewHolder holder, int position) {
        CleanFileBean fileInfo = list.get(position);
        if (fileInfo!=null&&fileInfo.getPath()!=null){
            Glide.with(holder.itemView.getContext())
                    .load(R.mipmap.l_tu)
                    .apply(new RequestOptions().transform(new RoundedCorners(10)))
                    .error(R.mipmap.ic_launcher)
                    .placeholder(R.mipmap.ic_launcher)
                    .into(holder.apk_icon);
            holder.apk_Name.setText(fileInfo.getName());
            holder.apk_number.setText(formatFileSize(fileInfo.getLength()));

            holder.apk_checked.setChecked(fileInfo.isSelected());

            holder.itemView.setOnClickListener(v -> {
                // 切换选中状态
                fileInfo.setSelected(!fileInfo.isSelected());
                // 通知数据变化，更新视图
                notifyItemChanged(position);
                // 如果设置了点击事件监听器，则回调
                if (listener != null) {
                    listener.onItemClick(fileInfo);
                }
            });
        }

    }

    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];
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
    public List<CleanFileBean> getSelectedFiles() {
        List<CleanFileBean> selectedFiles = new ArrayList<>();
        for (CleanFileBean fileBean : list) {
            if (fileBean.isSelected()) {
                selectedFiles.add(fileBean);
            }
        }
        return selectedFiles;
    }
    public void removeSelectedFiles(List<CleanFileBean> selectedFiles) {
        list.removeAll(selectedFiles);
        notifyDataSetChanged(); // 通知适配器数据已更改
    }
    public void setAllChecked(boolean isChecked) {
        for (CleanFileBean fileBean : list) {
            fileBean.setSelected(isChecked);
        }
        notifyDataSetChanged(); // 通知适配器数据已更改
    }

    public boolean isAllChecked() {
        for (CleanFileBean fileBean : list) {
            if (!fileBean.isSelected()) {
                return false;
            }
        }
        return true;
    }
    public static class AppInfoViewHolder extends RecyclerView.ViewHolder {
        ImageView apk_icon;
        TextView apk_Name, apk_number;
        CheckBox apk_checked;

        public AppInfoViewHolder(@NonNull View itemView) {
            super(itemView);
            apk_icon = itemView.findViewById(R.id.item_clean_list_file_img);
            apk_Name = itemView.findViewById(R.id.item_clean_list_file_name);
            apk_checked = itemView.findViewById(R.id.item_clean_list_file_checkout);
            apk_number = itemView.findViewById(R.id.item_clean_list_file_number);
        }
    }
    public interface OnItemClickListener {
        void onItemClick(CleanFileBean fileBean);
    }
}
