package com.app.paintingstool.myadapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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.app.paintingstool.R;
import com.app.paintingstool.myact.CarmelActivity;
import com.app.paintingstool.mydao.AppDatabase;
import com.app.paintingstool.mydao.FavoriteImage;
import com.app.paintingstool.myuntils.ImageItem;
import com.bumptech.glide.Glide;

import java.util.List;

public class ImageAdapter1 extends RecyclerView.Adapter<ImageAdapter1.ImageViewHolder> {

    private final List<ImageItem> imageList;
    private final Context context;
    private Activity activity;

    public ImageAdapter1(List<ImageItem> imageList,Context context,Activity activity) {
        this.imageList = imageList;
        this.context = context;
        this.activity=activity;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
        ImageItem imageItem = imageList.get(position);
        Glide.with(holder.itemView.getContext())
                .load(imageItem.imagePath)
                .into(holder.imageView);
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, CarmelActivity.class);
                intent.putExtra("imagePath",imageItem.imagePath);
                context.startActivity(intent);
            }
        });
        AppDatabase database = AppDatabase.getInstance(context);
        initView(holder, database, imageItem.imagePath);
        holder.loved.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addOrCancel();
            }

            private void addOrCancel() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        FavoriteImage image = database.favoriteImageDao().findByUrl(imageItem.imagePath);
                        if (image != null) {
                            database.favoriteImageDao().delete(image);
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    holder.loved.setImageResource(R.mipmap.icon_loved);
                                    Toast.makeText(context, "Cancel successfully", Toast.LENGTH_SHORT).show();
                                }
                            });
                        } else {
                            database.favoriteImageDao().insert(new FavoriteImage(imageItem.imagePath));
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    holder.loved.setImageResource(R.mipmap.icon_fav);
                                    Toast.makeText(context, "Added successfully", Toast.LENGTH_SHORT).show();
                                }
                            });

                        }
                    }
                }).start();
            }
        });
    }
    private void initView(@NonNull ImageViewHolder holder, AppDatabase database, String name) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                FavoriteImage image = database.favoriteImageDao().findByUrl(name);
                if (image != null) {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            holder.loved.setImageResource(R.mipmap.icon_fav);
                        }
                    });
                } else {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            holder.loved.setImageResource(R.mipmap.icon_loved);
                        }
                    });
                }
            }
        }).start();
    }
    @Override
    public int getItemCount() {
        return imageList.size();
    }

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

        public ImageViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image3);
            loved=itemView.findViewById(R.id.loved2);
        }
    }
}