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 LargeFileAdapter extends RecyclerView.Adapter<LargeFileAdapter.ViewHolder> {
    List<CleanFileBean> list;
    private OnItemClickListener listener;
    public LargeFileAdapter(List<CleanFileBean> list, OnItemClickListener listener) {
        this.list = list;
        this.listener=listener;
    }

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


    public interface OnItemClickListener {
        void onItemClick(CleanFileBean fileBean);
    }
    @NonNull
    @Override
    public LargeFileAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_larage_file_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull LargeFileAdapter.ViewHolder holder, int position) {
        CleanFileBean fileInfo = list.get(position);
        if (fileInfo.getPath()!=null){
            holder.name.setText(fileInfo.getName());
            holder.size.setText(formatFileSize(fileInfo.getLength()));
            // 根据文件类型设置不同的图标或背景等
            if (isImageFile(fileInfo)) {
                Glide.with(holder.itemView.getContext())
                        .asBitmap()
                        .load(fileInfo.getPath())
                        .placeholder(R.mipmap.l_tu)
                        .error(R.mipmap.l_tu)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                        .into(holder.img);
                // 设置图片图标或背景
            } else if (isVideoFile(fileInfo)) {
                Glide.with(holder.itemView.getContext())
                        .asBitmap()
                        .load(fileInfo.getPath())
                        .placeholder(R.mipmap.l_shipin)
                        .error(R.mipmap.l_shipin)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                        .into(holder.img);
                // 设置视频图标或背景
            } else if (isAudioFile(fileInfo)) {
                Glide.with(holder.itemView.getContext())
                        .asBitmap()
                        .load(fileInfo.getPath())
                        .placeholder(R.mipmap.w_audio)
                        .error(R.mipmap.w_audio)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                        .into(holder.img);
                // 设置音频图标或背景
            } else if (isDocFile(fileInfo)) {
                Glide.with(holder.itemView.getContext())
                        .asBitmap()
                        .load(fileInfo.getPath())
                        .placeholder(R.mipmap.l_doc)
                        .error(R.mipmap.l_doc)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                        .into(holder.img);
                // 设置文档图标或背景
            } else if (isApkFile(fileInfo)) {
                Glide.with(holder.itemView.getContext())
                        .asBitmap()
                        .load(fileInfo.getPath())
                        .placeholder(R.mipmap.l_apk)
                        .error(R.mipmap.l_apk)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                        .into(holder.img);
                // 设置 APK 图标或背景
            } else {
                // 设置其他类型的图标或背景
                Glide.with(holder.itemView.getContext())
                        .asBitmap()
                        .load(fileInfo.getPath())
                        .placeholder(R.mipmap.l_other)
                        .error(R.mipmap.l_other)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                        .into(holder.img);
            }

        }

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

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

    }
    public List<CleanFileBean> getSelectedFiles() {
        List<CleanFileBean> selectedFiles = new ArrayList<>();
        for (CleanFileBean fileBean : list) {
            if (fileBean.isSelected()) {
                selectedFiles.add(fileBean);
            }
        }
        return selectedFiles;
    }
    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, size;
        ImageView img;
        CheckBox checkBox;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.item_large_file_list_name);
            size = itemView.findViewById(R.id.item_large_file_list_size);
            img = itemView.findViewById(R.id.item_large_file_list_img);
            checkBox = itemView.findViewById(R.id.item_large_file_list_checked);
        }
    }
    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];
    }
    private boolean isImageFile(CleanFileBean file) {
        // 判断 CleanFileBean 是否代表图片文件
        String mimeType = file.getMineType();
        return mimeType!= null && mimeType.startsWith("image/");
    }

    private boolean isVideoFile(CleanFileBean file) {
        // 判断 CleanFileBean 是否代表视频文件
        String mimeType = file.getMineType();
        return mimeType!= null && mimeType.startsWith("video/");
    }

    private boolean isAudioFile(CleanFileBean file) {
        // 判断 CleanFileBean 是否代表音频文件
        String mimeType = file.getMineType();
        return mimeType!= null && mimeType.startsWith("audio/");
    }

    private boolean isDocFile(CleanFileBean file) {
        // 判断 CleanFileBean 是否代表文档文件
        String mimeType = file.getMineType();
        return mimeType!= null && (mimeType.equals("text/plain") ||
                mimeType.equals("application/msword") ||
                mimeType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document") ||
                mimeType.equals("application/vnd.ms-excel") ||
                mimeType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") ||
                mimeType.equals("application/pdf"));
    }

    private boolean isApkFile(CleanFileBean file) {
        // 判断 CleanFileBean 是否代表 APK 文件
        String fileName = file.getName();
        return fileName!= null && (fileName.endsWith(".apk") || fileName.endsWith(".aab"));
    }
}
