package com.sketch.art.trace.adapter;

import android.content.Context;
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.sketch.art.trace.databinding.ListMainLayoutBinding;
import com.sketch.art.trace.listener.OnMyClickListener;
import com.sketch.art.trace.tools.App;
import com.sketch.art.trace.tools.ComManager;
import com.sketch.art.trace.tools.ItemDec;

import java.util.List;

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

    private Context mCon;

    private OnMyClickListener listener;

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

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

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

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

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        String dir = nameList.get(position);
        String substring = dir.substring(dir.lastIndexOf("_") + 1);
        List<String> firstFourFilesFromAssetsDir = ComManager.getFirstFourFilesFromAssetsDir(mCon, dir);
        holder.itemBinding.name.setText(substring);

        AdapterChild listAdapter = new AdapterChild(mCon, firstFourFilesFromAssetsDir);
        listAdapter.setListener(listener);
        holder.itemBinding.childRecycler.setAdapter(listAdapter);

        if (holder.itemBinding.childRecycler.getLayoutManager() == null) {
            LinearLayoutManager layoutManager = new LinearLayoutManager(mCon,LinearLayoutManager.HORIZONTAL,false);
            holder.itemBinding.childRecycler.setLayoutManager(layoutManager);
        }

        if (holder.itemBinding.childRecycler.getItemDecorationCount() == 0) {
            ItemDec itemDec = new ItemDec(5,5,0);
            holder.itemBinding.childRecycler.addItemDecoration(itemDec);
        }

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

            }
        });
        if(position == nameList.size()-1){
            holder.itemBinding.view.setVisibility(View.VISIBLE);
        }else {
            holder.itemBinding.view.setVisibility(View.GONE);
        }


    }


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

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

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


    }
}
