package com.pwt.netty.message2;

import cn.hutool.core.util.RandomUtil;
import com.pwt.common.enums.ChargerFault;
import lombok.Data;

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


@Data
public class TempData {

    public static final int MAX_HOLDER_COUNT = 8;
    public static final int ACTUAL_HOLDER_COUNT = 7;

    public static final int CHARGER_COUNT = 6;

    // 最大充电模块数，决定给UI返回的数量
    public static final int MAX_CHARGER_MODULE_COUNT = 10;

    /**
     * 换电工作模式 1 手动 2 自动
     */
    private int exchangeWorkMode = 1;

    private OperationData operationData;

    /**
     * 按来源设备类型统计的告警数
     */
    private List<AlarmStatistics> alarmStats;

    private RobotInfo robot = new RobotInfo();

    private RfidInfo rfid = new RfidInfo();

    private List<BatteryHolder> batteryHolders;

    private List<Charger> chargers = new ArrayList<>();

    private List<ChargerGun> chargerGuns = new ArrayList<>();

    private List<BatteryInfo> batteryInfos = new ArrayList<>();
    private List<BatteryStateInfo> batteryStates = new ArrayList<>();

    private ExchangeProcess exchangeProcess = new ExchangeProcess();

    private List<GunAlarmTemplate> gunAlarms = new ArrayList<>();

    /**
     * 车辆状态. 0 空闲 1 进站 2 已到位 3 换电中 4 换电完成 5 出站
     */
    private int vehicleState = 0;

    /**
     * 当前车辆信息, 仅进站时有值
     */
    private VehicleInfo vehicleInfo ;

    private AvailableBatteryStats availableBatteryStats;

    private List<Alarm> alarms;


    public TempData() {

        this.initAlarmMap();

        // 运营数据和告警统计
        OperationData operationData = new OperationData();
        operationData.setDays(1);
        operationData.setTotalExchangeCount(2);
        operationData.setTotalExchangePower(3);
        operationData.setTotalRegisterCount(4);
        this.operationData = operationData;

        this.alarmStats = new ArrayList<>();
        Globals.alarmCount.forEach((k, v) -> {
            AlarmStatistics alarmStatistics = new AlarmStatistics();
            alarmStatistics.setSourceDeviceType(k);
            alarmStatistics.setCount(v);
            this.alarmStats.add(alarmStatistics);
        });

        // 当前告警
        alarms = new ArrayList<>();
//        Globals.alarmMap.forEach((k, v) -> {
//            k.set(v.toString());
//            alarms.add(k);
//        });

        // 充电机和枪
        for (int i = 1; i <= CHARGER_COUNT; i++) {
            Charger charger = new Charger();
            charger.setId(i + "");
            charger.setName("#" + i);
            charger.setIndex(i);
            int gunCount;
            // 第一个为1拖2，最后一个为应急充电
            if(i ==1 || i == CHARGER_COUNT)  {
                gunCount = 4;
            } else {
                gunCount = 2;
            }
            for (int j = 1 ; j <= gunCount; j++) {
                ChargerGun  gun = new ChargerGun();
                gun.setId(charger.getId() + j);
                gun.setName("#" + j);
                gun.setIndex(j);
                gun.setState(new ChargerStateInfo());
                gun.setChargerId(charger.getId());
                if(i == 1 ) {
                    if(j <= 2) {
                        gun.setHolderId(1+"");
                    } else {
                        gun.setHolderId(3+"");
                    }
                } else if(i == 2) {
                    gun.setHolderId(2+"");
                } else {
                    if(i == CHARGER_COUNT) {
                        if(j <= 2) {
                            gun.setHolderId((i+1)+"");
                        } else {
                            // 应急充电
                            gun.setHolderId(null);
                        }
                    } else {
                        gun.setHolderId((i+1)+"");
                    }

                }
                chargerGuns.add(gun);
            }
            List<ChargerModule> chargeModules = new ArrayList<>();
            // 充电枪模块
            for (int j = 1; j <= MAX_CHARGER_MODULE_COUNT ; j++) {
                ChargerModule module = new ChargerModule();
                module.setIndex(j);
                chargeModules.add(module);
            }
            charger.setChargeModule(chargeModules);
            chargers.add(charger);
        }

        // 电池仓位
        batteryHolders = new ArrayList<>();
        for (int i = 1; i <= MAX_HOLDER_COUNT; i++) {
            BatteryHolder batteryHolder = new BatteryHolder();
            batteryHolder.setId(i+ "");
            batteryHolder.setName("#"+i);
            batteryHolder.setIndex(i);
            if(i > ACTUAL_HOLDER_COUNT) {
                batteryHolder.setEnable(false);
            }
            batteryHolders.add(batteryHolder);
        }
        // 电池
        BatteryInfo batteryInfo = new BatteryInfo("1");
        batteryInfo.setId("1");
        batteryInfo.setHolderId("1");
        batteryInfo.setBatteryType("电池类型");
        batteryInfo.setBatteryFactory("电池厂家");
        batteryInfo.setBatteryType("电池类型");
        // 为batteryInfo对象其他属性调用set方法并给一个值
        batteryInfo.setChargeCount1(0);
        batteryInfo.setChargeCount2(0);
        batteryInfo.setBatteryTotalVoltage(RandomUtil.randomDouble(100, 200));
        batteryInfo.setMaxChargeVoltage(RandomUtil.randomDouble(100, 200));
        batteryInfo.setRatedCapacity(RandomUtil.randomDouble(100, 200));
        batteryInfo.setRatedVoltage(RandomUtil.randomDouble(100, 200));
        batteryInfo.setTotalCapacity(RandomUtil.randomDouble(100, 200));
        batteryInfo.setResidualEnergy(RandomUtil.randomDouble(100, 200));
        batteryInfos.add(batteryInfo);
        Globals.batteryInfoMap.put(1, batteryInfo);

        BatteryStateInfo batteryStateInfo = new BatteryStateInfo("1");
        batteryStateInfo.setHolderId("1");
        batteryStates.add(batteryStateInfo);
        Globals.batteryStateMap.put(1, batteryStateInfo);

        availableBatteryStats = new AvailableBatteryStats(ACTUAL_HOLDER_COUNT);
    }

    private void initAlarmMap() {

        for (ChargerFault value : ChargerFault.values()) {
            gunAlarms.add(new GunAlarmTemplate(value.getErrCode()+"", value.getErrMsg()));
        }

        // 初始化数量
        for (int i = 1 ; i <=5; i++) {
            Globals.alarmCount.put(i, 0);
        }
    }
}
