package com.ar.sketch.paint.drawing.adapter;

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

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

import com.ar.sketch.paint.drawing.api.OnClick;
import com.ar.sketch.paint.drawing.databinding.ListHomeBinding;
import com.ar.sketch.paint.drawing.tool.MyArApp;
import com.ar.sketch.paint.drawing.tool.Utils;

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

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

    private Context mCon;

    private OnClick listener;

    public ListMain(Context mCon) {
        this.mCon = mCon;
    }

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

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

        return new HomeVH(ListHomeBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @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 = Utils.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);
        listChild listAdapter = new listChild(mCon, data);
        listAdapter.setListener(listener);
        holder.itemBinding.childRecycler.setAdapter(listAdapter);
        holder.itemBinding.childRecycler.setLayoutManager(new LinearLayoutManager(mCon, LinearLayoutManager.HORIZONTAL, false));

        holder.itemBinding.all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClickItem(0, dir);
                }

            }
        });


    }


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

    public static class HomeVH extends RecyclerView.ViewHolder {
        private ListHomeBinding itemBinding;

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


    }
}
