package com.ar.sketch.drawing.myadapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.ar.sketch.drawing.MyApp;
import com.ar.sketch.drawing.databinding.ItemChildBinding;
import com.ar.sketch.drawing.mycallback.OnClickListener;
import com.ar.sketch.drawing.utils.MyUtils;
import com.ar.sketch.drawing.activity.ActivityList;
import com.ar.sketch.drawing.activity.ActivityPreview;

import java.util.List;

public class AdapterChild extends RecyclerView.Adapter<AdapterChild.ChildVH> {


    private Context mCon;
    private List<String> data;
    private String mDir;
    private OnClickListener listener;


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

    public AdapterChild(Context mCon, List<String> bitmapList, String dir) {
        this.mCon = mCon;
        data = bitmapList;
        mDir = dir;
    }


    @NonNull
    @Override
    public ChildVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        ItemChildBinding inflate = ItemChildBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ChildVH(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ChildVH holder, int position) {

        String name = data.get(position);
        Bitmap bitmap = MyUtils.loadImageFromAssets(mCon, name);
        holder.binding.imPreview.setImageBitmap(bitmap);

        if (position == data.size() - 1) {
            holder.binding.tvMore.setVisibility(View.VISIBLE);
            holder.binding.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClickItem(0, mDir);
                    }

                }
            });
        } else {
            holder.binding.tvMore.setVisibility(View.GONE);
            holder.binding.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClickItem(1, name);
                    }
                }
            });
        }


    }

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

    public static class ChildVH extends RecyclerView.ViewHolder {


        private ItemChildBinding binding;


        public ChildVH(@NonNull ItemChildBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }


    }
}
