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 android.widget.TextView;

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

import com.ar.paintar.R;
import com.ar.paintar.activity.CategoryActivity;
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 CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {

    private List<ImageData> imageEntries;
    private final Context context;
    private Activity activity;
    private List<String> categories;

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

    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_category, parent, false);
        return new CategoryAdapter.ViewHolder(view);
    }

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

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

    class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private TextView textView;

        ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image);
            textView = itemView.findViewById(R.id.text);
        }

        void bind(ImageData favoriteImage, String name) {
            String favoritePath = favoriteImage.getImagePath();

            setItemText(name);
            loadImage(favoritePath);
            setClickListeners(name);
        }

        private void setItemText(String name) {
            String title = name.substring(4);
            textView.setText(title);
        }


        private void loadImage(String favoritePath) {
            String imagePathToLoad = "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 setClickListeners(String name) {
            imageView.setOnClickListener(v -> {
                Intent intent = new Intent(context, CategoryActivity.class);
                intent.putExtra("Name", name);
                context.startActivity(intent);
            });
        }


    }

}
