package com.ar.drawing.assistant.paint.tooladapter;

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.drawing.assistant.paint.databinding.ItHomeBinding;
import com.ar.drawing.assistant.paint.toollistener.OnClickCallback;
import com.ar.drawing.assistant.paint.utils.Common;
import com.ar.drawing.assistant.paint.utils.MyApp;

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

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

    private Context mCon;

    private OnClickCallback listener;

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

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

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

        return new HomeVH(ItHomeBinding.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 = Common.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);
        AdChild listAdapter = new AdChild(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 ItHomeBinding itemBinding;

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


    }
}
