package com.swiftcleaner.chovey.view.adapter;

import android.view.LayoutInflater;
import android.view.RoundedCorner;
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 com.swiftcleaner.chovey.model.bean.ScreenShotBean;
import com.swiftcleaner.chovey.model.tool.FileHelps;

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


public class ScreenShotAdapter extends RecyclerView.Adapter<ScreenShotAdapter.ViewHolder> {
    List<CleanFileBean> list;
    private OnItemClickListener listener;

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

    @NonNull
    @Override
    public ScreenShotAdapter.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 ScreenShotAdapter.ViewHolder holder, int position) {
        CleanFileBean item = list.get(position);
        if (item != null && item.getPath() != null) {

            Glide.with(holder.itemView.getContext())
                    .load(item.getIcon1())
                    .placeholder(R.mipmap.h_appmanager)
                    .error(R.mipmap.err_img)
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                    .into(holder.item_screen_shot_img);
            holder.item_screen_shot_number.setText(formatFileSize(item.getLength()));

            holder.item_screen_shot_checked.setChecked(item.isSelected());

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

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void removeSelectedFiles(List<CleanFileBean> selectedFiles) {
        list.removeAll(selectedFiles);
        notifyDataSetChanged(); // 通知适配器数据已更改
    }

    public List<CleanFileBean> getSelectedFiles() {
        List<CleanFileBean> selectedFiles = new ArrayList<>();
        for (CleanFileBean fileBean : list) {
            if (fileBean.isSelected()) {
                selectedFiles.add(fileBean);
            }
        }
        return selectedFiles;
    }
    private static 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 class ViewHolder extends RecyclerView.ViewHolder {
        ImageView item_screen_shot_img;
        TextView item_screen_shot_number;
        CheckBox item_screen_shot_checked;

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

    public interface OnItemClickListener {
        void onItemClick(CleanFileBean fileBean);
    }
}
