package com.wisewoods.eduandroid.area;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wisewoods.base.base.BaseAdapter;
import com.wisewoods.base.base.BaseVH;
import com.wisewoods.base.bean.AreaBean;
import com.wisewoods.eduandroid.R;


public class VpAdapter extends BaseAdapter<AreaBean> {

    public interface ClickListener {
        void click(int layoutPosition, AreaBean.DataBean item);
    }
    private ClickListener listener;

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

    public VpAdapter() {
        super(R.layout.fragment_list);
    }

    @Override
    protected void convert(@NonNull BaseVH baseVH, AreaBean areaBean) {
        int layoutPosition = baseVH.getLayoutPosition();
        RecyclerView rv = baseVH.getView(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        VpListAdapter mAdapter = new VpListAdapter();
        mAdapter.setOnItemClickListener((adapter1, view, position) -> {
            for (int i = 0; i < mAdapter.getData().size(); i++) {
                if (i == position){
                    if (!mAdapter.getItem(i).isCheck){
                        mAdapter.getItem(position).isCheck = true;
                        mAdapter.setData(position, mAdapter.getItem(position));
                    }
                    if (listener!=null){
                        listener.click(layoutPosition,mAdapter.getItem(position));
                    }
                }else{
                    if (mAdapter.getItem(i).isCheck){
                        mAdapter.getItem(i).isCheck = false;
                        mAdapter.setData(i, mAdapter.getItem(i));
                    }
                }
            }
        });

        rv.setAdapter(mAdapter);
        mAdapter.setNewInstance(areaBean.data);
    }


}
