package com.app.paintingstool.myadapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

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

import com.app.paintingstool.R;
import com.app.paintingstool.myact.CarmelActivity;
import com.app.paintingstool.mydao.AppDatabase;
import com.app.paintingstool.mydao.FavoriteImage;
import com.bumptech.glide.Glide;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class LovedAdapter extends RecyclerView.Adapter<LovedAdapter.ViewHolder> {

    private final List<FavoriteImage> favoriteImages;
    private Context context;
    private Activity activity;

    public LovedAdapter(List<FavoriteImage> favoriteImages, Context context, Activity activity) {
        this.favoriteImages = favoriteImages;
        this.context = context;
        this.activity = activity;
    }

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


    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        String imagePath = favoriteImages.get(position).imageUrl;
        holder.loved.setImageResource(R.mipmap.icon_fav);
        if (isAssetFile(imagePath)) {
            Glide.with(context)
                    .load("file:///android_asset/" + imagePath)
                    .centerCrop()
                    .into(holder.imageView);
        } else {
            Glide.with(context)
                    .load(imagePath)
                    .centerCrop()
                    .into(holder.imageView);
        }
        AppDatabase database = AppDatabase.getInstance(context);
        holder.loved.setOnClickListener(view -> {
            int pos = holder.getAdapterPosition();
            new Thread(() -> {
                FavoriteImage image = database.favoriteImageDao().findByUrl(imagePath);
                if (image != null) {
                    database.favoriteImageDao().delete(image);
                    favoriteImages.remove(pos);
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            notifyDataSetChanged();
                            if (favoriteImages.isEmpty()) {
                                activity.findViewById(R.id.re_def2).setVisibility(View.VISIBLE);
                            }
                        }
                    });
                }
            }).start();
        });
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, CarmelActivity.class);
                intent.putExtra("imagePathOrAssetPath", imagePath);
                Log.d("-------------", "onClick: " + imagePath);
                context.startActivity(intent);
            }
        });
    }

    private boolean isAssetFile(String imagePath) {
        AssetManager assetManager = context.getAssets();
        try {
            InputStream inputStream = assetManager.open(imagePath);
            inputStream.close();
            return true;
        } catch (IOException e) {
            return false;
        }
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        ImageView loved;

        public ViewHolder(View view) {
            super(view);
            imageView = view.findViewById(R.id.image3);
            loved = view.findViewById(R.id.loved2);

        }
    }
}