package com.bjzc.zjyxdxj.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bjzc.zjyxdxj.R;
import com.bjzc.zjyxdxj.bean.DeviceTreeBean;

import java.util.List;


/**
 * Created by huangwenfei on 2022/4/7.
 */

public class DeviceTreeAdapter extends RecyclerView.Adapter<DeviceTreeAdapter.ViewHolder>{

    private LayoutInflater mLayoutInflater;
    private List<DeviceTreeBean.DataBean> datas;
    private Context mContext;

    private int mSelectedPos = 0;   //实现单选，保存当前选中的position

    private OnChildItemClickListener onChildItemClickListener;


    public    DeviceTreeAdapter(Context context, List<DeviceTreeBean.DataBean> data) {
        mLayoutInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.datas = data;
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).isSelected()) {
                mSelectedPos = i;
            }
        }
    }

    public List<DeviceTreeBean.DataBean> getDatas() {
        return datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.item_device_tree, null);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") int position) {

        holder.cb.setChecked(datas.get(position).isSelected());
        if(datas.get(position).getISLEAF().equals("1")){
            holder.iv_subDevice.setVisibility(View.GONE);
            holder.cb.setVisibility(View.GONE);
        }else{
            holder.iv_subDevice.setVisibility(View.VISIBLE);
            holder.cb.setVisibility(View.GONE);
        }

        holder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                datas.get(mSelectedPos).setSelected(false);

                mSelectedPos = position;

                datas.get(mSelectedPos).setSelected(true);

                notifyDataSetChanged();
            }
        });


        holder.tv_smmc.setText("["+datas.get(position).getID()+"]" + "-" +"[" +datas.get(position).getNNAME()+"]");
        holder.tv_smmc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onChildItemClickListener.onClick(datas.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void setOnChildItemClickListener(OnChildItemClickListener onChildItemClickListener) { //接口回调
        this.onChildItemClickListener = onChildItemClickListener;
    }

    public interface OnChildItemClickListener {
        void onClick(DeviceTreeBean.DataBean dataBean);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        CheckBox cb;
        TextView tv_smmc;
        ImageView iv_subDevice;
        LinearLayout itemViewDevice;

        public ViewHolder(View itemView) {
            super(itemView);
            cb = itemView.findViewById(R.id.cb);
            tv_smmc = itemView.findViewById(R.id.tv_smmc);
            iv_subDevice = itemView.findViewById(R.id.subDevice);
            itemViewDevice = itemView.findViewById(R.id.itemViewDevice);
        }
    }

}