package com.draw.painting.line.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

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

import com.draw.painting.line.R;
import com.draw.painting.line.manager.AppHelper;
import com.draw.painting.line.onClickListener;

public class CategoryThumbAdapter extends RecyclerView.Adapter<CategoryThumbAdapter.ThumbVH> {


    private Context mCon;
    private String dir;

    private int count = 4;
    private onClickListener listener;
    public CategoryThumbAdapter(Context mCon, String dirStr) {
        this.mCon = mCon;
        dir = dirStr;
    }

    public void setListener(onClickListener listener) {
        this.listener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ThumbVH holder, int position) {
        String filePath = dir + "/" + (position+1) + ".jpg";

        Log.d("---------tt","---------filePath="+filePath);
        Bitmap bitmap = AppHelper.loadImageFromAssets(mCon, filePath);
        holder.previewIm.setImageBitmap(bitmap);
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!= null){
                    listener.onClickItem(false,filePath);
                }

            }
        });


    }

    @Override
    public int getItemCount() {
        return count;
    }

    public static class ThumbVH extends RecyclerView.ViewHolder {
        private ImageView previewIm;
        private CardView cardView;

        public ThumbVH(@NonNull View itemView) {
            super(itemView);
            previewIm = itemView.findViewById(R.id.imPreview);
           cardView = itemView.findViewById(R.id.card);
        }


    }
}

