package com.tenlionsoft.oamodule.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tenlionsoft.baselib.core.widget.base.BaseRecyclerAdapter;
import com.tenlionsoft.oamodule.R;
import com.tenlionsoft.oamodule.beans.ChoosePersonListBean;
import com.tenlionsoft.oamodule.beans.DepartmentListBean;
import com.tenlionsoft.oamodule.holder.ChooseShowDeptHolder;
import com.tenlionsoft.oamodule.holder.PersonSelHolder;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * 作者: adam
 * 日期: 2020/11/24 - 9:16 AM
 * 邮箱: itgaojian@163.com
 * 描述: 部门人员选择
 */
public class ChooseDeptUserAdapter extends BaseRecyclerAdapter<Object, RecyclerView.ViewHolder> {
    public List<ChoosePersonListBean> mSelBeans = new ArrayList<>();

    public ChooseDeptUserAdapter(Context ctx, List<Object> list) {
        super(ctx, list);
    }

    @Override
    public RecyclerView.ViewHolder createHolder(ViewGroup parent, int viewType) {
        if (viewType == 2) {
            //部门
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_dept_show, parent, false);
            return new ChooseShowDeptHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_person_sel, parent, false);
            return new PersonSelHolder(itemView);
        }
    }

    @Override
    public void bindHolder(RecyclerView.ViewHolder holder, int i) {
        if (holder instanceof ChooseShowDeptHolder) {
            ChooseShowDeptHolder deptHolder = (ChooseShowDeptHolder) holder;
            DepartmentListBean bean = (DepartmentListBean) mData.get(i);
            deptHolder.mTvName.setText(bean.getDepartmentName());
            deptHolder.mTvName.setOnClickListener(v -> mLis.onLowerDept(bean, i));
        } else {
            PersonSelHolder personHolder = (PersonSelHolder) holder;
            ChoosePersonListBean bean = (ChoosePersonListBean) mData.get(i);
            personHolder.mTvName.setText(bean.getUserName());
            personHolder.mCbSel.setOnCheckedChangeListener(null);
            personHolder.mCbSel.setChecked(bean.isCheck());
            personHolder.mCbSel.setOnCheckedChangeListener((buttonView, isChecked) -> mLis.onItemCheckChange(bean, isChecked, i));
        }

    }

    @Override
    public int getItemViewType(int position) {
        Object o = mData.get(position);
        if (o instanceof DepartmentListBean) {
            return 2;
        } else {
            return 3;
        }
    }

    private OnItemCheckChange mLis;

    public void addOnItemCheckChange(OnItemCheckChange lis) {
        this.mLis = lis;
    }

    public interface OnItemCheckChange {
        void onItemCheckChange(ChoosePersonListBean bean, boolean isCheck, int pos);

        void onLowerDept(DepartmentListBean bean, int pos);
    }
}
