package com.paint.ar.drawing.adapter;

import android.content.Context;
import android.graphics.Bitmap;
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.paint.ar.drawing.MyApp;
import com.paint.ar.drawing.R;
import com.paint.ar.drawing.helper.Utils;
import com.paint.ar.drawing.api.onItemListener;
import com.paint.ar.drawing.myview.MyTv;

import java.util.List;

public class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.HomeVH> {
    private List<String> nameList = MyApp.getAllDir();

    private Context mCon;
    private String dir;

    private int count = 4;
    private onItemListener listener;
    public ChildAdapter(Context mCon) {
        this.mCon = mCon;
    }

    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_main, parent, false);
        return new HomeVH(view);
    }

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

        holder.name.setText(substring);
        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(dir);
                }

            }
        });


    }

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

    public static class HomeVH extends RecyclerView.ViewHolder {
        private ImageView previewIm;
        private CardView cardView;
        private MyTv name;

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


    }
}
