package com.trace.drawing.people.item;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.trace.drawing.people.callbacl.OnClickCallback;
import com.trace.drawing.people.databinding.RecyclerMainBinding;
import com.trace.drawing.people.tools.App;
import com.trace.drawing.people.tools.Common;
import com.trace.drawing.people.tools.ItemSpace;

import java.util.List;

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

    private Context mCon;

    private OnClickCallback listener;

    public MainItem(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(RecyclerMainBinding.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 = Common.getFirstFourFilesFromAssetsDir(mCon, dir);
        holder.itemBinding.name.setText(substring);

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

        if (holder.itemBinding.childRecycler.getLayoutManager() == null) {
            Log.d(App.TAG,"--------11111111111-2222222222");
            GridLayoutManager gridLayoutManager = new GridLayoutManager(mCon, 2);
            holder.itemBinding.childRecycler.setLayoutManager(gridLayoutManager);
        }

        if (holder.itemBinding.childRecycler.getItemDecorationCount() == 0) {
            Log.d(App.TAG,"--------11111111111-333333333");
            ItemSpace itemSpace = new ItemSpace(5,5,0);
            holder.itemBinding.childRecycler.addItemDecoration(itemSpace);
        }

        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 RecyclerMainBinding itemBinding;

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


    }
}
