package com.cooleshow.teacher.adapter;

import android.view.View;
import android.widget.TextView;

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

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.TeachableInstrumentBean;

import java.util.List;

/**
 * 创建日期：2022/5/13 17:31
 *
 * @author Ryan
 * 类说明：
 */
public class TeachableInstrumentAdapter extends BaseQuickAdapter<TeachableInstrumentBean, BaseViewHolder> {

    private int mSelectCount;
    private int count;

    public TeachableInstrumentAdapter(List<TeachableInstrumentBean> data, int selectCount) {
        super(R.layout.layout_teachable_instrument, data);
        mSelectCount = selectCount;
    }


    @Override
    protected void convert(BaseViewHolder helper, TeachableInstrumentBean item) {
        TextView tv_title = helper.getView(R.id.tv_title);
        tv_title.setText(item.name);
        RecyclerView rv_item = helper.getView(R.id.rv_item);
        if (null != item.subjects && item.subjects.size() > 0) {
            rv_item.setVisibility(View.VISIBLE);
            GridLayoutManager manager = new GridLayoutManager(getContext(), 3);
            rv_item.setHasFixedSize(true);
            rv_item.setLayoutManager(manager);
            TeachableInstrumentItemAdapter teachableInstrumentItemAdapter = new TeachableInstrumentItemAdapter(item.subjects);
            rv_item.setNestedScrollingEnabled(false);
            rv_item.setAdapter(teachableInstrumentItemAdapter);
            teachableInstrumentItemAdapter.setSelectedListener((bean, position) -> {
                if (!bean.isSelect) {
                    count++;
                    bean.isSelect = true;
                    if (count + mSelectCount > 5) {
                        count--;
                        bean.isSelect = false;
                        ToastUtil.getInstance().showShort("最多选5个乐器！");
                    }
                } else {
                    bean.isSelect = false;
                    count--;
                }
                teachableInstrumentItemAdapter.notifyItemChanged(position);
            });
        } else {
            rv_item.setVisibility(View.GONE);
        }

    }

}