package com.example.chemical.ui.plan.change;

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

import com.blankj.utilcode.util.LogUtils;
import com.example.chemical.R;
import com.example.chemical.databinding.IncludeCabinetBinding;
import com.example.chemical.ui.plan.FragmentCallBack;
import com.example.chemical.ui.plan.locker.DrawerUiBean;
import com.example.chemical.ui.plan.room.bean.locker.CabinetLayerSelect;
import com.example.chemical.ui.plan.room.bean.locker.HxpCabinetDoorVo;
import com.example.chemical.ui.plan.room.bean.locker.HxpCabinetVo;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class ChangeCabinetHelp {

    private Map<Long, List<DrawerUiBean>> drawerUiMap = new TreeMap<>();
    private IncludeCabinetBinding binding;
    private FragmentCallBack fragmentCallBack;

    public ChangeCabinetHelp(IncludeCabinetBinding binding, HxpCabinetVo hxpCabinetVo, FragmentCallBack fragmentCallBack) {
        this.binding = binding;
        this.fragmentCallBack = fragmentCallBack;
        List<DrawerUiBean> drawerUiUpList = new LinkedList<>();
        List<DrawerUiBean> drawerUiDownList = new LinkedList<>();
        // 上柜view控制
        drawerUiUpList.add(new DrawerUiBean(binding.lockerUpDrawer1RB, binding.lockerUpDrawer1TV, false));
        drawerUiUpList.add(new DrawerUiBean(binding.lockerUpDrawer2RB, binding.lockerUpDrawer2TV, false));
        drawerUiUpList.add(new DrawerUiBean(binding.lockerUpDrawer3RB, binding.lockerUpDrawer3TV, false));
        drawerUiUpList.add(new DrawerUiBean(binding.lockerUpDrawer4RB, binding.lockerUpDrawer4TV, false));
        drawerUiUpList.add(new DrawerUiBean(binding.lockerUpDrawer5RB, binding.lockerUpDrawer5TV, false));
        drawerUiUpList.add(new DrawerUiBean(binding.lockerUpDrawer6RB, binding.lockerUpDrawer6TV, false));
        drawerUiUpList.add(new DrawerUiBean(binding.lockerUpDrawer7RB, binding.lockerUpDrawer7TV, false));
        drawerUiUpList.add(new DrawerUiBean(binding.lockerUpDrawer8RB, binding.lockerUpDrawer8TV, false));

        // 下柜view控制
        drawerUiDownList.add(new DrawerUiBean(binding.lockerDownDrawer1RB, binding.lockerDownDrawer1TV, false));
        drawerUiDownList.add(new DrawerUiBean(binding.lockerDownDrawer2RB, binding.lockerDownDrawer2TV, false));
        drawerUiDownList.add(new DrawerUiBean(binding.lockerDownDrawer3RB, binding.lockerDownDrawer3TV, false));
        drawerUiDownList.add(new DrawerUiBean(binding.lockerDownDrawer4RB, binding.lockerDownDrawer4TV, false));

        // 绘制柜层
        draw(hxpCabinetVo, drawerUiUpList, drawerUiDownList);

        drawerUiMap.put(hxpCabinetVo.getCabinetId(), drawerUiUpList);
        drawerUiMap.put(hxpCabinetVo.getCabinetId(), drawerUiDownList);
    }

    private void draw(HxpCabinetVo hxpCabinetVo, List<DrawerUiBean> drawerUiUpList, List<DrawerUiBean> drawerUiDownList) {
        LogUtils.json(hxpCabinetVo);
        for (int i = 0; i < drawerUiUpList.size(); i++) {
            drawerUiUpList.get(i).getRadioButton().setOnClickListener(createClickDoorUpListener(hxpCabinetVo));
        }
        for (int i = 0; i < drawerUiDownList.size(); i++) {
            drawerUiDownList.get(i).getRadioButton().setOnClickListener(createClickDoorDownListener(hxpCabinetVo));
        }
        // 所有柜门
        List<HxpCabinetDoorVo> doorVoList = hxpCabinetVo.getCabinetDoorVoList();

        // 柜子名称
        String cabinetName = hxpCabinetVo.getCabinetName();
        binding.lockerNameTV.setText(TextUtils.isEmpty(cabinetName) ? "" : cabinetName);

        // 一个柜门
        if (doorVoList.size() == 1) {
            binding.cabinetRL.setBackgroundResource(R.mipmap.locker_one);
            HxpCabinetDoorVo hxpCabinetDoorVo = doorVoList.get(0);
            // 柜门名称
            String doorName = hxpCabinetDoorVo.getDoorName();
            binding.upDoorNameTV.setText(TextUtils.isEmpty(doorName) ? "" : doorName);
            // 层数
            int doorLayers = hxpCabinetDoorVo.getDoorLayers();
            for (int i = 0; i < 8; i++) {
                DrawerUiBean drawerUiBean = drawerUiUpList.get(i);
                drawerUiBean.setVisible(i < doorLayers);
            }

            for (int i = 0; i < drawerUiUpList.size(); i++) {
                DrawerUiBean drawerUiBean = drawerUiUpList.get(i);
                if (drawerUiBean.isVisible()) {
                    drawerUiBean.getRadioButton().setVisibility(View.VISIBLE);
                    drawerUiBean.getTextView().setVisibility(View.VISIBLE);
                }
            }
        }
        // 两个柜门
        else if (doorVoList.size() > 1) {
            binding.cabinetRL.setBackgroundResource(R.mipmap.locker);
            // 上柜门
            HxpCabinetDoorVo hxpCabinetDoorUpVo = doorVoList.get(0);
            String doorName = hxpCabinetDoorUpVo.getDoorName();
            binding.upDoorNameTV.setText(TextUtils.isEmpty(doorName) ? "" : doorName);
            // 层数
            int doorUpLayers = hxpCabinetDoorUpVo.getDoorLayers();
            for (int i = 0; i < 4; i++) {
                DrawerUiBean drawerUiBean = drawerUiUpList.get(i);
                drawerUiBean.setVisible(i < doorUpLayers);
            }

            for (int i = 0; i < drawerUiUpList.size(); i++) {
                DrawerUiBean drawerUiBean = drawerUiUpList.get(i);
                if (drawerUiBean.isVisible()) {
                    drawerUiBean.getRadioButton().setVisibility(View.VISIBLE);
                    drawerUiBean.getTextView().setVisibility(View.VISIBLE);
                }
            }

            binding.lockerDownDrawerLL.setVisibility(View.VISIBLE);
            binding.lockerDownDrawerRG.setVisibility(View.VISIBLE);

            // 下柜门
            HxpCabinetDoorVo hxpCabinetDoorDownVo = doorVoList.get(1);
            String doorDownName = hxpCabinetDoorDownVo.getDoorName();
            binding.downDoorNameTV.setText(TextUtils.isEmpty(doorDownName) ? "" : doorDownName);
            // 层数
            int doorDownLayers = hxpCabinetDoorDownVo.getDoorLayers();
            for (int i = 0; i < 4; i++) {
                DrawerUiBean drawerUiBean = drawerUiDownList.get(i);
                drawerUiBean.setVisible(i < doorDownLayers);
            }

            for (int i = 0; i < drawerUiDownList.size(); i++) {
                DrawerUiBean drawerUiBean = drawerUiDownList.get(i);
                if (drawerUiBean.isVisible()) {
                    drawerUiBean.getRadioButton().setVisibility(View.VISIBLE);
                    drawerUiBean.getTextView().setVisibility(View.VISIBLE);
                }
            }
        }
    }

    private View.OnClickListener createClickDoorUpListener(HxpCabinetVo hxpCabinetVo) {
        return v -> {
            if (-1 != binding.lockerDownDrawerRG.getCheckedRadioButtonId()) {
                binding.lockerDownDrawerRG.clearCheck();
            }
            int layer = 0;
            if (binding.lockerUpDrawer1RB.getId() == v.getId()) {
                layer = 1;
            } else if (binding.lockerUpDrawer2RB.getId() == v.getId()) {
                layer = 2;
            } else if (binding.lockerUpDrawer3RB.getId() == v.getId()) {
                layer = 3;
            } else if (binding.lockerUpDrawer4RB.getId() == v.getId()) {
                layer = 4;
            } else if (binding.lockerUpDrawer5RB.getId() == v.getId()) {
                layer = 5;
            } else if (binding.lockerUpDrawer6RB.getId() == v.getId()) {
                layer = 6;
            } else if (binding.lockerUpDrawer7RB.getId() == v.getId()) {
                layer = 7;
            } else if (binding.lockerUpDrawer8RB.getId() == v.getId()) {
                layer = 8;
            }
            LogUtils.d("上柜", layer + "层");
            fragmentCallBack.onSelected(new CabinetLayerSelect(hxpCabinetVo.getCabinetId(), hxpCabinetVo.getCabinetDoorVoList().get(0).getDoorId(), layer));
        };
    }

    private View.OnClickListener createClickDoorDownListener(HxpCabinetVo hxpCabinetVo) {
        return v -> {
            if (-1 != binding.lockerUpDrawerRG.getCheckedRadioButtonId()) {
                binding.lockerUpDrawerRG.clearCheck();
            }
            int layer = 0;
            if (binding.lockerDownDrawer1RB.getId() == v.getId()) {
                layer = 1;
            } else if (binding.lockerDownDrawer2RB.getId() == v.getId()) {
                layer = 2;
            } else if (binding.lockerDownDrawer3RB.getId() == v.getId()) {
                layer = 3;
            } else if (binding.lockerDownDrawer4RB.getId() == v.getId()) {
                layer = 4;
            }
            LogUtils.d("下柜", layer + "层");
            fragmentCallBack.onSelected(new CabinetLayerSelect(hxpCabinetVo.getCabinetId(), hxpCabinetVo.getCabinetDoorVoList().get(1).getDoorId(), layer));
        };
    }

    public void recycle() {
        drawerUiMap.clear();
        binding = null;
    }

}
