package com.ar.sketch.drawing.myadapter;

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

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

import com.ar.sketch.drawing.MyApp;
import com.ar.sketch.drawing.databinding.ItemHomeBinding;
import com.ar.sketch.drawing.mycallback.OnClickListener;
import com.ar.sketch.drawing.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

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

    private Context mCon;

    private OnClickListener listener;
    public AdapterMain(Context mCon) {
        this.mCon = mCon;
    }

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

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

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

        holder.itemBinding.name.setText(substring);
        List<String> data = new ArrayList<>();
        data.add(filePath);
        data.add(filePath2);
        data.add(filePath3);
        data.add(filePath4);
        AdapterChild listAdapter = new AdapterChild(mCon, data,dir);
        listAdapter.setListener(listener);
        holder.itemBinding.childRecycler.setAdapter(listAdapter);
        holder.itemBinding.childRecycler.setLayoutManager(new LinearLayoutManager(mCon,LinearLayoutManager.HORIZONTAL, false));

//        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 ItemHomeBinding itemBinding;

        public HomeVH(@NonNull ItemHomeBinding itemHomeBinding) {
            super(itemHomeBinding.getRoot());
            itemBinding = itemHomeBinding;
        }


    }
}
