package com.ar.ardrawingboard.ui.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.ardrawingboard.R;
import com.ar.ardrawingboard.ui.activity.CategoryActivity;
import com.ar.ardrawingboard.room.BoardEntry;
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 CategoryFragmentAdapter extends RecyclerView.Adapter<CategoryFragmentAdapter.ViewHolder> {
    private final Activity hostActivity;
    private List<BoardEntry> boardList;
    private final List<String> rawCategoryList;
    private final List<String> shownCategoryNames;

    public CategoryFragmentAdapter(Activity hostActivity, List<BoardEntry> boardList,
                                   List<String> rawCategoryList, List<String> shownCategoryNames) {
        this.hostActivity = hostActivity;
        this.boardList = boardList;
        this.rawCategoryList = rawCategoryList;
        this.shownCategoryNames = shownCategoryNames;
    }

    public void refreshList(List<BoardEntry> updatedBoards) {
        this.boardList = updatedBoards;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder itemHolder, int pos) {
        if (pos >= boardList.size() || pos >= shownCategoryNames.size() || pos >= rawCategoryList.size())
            return;

        BoardEntry entry = boardList.get(pos);
        String shownName = shownCategoryNames.get(pos);
        String rawName = rawCategoryList.get(pos);

        itemHolder.titleView.setText(shownName);

        String assetUri = "file:///android_asset/" + entry.getPath();

        Glide.with(hostActivity)
                .load(assetUri)
                .transform(new CenterCrop(),new RoundedCorners(32))
                .placeholder(R.mipmap.placeholder)
                .error(R.mipmap.placeholder)
                .into(itemHolder.thumbnail);

        itemHolder.thumbnail.setOnClickListener(view -> {
            Intent goToCategory = new Intent(hostActivity, CategoryActivity.class);
            goToCategory.putExtra("Original", rawName);
            goToCategory.putExtra("Title", shownName);
            hostActivity.startActivity(goToCategory);
        });
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder {

        final ImageView thumbnail;
        final TextView titleView;

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