package com.lh.wallpaper2.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.lh.wallpaper2.R;
import com.lh.wallpaper2.ViewActivity;

import java.util.List;

public class ImageRecyclerViewAdapter extends RecyclerView.Adapter<ImageRecyclerViewAdapter.ViewHolder> {
    private List<String> imagePaths;
    private final Activity context;
    private OnImageDeleteListener onImageDeleteListener; // 删除监听器

    public ImageRecyclerViewAdapter(List<String> imagePaths, Activity context) {
        this.imagePaths = imagePaths;
        this.context = context;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String imagePath = imagePaths.get(position);

        RequestOptions requestOptions = new RequestOptions()
                .transform(new CenterCrop(), new RoundedCorners(20)); // 20 是圆角半径

        if (imagePath.startsWith("/data/user/")) {
            Glide.with(context)
                    .load(imagePath)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .apply(requestOptions)
                    .placeholder(R.drawable.preview)
                    .into(holder.imageView);
        } else {
            Glide.with(context)
                    .load("file:///android_asset/" + imagePath)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .apply(requestOptions)
                    .placeholder(R.drawable.preview)
                    .into(holder.imageView);
        }

        holder.imageView.setOnClickListener(v -> {
            Intent intent = new Intent(context, ViewActivity.class);
            intent.putExtra("import", imagePath);
            context.startActivity(intent);
        });

        // 设置删除按钮的点击事件
        holder.delete.setOnClickListener(v -> {
            int adapterPosition = holder.getAdapterPosition(); // 动态获取当前位置
            if (adapterPosition != RecyclerView.NO_POSITION && onImageDeleteListener != null) {
                onImageDeleteListener.onImageDelete(imagePaths.get(adapterPosition)); // 调用删除回调
            }
        });
    }

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

    // 更新数据并通知数据集更改
    public void updateImagePaths(List<String> newImagePaths) {
        this.imagePaths = newImagePaths;
        notifyDataSetChanged();
    }

    // 设置删除监听器
    public void setOnImageDeleteListener(OnImageDeleteListener listener) {
        this.onImageDeleteListener = listener;
    }

    public interface OnImageDeleteListener {
        void onImageDelete(String imagePath);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        ImageView delete;

        ViewHolder(View view) {
            super(view);
            imageView = view.findViewById(R.id.imageView_wallpaper);
            delete = view.findViewById(R.id.delete);
        }
    }
}
