package com.draw.painting.canvas.adapters;

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.canvas.R;
import com.draw.painting.canvas.core.SystemHelper;
import com.draw.painting.canvas.onClickListener;

public class SketchItemAdapter extends RecyclerView.Adapter<SketchItemAdapter.HomeVH> {


    private Context mCon;
    private String dir;

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

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

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

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

        Log.d("---------tt","---------filePath="+filePath);
        Bitmap bitmap = SystemHelper.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 HomeVH extends RecyclerView.ViewHolder {
        private ImageView previewIm;
        private CardView cardView;

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


    }
}

