package com.ar.ardrawingboard.ui.adapter;

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.ardrawingboard.R;
import com.ar.ardrawingboard.ui.activity.BoardActivity;
import com.ar.ardrawingboard.room.AppDatabase;
import com.ar.ardrawingboard.room.BoardEntry;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

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

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

    private List<BoardEntry> favoritesList;
    private final Context appContext;

    public FavoriteAndCategoryAdapter(Context appContext, List<BoardEntry> favoritesList) {
        this.appContext = appContext;
        this.favoritesList = favoritesList;
    }

    public void refreshFavorites(List<BoardEntry> updatedList) {
        this.favoritesList = updatedList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup root, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(appContext);
        View viewItem = inflater.inflate(R.layout.item_import, root, false);
        return new ViewHolder(viewItem);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int pos) {
        BoardEntry entry = favoritesList.get(pos);
        viewHolder.bindItem(entry);
    }

    @Override
    public int getItemCount() {
        return favoritesList != null ? favoritesList.size() : 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final ImageView previewImage;
        private final ImageView favToggle;

        ViewHolder(@NonNull View itemLayout) {
            super(itemLayout);
            previewImage = itemLayout.findViewById(R.id.image_view);
            favToggle = itemLayout.findViewById(R.id.btn_favorite);
        }

        void bindItem(BoardEntry itemData) {
            String imagePath = itemData.getPath();
            String finalPath = imagePath.startsWith("/data/user/") ? imagePath : "file:///android_asset/" + imagePath;

            Glide.with(appContext)
                    .load(finalPath)
                    .transform(new CenterCrop(),new RoundedCorners(32))
                    .placeholder(R.mipmap.placeholder)
                    .error(R.mipmap.placeholder)
                    .into(previewImage);

            previewImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent previewIntent = new Intent(appContext, BoardActivity.class);
                    previewIntent.putExtra("imagePath", imagePath);
                    appContext.startActivity(previewIntent);
                }
            });

            favToggle.setImageResource(itemData.isFavorite() ? R.drawable.like : R.drawable.un_like);

            favToggle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    boolean updatedStatus = !itemData.isFavorite();
                    itemData.setFavorite(updatedStatus);

                    Executors.newSingleThreadExecutor().execute(() -> {
                        AppDatabase database = AppDatabase.getInstance(appContext);
                        database.boardEntryDao().update(itemData);
                    });

                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        notifyItemChanged(position);
                    }
                }
            });
        }
    }
}

