package com.bonait.bnframework.common.linktab;

import android.content.res.Resources;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;

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

import com.bonait.bnframework.R;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.List;

public class LeftAdapter extends BaseQuickAdapter<LinkMode, BaseViewHolder> {
    public List<LinkMode> Fdata;
    public Resources resources;
    public LinearLayoutManager Manager;
    public LeftAdapter(@LayoutRes int layoutResId, @Nullable List<LinkMode> data, Resources _resources, LinearLayoutManager _manager) {
        super(layoutResId, data);
        resources=_resources;
        Fdata=data;
        Manager=_manager;
    }

    @Override
    protected void convert(BaseViewHolder holder, LinkMode item) {
        holder.setText(R.id.tv_type,(String)item.type);
        int index = Fdata.indexOf(item);
        if (item.isChoose)
        {
            holder.setBackgroundColor(R.id.tv_linearlayout, resources.getColor(R.color.main_background));
            holder.setTextColor(R.id.tv_type, resources.getColor(R.color.app_color_blue));
            holder.setVisible(R.id.tv_image,true);
        }
        else
        {
            holder.setBackgroundColor(R.id.tv_linearlayout, resources.getColor(R.color.activity_background1));
            holder.setTextColor(R.id.tv_type, resources.getColor(R.color.tab_text_normal));
            holder.setVisible(R.id.tv_image,false);
        }

        //点击事件
        holder.getView(R.id.tv_linearlayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setChoose(index);
                Manager.scrollToPositionWithOffset(index, 0);
            }
        });
		/*这里设置子条目点击事件需要注意一下！
		如果 添加了固定的某控件id点击事件（如下文的 .addOnLongClickListener(R.id.tv_btn)），
		同时你又把子条目的id控件设置成了match的属性！那么OnItemOnClick你会发现调用不到！！！
		这时候你需要用到的子条目监听就是setOnItemChildClickListener，然后去绑定对应的id！
		如果只是需要子条目的点击事件，，不需要add任何Listener！！！
		**/
        //holder.addOnClickListener(R.id.tv_btn).addOnLongClickListener(R.id.tv_btn);
    }



    /**
     * 设置是否选择
     * @param position
     */
    public void setChoose(Integer position)
    {
        for (LinkMode it:Fdata)
        {
            it.isChoose = false;
        }
        Fdata.get(position).isChoose = true;
        notifyDataSetChanged();
    }
}
