package com.wallpaper.perfectwallpaper.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.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.wallpaper.perfectwallpaper.R;
import com.wallpaper.perfectwallpaper.activity.CategoryActivity;
import com.wallpaper.perfectwallpaper.room.ImageEntry;

import java.util.List;

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

    private List<ImageEntry> imageEntries;
    private final Context context;
    private final Activity activity;

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

    public void updateData(List<ImageEntry> 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 ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ImageEntry imageEntry = imageEntries.get(position);
        holder.bind(imageEntry);
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

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

        void bind(ImageEntry imageEntry) {
            String imageEntryOriginal = imageEntry.getOriginal();

            textView.setText(imageEntry.getName());
            loadImage(imageEntryOriginal);
            setClickListeners(imageEntry);
        }

        private void loadImage(String imageEntryOriginal) {

            Glide.with(context)
                    .load(imageEntryOriginal)
                    .transform(new RoundedCorners(32))
                    .error(R.mipmap.placeholder)
                    .placeholder(R.mipmap.placeholder)
                    .into(imageView);
        }

        private void setClickListeners(final ImageEntry imageEntry) {
            imageView.setOnClickListener(v -> {
                Intent intent = new Intent(activity, CategoryActivity.class);
                intent.putExtra("Name", imageEntry.getName());
                activity.startActivity(intent);
            });
        }

    }
}