package com.ar.imagetracear.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.ar.imagetracear.R;
import com.ar.imagetracear.database.ImageTraceEntry;
import com.ar.imagetracear.activity.SubListActivity;
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;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ItemHolder> {

    private final Activity activityContext;
    private List<ImageTraceEntry> imageEntries;
    private final List<String> originalCategories;
    private final List<String> displayCategoryNames;

    public HomeAdapter(Activity activityContext, List<ImageTraceEntry> imageEntries,
                       List<String> originalCategories, List<String> displayCategoryNames) {
        this.activityContext = activityContext;
        this.imageEntries = imageEntries;
        this.originalCategories = originalCategories;
        this.displayCategoryNames = displayCategoryNames;
    }

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

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View layoutView = layoutInflater.inflate(R.layout.item_home, parent, false);
        return new ItemHolder(layoutView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
        if (position < 0 || position >= imageEntries.size()
                || position >= displayCategoryNames.size()
                || position >= originalCategories.size()) {
            return;
        }

        ImageTraceEntry currentEntry = imageEntries.get(position);
        String displayName = displayCategoryNames.get(position);
        String originalName = originalCategories.get(position);

        holder.textView.setText(displayName);

        String imagePath = "file:///android_asset/" + currentEntry.getPath();

        Glide.with(activityContext)
                .load(imagePath)
                .transform(new CenterCrop(), new RoundedCorners(32))
                .placeholder(R.mipmap.placeholder)
                .error(R.mipmap.placeholder)
                .into(holder.imageView);

        holder.imageView.setOnClickListener(v -> {
            Intent intent = new Intent(activityContext, SubListActivity.class);
            intent.putExtra("Original", originalName);
            intent.putExtra("Title", displayName);
            activityContext.startActivity(intent);
        });
    }

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

    static class ItemHolder extends RecyclerView.ViewHolder {

        final ImageView imageView;
        final TextView textView;

        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.item_imageview);
            textView = itemView.findViewById(R.id.item_textview);
        }
    }
}