package xn.hxp.ui.plan.locker;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.LogUtils;
import xn.hxp.R;
import xn.hxp.databinding.FragmentLockerBinding;
import xn.hxp.ui.plan.FragmentCallBack;
import xn.hxp.ui.plan.room.RoomTool;
import xn.hxp.ui.plan.room.bean.input_add.InventoryItemBean;
import xn.hxp.ui.plan.room.bean.locker.HxpCabinetDoorVo;
import xn.hxp.ui.plan.room.bean.locker.HxpCabinetVo;
import xn.hxp.ui.plan.room.bean.locker.CabinetLayerSelect;
import xn.hxp.ui.plan.room.dao.HxpCabinetDAO;
import xn.hxp.ui.plan.room.dao.HxpDoorDAO;
import xn.hxp.ui.plan.room.dao.HxpInventoryDAO;
import xn.hxp.ui.plan.room.dao.HxpLayerDAO;

import java.util.LinkedList;
import java.util.List;

public class LockerFragment extends Fragment {
    private FragmentLockerBinding binding;

    // 上柜门柜层
    private List<DrawerUiBean> drawerUiUpList = new LinkedList<>();
    // 下柜门柜层
    private List<DrawerUiBean> drawerUiDownList = new LinkedList<>();
    private FragmentCallBack fragmentCallBack;

    private List<HxpCabinetDoorVo> doorVoList;

    // 柜子
    private long cabinetId;
    private HxpCabinetDAO hxpCabinetDAO;
    private HxpDoorDAO hxpDoorDAO;
    private HxpLayerDAO hxpLayerDAO;
    private HxpCabinetVo hxpCabinetVo;
    private HxpInventoryDAO hxpInventoryDAO;

    public LockerFragment(long cabinetId, FragmentCallBack fragmentCallBack) {
        this.cabinetId = cabinetId;
        this.fragmentCallBack = fragmentCallBack;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentLockerBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        updateUi();
    }

    @Override
    public void onPause() {
        super.onPause();
        updateUi();
    }

