package com.rich.keyboard.skin.listadapter;


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.rich.keyboard.skin.MyApp;
import com.rich.keyboard.skin.databinding.ItemMainBinding;
import com.rich.keyboard.skin.callback.OnSeeAllCallback;
import com.rich.keyboard.skin.localdata.ParentData;
import com.rich.keyboard.skin.utils.MyCommon;
import com.rich.keyboard.skin.utils.MyItemDecoration;

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

public class MainList extends RecyclerView.Adapter<MainList.MainViewHolder> {

    private Context mContext;
    private List<ParentData> mList = new ArrayList<>();

    private OnSeeAllCallback mCallBack;

    public MainList(Context context, List<ParentData> list) {
        mContext = context;
        this.mList = list;
    }

    public void setClickAction(OnSeeAllCallback callback) {
        mCallBack = callback;
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemMainBinding inflate = ItemMainBinding.inflate(LayoutInflater.from(parent.getContext()));


        return new MainViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        ParentData ParentData = mList.get(position);
        String parentName = ParentData.getParentName();
        holder.binding.className.setText(parentName);


        MainChildList mainChildList = new MainChildList(mContext, ParentData.getKeyboardList().subList(0, 4));
        holder.binding.childRecycler.setHasFixedSize(true); // 可选：提高性能
        holder.binding.childRecycler.setNestedScrollingEnabled(false); // 禁用嵌套滑动
        holder.binding.childRecycler.setAdapter(mainChildList);
        Log.d(MyApp.TAG,"--------11111111111111");
        if (holder.binding.childRecycler.getLayoutManager() == null) {
            Log.d(MyApp.TAG,"--------11111111111-2222222222");
            GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
            holder.binding.childRecycler.setLayoutManager(gridLayoutManager);
        }

        if (holder.binding.childRecycler.getItemDecorationCount() == 0) {
            Log.d(MyApp.TAG,"--------11111111111-333333333");
            MyItemDecoration itemDec = new MyItemDecoration(MyCommon.INSTANCE.dpToPx(15), MyCommon.INSTANCE.dpToPx(5), MyCommon.INSTANCE.dpToPx(0));
            holder.binding.childRecycler.addItemDecoration(itemDec);
        }
        holder.binding.viewAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallBack.OnClickSeeAll(parentName);
            }
        });
        if (position == (mList.size() - 1)) {
            holder.binding.space.setVisibility(View.VISIBLE);
        } else {
            holder.binding.space.setVisibility(View.GONE);
        }

    }

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

    public static class MainViewHolder extends RecyclerView.ViewHolder {

        private ItemMainBinding binding;


        public MainViewHolder(@NonNull ItemMainBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;


        }

    }


}
