package com.key.board.journey.inputmethod.list;


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.key.board.journey.inputmethod.databinding.ItemMainBinding;
import com.key.board.journey.inputmethod.myInter.InterfaceList;
import com.key.board.journey.inputmethod.data.ClassBean;
import com.key.board.journey.inputmethod.tools.ItemDec;

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

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

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

    private InterfaceList mCallBack;

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

    public void setClickAction(InterfaceList 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) {

        int adapterPosition = holder.getAdapterPosition();
        ClassBean classBean = mList.get(adapterPosition);
        String parentName = classBean.getParentName();
        holder.binding.className.setText(parentName);
        ItemChild itemChild = new ItemChild(mContext, classBean.getKeyboardList().subList(0, 6));
        holder.binding.childRecycler.setAdapter(itemChild);

        if (holder.binding.childRecycler.getLayoutManager() == null) {
            holder.binding.childRecycler.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL, false));
        }
        if (holder.binding.childRecycler.getItemDecorationCount() == 0) {
            ItemDec itemDec = new ItemDec(5, 5, 4);
            holder.binding.childRecycler.addItemDecoration(itemDec);
        }
        holder.binding.imAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallBack.OnClickSeeAll(parentName);
            }
        });
        if (adapterPosition == (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;


        }

    }


}
