package com.lh.painting.adapter;


import static androidx.core.content.ContentProviderCompat.requireContext;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
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.dao.TaskDao;
import com.lh.painting.db.AppDatabase;
import com.lh.painting.entity.Favorite;
import com.lh.painting.manager.Keys;
import com.lh.painting.R;
import com.lh.painting.manager.Utils;

import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.HomeVH> {

    private List<String> nameList = Keys.getAllDir();
    private Favorite favorite;
    private Context mCon;
    private TaskDao dao;
    private String dir;
    public static final  String dadgsfgsf = "app_database";
    public static final int aadasdasd = 1;
    public HomeAdapter(Context mCon, String dir) {
        this.mCon = mCon;
        this.dir = dir;
    }

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

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, @SuppressLint("RecyclerView") int position) {

        String filePath = dir + "/" + (position + 1) + ".jpg";

        // 初始化数据库
        initDb();

        // 重置收藏按钮的初始状态，避免复用问题
        holder.collect.setImageResource(R.drawable.collect); // 初始状态为未收藏

        // 加载图片
        Glide.with(mCon)
                .asDrawable()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .load("file:///android_asset/" + filePath)
                .into(holder.imPreview);

        // 查询数据库，更新收藏状态
        new Thread(() -> {
            Favorite favorite = dao.getTaskByUrl(filePath);  // 查询数据库是否已收藏
            ((Activity) mCon).runOnUiThread(() -> {
                if (favorite != null) {
                    holder.collect.setImageResource(R.drawable.like);  // 如果已收藏，设置为收藏状态
                } else {
                    holder.collect.setImageResource(R.drawable.collect);  // 未收藏，显示默认状态
                }
            });
        }).start();

        // 处理点击事件
        holder.collect.setOnClickListener(view -> {
            new Thread(() -> {
                Favorite favorite1 = dao.getTaskByUrl(filePath);
                if (favorite1 == null) {
                    // 如果未收藏，添加到数据库
                    favorite = new Favorite();
                    favorite.setPicture(filePath);
                    AppDatabase.getDatabase(mCon).taskDao().insertTask(favorite);
                    ((Activity) mCon).runOnUiThread(() -> {
                        holder.collect.setImageResource(R.drawable.like);  // 更新为已收藏状态
                    });
                } else {
                    // 如果已收藏，从数据库中移除
                    AppDatabase.getDatabase(mCon).taskDao().deleteTaskByUrl(filePath);
                    ((Activity) mCon).runOnUiThread(() -> {
                        holder.collect.setImageResource(R.drawable.collect);  // 更新为未收藏状态
                    });
                }
            }).start();
        });

        // 处理点击查看大图的事件
        holder.itemView.setOnClickListener(view -> {
            Intent intent = new Intent(mCon, CameraActivity.class);
            intent.putExtra("filePath", filePath);
            mCon.startActivity(intent);
        });
    }


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

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

    public static class HomeVH extends RecyclerView.ViewHolder {
        private ImageView imPreview;
        private ImageView collect;

        public HomeVH(@NonNull View itemView) {
            super(itemView);
            collect = itemView.findViewById(R.id.collect);
            imPreview = itemView.findViewById(R.id.imPreview);
        }
    }
}
