package com.ar.paintar.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.ar.paintar.R;
import com.ar.paintar.activity.PhotoActivity;
import com.ar.paintar.room.AppDatabase;
import com.ar.paintar.room.ImageData;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

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

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

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

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

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

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

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

    @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(ImageData favoriteImage) {
            String favoritePath = favoriteImage.getImagePath();

            loadImage(favoritePath);
            setFavoriteButton(favoriteImage);
            setClickListeners(favoriteImage);
        }

        private void loadImage(String favoritePath) {
            String imagePathToLoad = favoritePath.startsWith("/data/user/")
                    ? favoritePath
                    : "file:///android_asset/" + favoritePath;

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

        private void setFavoriteButton(ImageData favoriteImage) {
            favoriteButton.setImageResource(favoriteImage.isFavoriteStatus()
                    ? R.drawable.favorite
                    : R.drawable.un_favorite);
        }

        private void setClickListeners(final ImageData favoriteImage) {
            imageView.setOnClickListener(v -> {
                Intent intent = new Intent(context, PhotoActivity.class);
                intent.putExtra("imagePath", favoriteImage.getImagePath());
                context.startActivity(intent);
            });

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

        private void toggleFavorite(ImageData favoriteImage) {
            boolean newStatus = !favoriteImage.isFavoriteStatus();
            favoriteImage.setFavoriteStatus(newStatus);

            updateImageInDatabase(favoriteImage);
            notifyItemChanged(getAdapterPosition());
        }

        private void updateImageInDatabase(ImageData favoriteImage) {
            executor.execute(() -> {
                AppDatabase.getInstance(context)
                        .imageDataDao()
                        .update(favoriteImage);
            });
        }
    }
}