    private void updateUi() {
        drawerUiUpList.clear();
        drawerUiDownList.clear();
        binding.lockerUpDrawerRG.clearCheck();
        binding.lockerDownDrawerRG.clearCheck();
        // 上柜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));

        hxpCabinetDAO = RoomTool.getInstance().hxpCabinetDAO();
        hxpDoorDAO = RoomTool.getInstance().hxpDoorDAO();
        hxpLayerDAO = RoomTool.getInstance().hxpLayerDAO();
        hxpInventoryDAO = RoomTool.getInstance().hxpInventoryDAO();
        hxpCabinetVo = hxpCabinetDAO.getById(cabinetId);
        // 所有柜门
        doorVoList = hxpDoorDAO.getByCabinetId(cabinetId);
        // 所有待存储清单
        List<InventoryItemBean> inventoryItemBeanList = hxpInventoryDAO.getAll();

        for (int i = 0; i < drawerUiUpList.size(); i++) {
            drawerUiUpList.get(i).getRadioButton().setOnClickListener(onClickDoorUpListener);
        }
        for (int i = 0; i < drawerUiDownList.size(); i++) {
            drawerUiDownList.get(i).getRadioButton().setOnClickListener(onClickDoorDownListener);
        }
        if (null != doorVoList) {
            // 柜子名称
            String cabinetName = hxpCabinetVo.getCabinetName();
            // 库存总数
            int stockCount = 0;
            for (int i = 0; i < doorVoList.size(); i++) {
                HxpCabinetDoorVo hxpCabinetDoorVo = doorVoList.get(i);
                stockCount = stockCount + hxpCabinetDoorVo.getStockNum();
            }
            binding.lockerNameTV.setText(TextUtils.isEmpty(cabinetName) ? "" : cabinetName + "(" + stockCount + ")");
            // 选中的柜层信息
            CabinetLayerSelect cabinetLayerSelect = hxpLayerDAO.getCabinetChanged(cabinetId);
            // 一个柜门
            if (doorVoList.size() == 1) {
                HxpCabinetDoorVo hxpCabinetDoorVo = doorVoList.get(0);
                binding.cabinetRL.setBackgroundResource(R.mipmap.locker_one);
                // 柜门名称
                String doorName = hxpCabinetDoorVo.getDoorName();
                binding.upDoorNameTV.setText(TextUtils.isEmpty(doorName) ? "" : doorName);
                // 选中的柜层
                int layer = -1;
                if (null != cabinetLayerSelect) {
                    if (hxpCabinetDoorVo.getDoorId() == cabinetLayerSelect.getDoorId()) {
                        layer = cabinetLayerSelect.getLayer();
                    }
                }
                // 层数
                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);
                        if (layer > 0 && (i + 1) == layer) {
                            drawerUiBean.getRadioButton().setChecked(true);
                        }
                        if (null != inventoryItemBeanList && !inventoryItemBeanList.isEmpty()) {
                            boolean isHasHxp = false;
                            for (int j = 0; j < inventoryItemBeanList.size(); j++) {
                                InventoryItemBean inventoryItemBean = inventoryItemBeanList.get(j);
                                if (hxpCabinetDoorVo.getDoorId() == inventoryItemBean.getDoorId()) {
                                    if (inventoryItemBean.getLayer() == (i + 1)) {
                                        isHasHxp = true;
                                        break;
                                    }
                                }
                            }
                            if (isHasHxp) {
                                drawerUiBean.getTextView().setTextColor(Color.parseColor("#FFFF8C00"));
                            } else {
                                drawerUiBean.getTextView().setTextColor(Color.WHITE);
                            }
                        }

                    }
                }
            }
            // 两个柜门
            else if (doorVoList.size() > 1) {
                // 上柜门
                HxpCabinetDoorVo hxpCabinetDoorUpVo = doorVoList.get(0);
                binding.cabinetRL.setBackgroundResource(R.mipmap.locker);
                String doorName = hxpCabinetDoorUpVo.getDoorName();
                binding.upDoorNameTV.setText(TextUtils.isEmpty(doorName) ? "" : doorName);
                int layerUp = -1;
                if (null != cabinetLayerSelect) {
                    if (hxpCabinetDoorUpVo.getDoorId() == cabinetLayerSelect.getDoorId()) {
                        layerUp = cabinetLayerSelect.getLayer();
                    }
                }
                // 层数
                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);
                        if (layerUp > 0 && (i + 1) == layerUp) {
                            drawerUiBean.getRadioButton().setChecked(true);
                        }
                    }
                    if (null != inventoryItemBeanList && !inventoryItemBeanList.isEmpty()) {
                        boolean isHasHxp = false;
                        for (int j = 0; j < inventoryItemBeanList.size(); j++) {
                            InventoryItemBean inventoryItemBean = inventoryItemBeanList.get(j);
                            if (hxpCabinetDoorUpVo.getDoorId() == inventoryItemBean.getDoorId()) {
                                if (inventoryItemBean.getLayer() == (i + 1)) {
                                    isHasHxp = true;
                                    break;
                                }
                            }
                        }
                        if (isHasHxp) {
                            drawerUiBean.getTextView().setTextColor(Color.parseColor("#FFFF8C00"));
                        } else {
                            drawerUiBean.getTextView().setTextColor(Color.WHITE);
                        }
                    }
                }

                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 layerDown = -1;
                if (null != cabinetLayerSelect) {
                    if (hxpCabinetDoorDownVo.getDoorId() == cabinetLayerSelect.getDoorId()) {
                        layerDown = cabinetLayerSelect.getLayer();
                    }
                }
                // 层数
                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);
                        if (layerDown > 0 && (i + 1) == layerDown) {
                            drawerUiBean.getRadioButton().setChecked(true);
                        }
                    }
                    if (null != inventoryItemBeanList && !inventoryItemBeanList.isEmpty()) {
                        boolean isHasHxp = false;
                        for (int j = 0; j < inventoryItemBeanList.size(); j++) {
                            InventoryItemBean inventoryItemBean = inventoryItemBeanList.get(j);
                            if (hxpCabinetDoorDownVo.getDoorId() == inventoryItemBean.getDoorId()) {
                                if (inventoryItemBean.getLayer() == (i + 1)) {
                                    isHasHxp = true;
                                    break;
                                }
                            }
                        }
                        if (isHasHxp) {
                            drawerUiBean.getTextView().setTextColor(Color.parseColor("#FFFF8C00"));
                        } else {
                            drawerUiBean.getTextView().setTextColor(Color.WHITE);
                        }
                    }
                }

            }
        }

    }

    private final View.OnClickListener onClickDoorUpListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            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;
            }
            fragmentCallBack.onSelected(new CabinetLayerSelect(hxpCabinetVo.getCabinetId(), doorVoList.get(0).getDoorId(), layer));
        }
    };
    private final View.OnClickListener onClickDoorDownListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            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;
            }
            fragmentCallBack.onSelected(new CabinetLayerSelect(hxpCabinetVo.getCabinetId(), doorVoList.get(1).getDoorId(), layer));
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}
