package com.ar.imagetracear.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.imagetracear.R;
import com.ar.imagetracear.database.AppDatabase;
import com.ar.imagetracear.database.ImageTraceEntry;
import com.ar.imagetracear.activity.ImageTraceActivity;
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 SubListAdapter extends RecyclerView.Adapter<SubListAdapter.ItemHolder> {

    private List<ImageTraceEntry> entryList;
    private final Context contextRef;

    public SubListAdapter(Context contextRef, List<ImageTraceEntry> entryList) {
        this.contextRef = contextRef;
        this.entryList = entryList;
    }

    public void updateEntries(List<ImageTraceEntry> newList) {
        this.entryList = newList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup parentGroup, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(contextRef);
        View listItemView = layoutInflater.inflate(R.layout.item_import, parentGroup, false);
        return new ItemHolder(listItemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
        ImageTraceEntry trace = entryList.get(position);
        holder.display(trace);
    }

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

    class ItemHolder extends RecyclerView.ViewHolder {

        private final ImageView imageView;
        private final ImageView favIcon;

        ItemHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_view);
            favIcon = itemView.findViewById(R.id.btn_favorite);
        }

        void display(ImageTraceEntry data) {
            final String originalPath = data.getPath();
            final String displayPath = originalPath.startsWith("/data/user/")
                    ? originalPath
                    : "file:///android_asset/" + originalPath;

            Glide.with(contextRef)
                    .load(displayPath)
                    .transform(new CenterCrop(), new RoundedCorners(32))
                    .placeholder(R.mipmap.placeholder)
                    .error(R.mipmap.placeholder)
                    .into(imageView);

            imageView.setOnClickListener(view -> {
                Intent showIntent = new Intent(contextRef, ImageTraceActivity.class);
                showIntent.putExtra("imagePath", originalPath);
                contextRef.startActivity(showIntent);
            });

            favIcon.setImageResource(data.isFavorite() ? R.drawable.like : R.drawable.dislike);

            favIcon.setOnClickListener(view -> {
                boolean toggledFavorite = !data.isFavorite();
                data.setFavorite(toggledFavorite);

                Executors.newSingleThreadExecutor().execute(() -> {
                    AppDatabase.getInstance(contextRef).imageTraceEntryDao().update(data);
                });

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