package com.picture.ar.drawing.list;

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.picture.ar.drawing.databinding.ListMainBinding;
import com.picture.ar.drawing.listener.ClickListener;
import com.picture.ar.drawing.utils.App;
import com.picture.ar.drawing.utils.ComUtils;
import com.picture.ar.drawing.utils.ItemSpace;

import java.util.List;

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

    private Context mCon;

    private ClickListener listener;

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

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

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

        return new HomeVH(ListMainBinding.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 = ComUtils.getFirstFourFilesFromAssetsDir(mCon, dir);
        holder.itemBinding.name.setText(substring);

        MainChildList listAdapter = new MainChildList(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 ListMainBinding itemBinding;

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


    }
}
