package xn.hxp.ui.main.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.appcompat.widget.AppCompatTextView;

import com.kongzue.dialogx.dialogs.PopTip;
import com.zhpan.bannerview.BaseBannerAdapter;
import com.zhpan.bannerview.BaseViewHolder;
import com.zhpan.bannerview.utils.BannerUtils;

import java.util.List;

import xn.hxp.R;
import xn.hxp.room.bean.cabinet.Cabinet;
import xn.hxp.room.bean.cabinet.CabinetDoor;
import xn.hxp.room.bean.cabinet.CabinetDoorAdmin;

public class CabinetAdapter extends BaseBannerAdapter<Cabinet> {
    private final StringBuffer stringBuffer = new StringBuffer();

    @Override
    protected void bindData(BaseViewHolder<Cabinet> holder, Cabinet data, int position, int pageSize) {
        int adapterPosition = holder.getAdapterPosition();
        int realPosition = BannerUtils.getRealPosition(position, adapterPosition);

        List<CabinetDoor> cabinetDoorList = data.getCabinetDoorVoList();
        // 柜门名字
        AppCompatTextView cabinetNameTV = holder.findViewById(R.id.cabinetName);
        cabinetNameTV.setText(data.getCabinetName());
        // 左柜门布局
        RelativeLayout doorRLL = holder.findViewById(R.id.doorRLL);
        doorRLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CabinetDoor cabinetDoor = cabinetDoorList.get(0);
                PopTip.show("点击左柜门:" + data.getCabinetName() + "__" + cabinetDoor.getDoorName());
            }
        });
        // 右柜门布局
        RelativeLayout doorRLR = holder.findViewById(R.id.doorRLR);
        doorRLR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CabinetDoor cabinetDoor = cabinetDoorList.get(1);
                PopTip.show("点击右柜门:" + data.getCabinetName() + "__" + cabinetDoor.getDoorName());
            }
        });
        if (null != cabinetDoorList && !cabinetDoorList.isEmpty()) {
            // 左柜门
            doorRLL.setVisibility(View.VISIBLE);
            CabinetDoor cabinetDoorL = cabinetDoorList.get(0);
            // 左柜门名字
            AppCompatTextView doorName1 = holder.findViewById(R.id.doorName1);
            doorName1.setText(TextUtils.isEmpty(cabinetDoorL.getDoorName()) ? "" : cabinetDoorL.getDoorName());
            // 左柜门化学品总数
            AppCompatTextView doorHxpSize1 = holder.findViewById(R.id.doorHxpSize1);
            doorHxpSize1.setText(String.valueOf(cabinetDoorL.getStockNum()));
            // 左柜门管理员
            AppCompatTextView doorAdmin1 = holder.findViewById(R.id.doorAdmin1);
            List<CabinetDoorAdmin> cabinetDoorAdminLList = cabinetDoorL.getCabinetAdminVoList();
            if (null != cabinetDoorAdminLList && !cabinetDoorAdminLList.isEmpty()) {
                stringBuffer.setLength(0);
                for (int i = 0; i < cabinetDoorAdminLList.size(); i++) {
                    CabinetDoorAdmin cabinetDoorAdmin = cabinetDoorAdminLList.get(i);
                    String userName = cabinetDoorAdmin.getUserName();
                    if (!TextUtils.isEmpty(userName)) {
                        stringBuffer.append(userName);
                        if (i != cabinetDoorAdminLList.size() - 1) {  // 不是最后一个元素
                            stringBuffer.append("\n");  // 添加分隔符
                        }
                    }
                }
                doorAdmin1.setText(stringBuffer.toString());
            } else {
                doorAdmin1.setText("");
            }

            if (cabinetDoorList.size() > 1) {
                // 右柜门
                doorRLR.setVisibility(View.VISIBLE);
                CabinetDoor cabinetDoorR = cabinetDoorList.get(1);
                // 右柜门名字
                AppCompatTextView doorName2 = holder.findViewById(R.id.doorName2);
                doorName2.setText(TextUtils.isEmpty(cabinetDoorR.getDoorName()) ? "" : cabinetDoorR.getDoorName());
                // 右柜门化学品总数
                AppCompatTextView doorHxpSize2 = holder.findViewById(R.id.doorHxpSize2);
                doorHxpSize2.setText(String.valueOf(cabinetDoorR.getStockNum()));
                // 右柜门管理员
                AppCompatTextView doorAdmin2 = holder.findViewById(R.id.doorAdmin2);
                List<CabinetDoorAdmin> cabinetDoorAdminRList = cabinetDoorR.getCabinetAdminVoList();
                if (null != cabinetDoorAdminRList && !cabinetDoorAdminRList.isEmpty()) {
                    stringBuffer.setLength(0);
                    for (int i = 0; i < cabinetDoorAdminRList.size(); i++) {
                        CabinetDoorAdmin cabinetDoorAdmin = cabinetDoorAdminRList.get(i);
                        String userName = cabinetDoorAdmin.getUserName();
                        if (!TextUtils.isEmpty(userName)) {
                            stringBuffer.append(userName);
                            if (i != cabinetDoorAdminRList.size() - 1) {  // 不是最后一个元素
                                stringBuffer.append("\n");  // 添加分隔符
                            }
                        }
                    }
                    doorAdmin2.setText(stringBuffer.toString());
                } else {
                    doorAdmin2.setText("");
                }
            } else {
                doorRLR.setVisibility(View.GONE);
            }
        } else {
            doorRLL.setVisibility(View.GONE);
            doorRLR.setVisibility(View.GONE);
        }
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.fragment_main_cabinet;
    }
}
