package com.ar.artracestudio.adapter;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
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.artracestudio.R;
import com.ar.artracestudio.activity.ImageActivity;
import com.ar.artracestudio.room.ImageEntry;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import java.util.List;


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

    private final Activity activity;
    private List<ImageEntry> categories;
    private final List<String> originalCategories;
    private final List<String> displayCategories;

    public CategoryAdapter(Activity activity, List<ImageEntry> categories, List<String> originalCategories ,List<String> displayCategories) {
        this.activity = activity;
        this.categories = categories;
        this.originalCategories = originalCategories;
        this.displayCategories = displayCategories;

    }

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

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        String original = originalCategories.get(position);
        String title = displayCategories.get(position);
        ImageEntry imageEntry = categories.get(position);

        holder.textView.setText(title);

        Log.d("category_adapter","imagepaths:  " +categories.size() );
        Log.d("category_adapter","imagepaths:  " +title );
        Log.d("category_adapter","imagepaths:  " +imageEntry.getImagePath() );

        Glide.with(activity)
                .load("file:///android_asset/" + imageEntry.getImagePath())
                .transform(new RoundedCorners(20))
                .into(holder.imageView);

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, ImageActivity.class);
                intent.putExtra("Original", original);
                intent.putExtra("Title", title);
                activity.startActivity(intent);
            }
        });

    }

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

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView;

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


}
