package com.lh.painting.adapter;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.lh.painting.CameraActivity;
import com.lh.painting.ImportActivity;
import com.lh.painting.R;
import com.lh.painting.dao.TaskDao;
import com.lh.painting.db.AppDatabase;
import com.lh.painting.entity.Favorite;
import java.util.ArrayList;
import java.util.List;

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
    private TaskDao dao;
    private Context context;
    private Favorite favorite;
    private List<String> imagePaths = new ArrayList<>();
    private ImportActivity activity;
    public ImageAdapter(List<String> imagePaths , Context context, ImportActivity activity) {
        this.imagePaths=imagePaths;
        this.context=context;
        this.activity=activity;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Log.d("22222225555", "onBindViewHolder: "+imagePaths.size());

        initDb();
        String imagePath = imagePaths.get(position);
        Glide.with(context)
                .load(imagePath)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.imageView);
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (dao.getTaskByUrl(imagePath) != null) {
                    Log.d("2323232", "run: " + dao.getTaskByUrl(imagePath));
                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            holder.collect.setImageResource(R.drawable.like);
                        }
                    });
                }
            }
        }).start();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, CameraActivity.class);
                intent.putExtra("filePath", imagePath);
                context.startActivity(intent);
            }
        });


        holder.itemView.setOnLongClickListener(v -> {
            // 创建 AlertDialog.Builder
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle("Confirm deletion");
            builder.setMessage("Are you sure you want to delete this picture？");

            // 设置删除按钮
            builder.setPositiveButton("delete", (dialog, which) -> {
                // 执行删除操作
                activity.deleteImage(imagePath, this);  // 传递适配器实例，调用 ImportActivity 中的删除方法
                imagePaths.remove(position);  // 从列表中移除
                notifyItemRemoved(position);  // 通知适配器更新
                notifyItemRangeChanged(position, imagePaths.size());  // 更新剩余项的位置

                activity.runOnUiThread(() -> {
                    // 检查列表是否为空，更新 UI
                    if (imagePaths.isEmpty()) {
                        activity.findViewById(R.id.import_background).setVisibility(View.VISIBLE);
                        activity.findViewById(R.id.import_text).setVisibility(View.VISIBLE);
                    }
                });
            });

            // 设置取消按钮
            builder.setNegativeButton("cancel", (dialog, which) -> dialog.dismiss());

            // 显示对话框
            AlertDialog dialog = builder.create();
            dialog.show();

            return true;  // 表示事件已处理
        });

        holder.collect.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Favorite favorite1 = dao.getTaskByUrl(imagePath);
                        if (favorite1 == null) {
                            favorite = new Favorite();
                            favorite.setPicture(imagePath);
                            AppDatabase.getDatabase(context).taskDao().insertTask(favorite);
                            Log.d("4444444", "run: " + position);
                            Log.d("4444444", "run: " + imagePath);
                            ((Activity) context).runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    holder.collect.setImageResource(R.drawable.like);
//                                    Toast.makeText(context, "Successful collection", Toast.LENGTH_SHORT).show();
                                }
                            });
                        } else {
                            AppDatabase.getDatabase(context).taskDao().deleteTaskByUrl(imagePath);
                            Log.d("4444444", "ru: " + imagePath);
                            ((Activity) context).runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    holder.collect.setImageResource(R.drawable.collect);
//                                    Toast.makeText(context, "cancel collection", Toast.LENGTH_SHORT).show();

                                }
                            });
                        }

                    }
                }).start();
            }
        });


    }

    @Override
    public int getItemCount() {
        Log.d("2332323", "getItemCount: "+imagePaths.size());
        return imagePaths.size();
    }

    private void initDb() {
        //初始化db
        AppDatabase db = AppDatabase.getDatabase(context);
        dao = db.taskDao();
    }

    static class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        ImageView collect;
        ImageView delete;
        ImageViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imPreview);
            collect = itemView.findViewById(R.id.collect);
            delete = itemView.findViewById(R.id.delete);
        }
    }
}

