package com.paint.trace.drawing.item;

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.FrameLayout;
import android.widget.ImageView;

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

import com.paint.trace.drawing.R;
import com.paint.trace.drawing.other.Utils;
import com.paint.trace.drawing.alllistener.onItemListener;

import java.util.Objects;

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


    private Context mCon;
    private String dir;

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

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

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

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        String filePath = dir + "/" + (position+2) + ".jpg";
        Bitmap bitmap = Utils.loadImageFromAssets(mCon, filePath);

        holder.previewIm.setImageBitmap(bitmap);

        ViewGroup.LayoutParams layoutParams = holder.previewIm.getLayoutParams();

        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;
        private FrameLayout fl;

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


    }
}
