package com.wallpaper.perfectwallpaper.adapter;

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 androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.wallpaper.perfectwallpaper.R;
import com.wallpaper.perfectwallpaper.activity.ImageActivity;
import com.wallpaper.perfectwallpaper.room.AppDatabase;
import com.wallpaper.perfectwallpaper.room.ImageEntry;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

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

    private List<ImageEntry> imageEntries;
    private final Context context;
    private Activity activity;
    private final Executor executor = Executors.newSingleThreadExecutor();

    public ImageAdapter(Context context, List<ImageEntry> imageEntries, Activity activity) {
        this.context = context;
        this.imageEntries = imageEntries;
        this.activity = activity;
    }

    public void updateData(List<ImageEntry> newFavoriteImages) {
        this.imageEntries = newFavoriteImages;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ImageEntry imageEntry = imageEntries.get(position);
        holder.bind(imageEntry);
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        ImageView favoriteButton;

        ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_view);
            favoriteButton = itemView.findViewById(R.id.btn_favorite);
        }

        void bind(ImageEntry imageEntry) {
            String imageEntryOriginal = imageEntry.getOriginal();

            loadImage(imageEntryOriginal);
            setFavoriteButton(imageEntry);
            setClickListeners(imageEntry);
        }

        private void loadImage(String imageEntryOriginal) {

            Glide.with(context)
                    .load(imageEntryOriginal)
                    .transform(new RoundedCorners(16))
                    .error(R.mipmap.placeholder)
                    .placeholder(R.mipmap.placeholder)
                    .into(imageView);
        }

        private void setFavoriteButton(ImageEntry imageEntry) {
            favoriteButton.setImageResource(imageEntry.getLike()
                    ? R.drawable.like
                    : R.drawable.un_like);
        }

        private void setClickListeners(final ImageEntry imageEntry) {
            imageView.setOnClickListener(v -> {
                Intent intent = new Intent(activity, ImageActivity.class);
                intent.putExtra("imagePath", imageEntry.getSource());
                activity.startActivity(intent);
            });

            favoriteButton.setOnClickListener(v -> toggleFavorite(imageEntry));
        }

        private void toggleFavorite(ImageEntry imageEntry) {
            boolean newStatus = !imageEntry.getLike();
            imageEntry.setLike(newStatus);

            updateImageInDatabase(imageEntry);
            notifyItemChanged(getAdapterPosition());
        }

        private void updateImageInDatabase(ImageEntry imageEntry) {
            executor.execute(() -> {
                AppDatabase.getInstance(context)
                        .imageEntryDao()
                        .update(imageEntry);
            });
        }
    }
}