package com.cooleshow.student.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.student.R;
import com.cooleshow.student.bean.TeachableInstrumentBean;

import java.util.List;

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

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


    @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.setItemSelectListener(new TeachableInstrumentItemAdapter.ItemSelectListener() {
                @Override
                public void select(long id) {
                    setOtherUnSelect(id);
                }
            });
        }else {
            rv_item.setVisibility(View.GONE);
        }

    }
    private void setOtherUnSelect(long id){
        for (TeachableInstrumentBean teachableInstrumentBean : getData()) {
            if (null != teachableInstrumentBean.subjects && teachableInstrumentBean.subjects.size() > 0) {
                for (TeachableInstrumentBean subject : teachableInstrumentBean.subjects) {
                    if (subject.id==id){
                        subject.isSelect=true;
                    }else {
                        subject.isSelect=false;
                    }
                }
            }
        }
        TeachableInstrumentAdapter.this.notifyDataSetChanged();
    }

}