package com.pwt.netty.util;

import cn.hutool.core.util.RandomUtil;
import com.pwt.netty.message.*;
import com.pwt.netty.message2.*;
import com.pwt.netty.message2.BatteryInfo;
import com.pwt.netty.message2.Charger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author linyun
 * @version V1.0
 * @Title: MessageTest
 * @Description: 数据测试
 * @date 2022/11/30
 **/
//@Component
public class MessageTest2 {


    //信息汇总 运营数据
    @Scheduled(fixedRate = 1000)
    public void OperationData()
    {
        OperationData data = Globals.data.getOperationData();
        data.setDays(RandomUtil.randomInt(0,1000));
        data.setTotalExchangePower(RandomUtil.randomDouble(0, 1000));
        data.setTotalExchangeCount(RandomUtil.randomInt(0,1000));
        data.setTotalRegisterCount(RandomUtil.randomInt(0,1000));
        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("operationData", data));
    }

    //     首页 仓位
    @Scheduled(fixedRate = 1000)
    public void availableBatteryStats()
    {
        AvailableBatteryStats data = Globals.data.getAvailableBatteryStats();
        data.setAvailableBatteryCount(RandomUtil.randomInt(0,10));
        data.setRepairingCount(RandomUtil.randomInt(0,10));
        data.setChargingBatteryCount(RandomUtil.randomInt(0,10));
        data.setTotalHolderCount(RandomUtil.randomInt(0,10));
        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("availableBatteryStats", data));
    }
    //rfid连接信息
    @Scheduled(fixedRate = 1000)
    public void rfidStatus()
    {
        RfidInfo rfidInfo = Globals.data.getRfid();
        rfidInfo.setState(RandomUtil.randomInt(0, 2));
        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("rfid", rfidInfo));
    }

    //robot
    @Scheduled(fixedRate = 1000)
    public void robot()
    {
        RobotInfo data = Globals.data.getRobot();
        data.setState(RandomUtil.randomInt(0, 3));
        data.setX(RandomUtil.randomInt(0, 1000));
        data.setY(RandomUtil.randomInt(0, 1000));
        data.setZ(RandomUtil.randomInt(0, 1000));
        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("robot", data));
    }


    //vehicle
    @Scheduled(fixedRate = 1000)
    public void vehicleState()
    {
        int state = RandomUtil.randomInt(0, 6);
        Globals.data.setVehicleState(state);
        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("vehicleState", state));
    }

    @Scheduled(fixedRate = 1000)
    public void vehicleInfo()
    {
        VehicleInfo vehicleInfo = Globals.data.getVehicleInfo();
        if(vehicleInfo == null) {
            vehicleInfo = new VehicleInfo();
        }
        vehicleInfo.setLicensePlate("粤B" + RandomUtil.randomInt(10000, 99999));
        vehicleInfo.setLockState(RandomUtil.randomInt(1, 3));

        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("vehicleInfo", vehicleInfo));
    }

    // 充电机
    @Scheduled(fixedRate = 1000)
    public void charger() {
        List<Charger> chargers = Globals.data.getChargers();
//        int index = RandomUtil.randomInt(0, chargers.size()-1);
        int index = 0;
        Charger charger = chargers.get(index);
//        int commState = RandomUtil.randomInt(0, 2);
        charger.setCommunicationState(1);
//        charger.setCommunicationState(commState);
        charger.setName("#充电机" + RandomUtil.randomInt(1, 1000));
        if(charger.getCommunicationState() == 0) {
            charger.getChargeModule().forEach(module -> {
                module.setStatus(2);
                module.setTemperature(0);
                module.setOutCurrent(0);
                module.setOutVoltage(0);
            });
        } else {
            charger.getChargeModule().forEach(module -> {
                module.setStatus(RandomUtil.randomInt(0, 2));
                module.setTemperature(RandomUtil.randomInt(0, 100));
                module.setOutCurrent(RandomUtil.randomInt(0, 100));
                module.setOutVoltage(RandomUtil.randomInt(0, 100));
            });
        }

        List<Charger> chargers2  = new ArrayList<>(1);
        chargers2.add(charger);
        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("chargers", chargers2));
    }
    // 充电枪
    @Scheduled(fixedRate = 1000)
    public void chargerGun() {
        List<ChargerGun> guns = Globals.data.getChargerGuns();
        guns.forEach(gun -> {
            int state = RandomUtil.randomInt(0, 5);
            gun.getState().setState(state);
            if(state == 0) {
                gun.setVoltage(0D);
                gun.setCurrent(0D);
                gun.setDemandCurrent(0D);
                gun.setDemandVoltage(0D);
                gun.setChargeTime(0);
                gun.setSurplusTime(0);
            } else {
                gun.setVoltage(RandomUtil.randomDouble(0, 100));
                gun.setCurrent(RandomUtil.randomDouble(0, 100));
                gun.setDemandCurrent(RandomUtil.randomDouble(0, 100));
                gun.setDemandVoltage(RandomUtil.randomDouble(0, 100));
                gun.setChargeTime(RandomUtil.randomInt(0, 100));
                gun.setSurplusTime(RandomUtil.randomInt(0, 100));
                gun.setChargeMode(RandomUtil.randomInt(0, 2));
                gun.setSOC(RandomUtil.randomInt(0, 101));
            }
        });
        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("chargerGuns", guns));
    }
    //     电池
    @Scheduled(fixedRate = 1000)
    public void battery() {
        try {
            long count = Globals.batteryStateMap.values().size();
            int state;
            int holderIndex;
            // 没有数据，仅新增
            if(count ==0) {
                state = 1;
            } else if(count >= TempData.ACTUAL_HOLDER_COUNT) {
                // 数据满了，仅删除
                state = 0;
            } else {
                // 其余，随机
                state = RandomUtil.randomInt(0, 3);
            }

            // 临时: 仅对一条数据进行操作
            if(count == 0) {
                state = 1;
            } else {
                int random = RandomUtil.randomInt(0, 2);
                if(random == 0) {
                    state = 0;
                } else {
                    state = 2;
                }
            }
            holderIndex = 1;

            // 0:删除 1:新增 2:修改
            if(state == 0 ) {
                System.out.println("准备删除");
                if(count > 0) {
//                    holderIndex = RandomUtil.randomInt(1, TempData.ACTUAL_HOLDER_COUNT + 1);
//                    while(!Globals.batteryStateMap.containsKey(holderIndex)) {
//                        holderIndex = RandomUtil.randomInt(1, TempData.ACTUAL_HOLDER_COUNT + 1);
//                    }
                    BatteryStateInfo removeState = Globals.batteryStateMap.remove(holderIndex);
                    if(removeState != null) {
                        removeState.setIsDelete(true);
                        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("batteryStates", Collections.singletonList(removeState)));
                    }
                    BatteryInfo removeInfo = Globals.batteryInfoMap.remove(holderIndex);
                    if(removeInfo != null) {
                        removeInfo.setIsDelete(true);
                        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("batteryInfos", Collections.singletonList(removeInfo)));
                    }
                }
                System.out.println("删除完成");
            } else if(state == 1) {
                System.out.println("准备新增");
//                holderIndex = RandomUtil.randomInt(1, TempData.ACTUAL_HOLDER_COUNT + 1);
//                while(Globals.batteryStateMap.containsKey(holderIndex)) {
//                    holderIndex = RandomUtil.randomInt(1, TempData.ACTUAL_HOLDER_COUNT + 1);
//                }
                // 电池
                BatteryInfo batteryInfo = new BatteryInfo("1");
                batteryInfo.setHolderId(holderIndex+"");
                batteryInfo.setBatteryType("电池类型"+holderIndex);
                batteryInfo.setBatteryFactory("电池厂家" + holderIndex);
                batteryInfo.setBatteryType("电池类型" + holderIndex);
                // 为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));
                Globals.batteryInfoMap.put(holderIndex, batteryInfo);

                BatteryStateInfo batteryStateInfo = new BatteryStateInfo(holderIndex+"");
                batteryStateInfo.setHolderId(holderIndex+"");
                batteryStateInfo.setState(RandomUtil.randomInt(1, 3));
                batteryStateInfo.setBatteryVoltage(RandomUtil.randomDouble(100, 200));
                batteryStateInfo.setBatteryTemperature(RandomUtil.randomDouble(100, 200));
                batteryStateInfo.setSOC(RandomUtil.randomDouble(0, 100));
                batteryStateInfo.setCellMaxTemperature(RandomUtil.randomDouble(100, 200));
                batteryStateInfo.setCellMinTemperature(RandomUtil.randomDouble(100, 200));
                batteryStateInfo.setCellMaxVoltage(RandomUtil.randomDouble(100, 200));
                batteryStateInfo.setCellMinVoltage(RandomUtil.randomDouble(100, 200));
                batteryStateInfo.setCellMaxVoltageNum(RandomUtil.randomInt(1, 10));
                batteryStateInfo.setCellMinVoltageNum(RandomUtil.randomInt(1, 10));
                batteryStateInfo.setCellMaxTemperatureNum(RandomUtil.randomInt(1, 10));
                batteryStateInfo.setCellMinTemperatureNum(RandomUtil.randomInt(1, 10));
                batteryInfo.setBatterySn(RandomUtil.randomInt(100, 200)+"");
                Globals.batteryStateMap.put(holderIndex, batteryStateInfo);
                WebSocketUtil.pushMsgToAll(WebSocketMessage.build("batteryInfos", Collections.singletonList(batteryInfo)));
                WebSocketUtil.pushMsgToAll(WebSocketMessage.build("batteryStates", Collections.singletonList(batteryStateInfo)));
                System.out.println("新增完成");
            } else if(count > 0){
                System.out.println("准备修改");
//                holderIndex = RandomUtil.randomInt(1, TempData.ACTUAL_HOLDER_COUNT + 1);
//                while(!Globals.batteryStateMap.containsKey(holderIndex)) {
//                    holderIndex = RandomUtil.randomInt(1, TempData.ACTUAL_HOLDER_COUNT + 1);
//                }
                BatteryInfo batteryInfo = Globals.batteryInfoMap.get(holderIndex);
                int random = RandomUtil.randomInt(0, 10);
                batteryInfo.setBatteryType(random+"电池类型"+holderIndex);
                batteryInfo.setBatteryFactory(random+"电池厂家" + holderIndex);
                batteryInfo.setBatteryType(random+"电池类型" + holderIndex);
                // 为batteryInfo对象其他属性调用set方法并给一个值
                batteryInfo.setChargeCount1(random);
                batteryInfo.setChargeCount2(random);

                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));

                BatteryStateInfo batteryStateInfo = Globals.batteryStateMap.get(holderIndex);
                batteryStateInfo.setState(RandomUtil.randomInt(1, 3));
                batteryStateInfo.setBatteryVoltage(RandomUtil.randomDouble(100, 200));
                batteryStateInfo.setBatteryTemperature(RandomUtil.randomDouble(100, 200));
                batteryStateInfo.setSOC(RandomUtil.randomDouble(0, 100));
                batteryStateInfo.setCellMaxTemperature(RandomUtil.randomDouble(100, 200));
                batteryStateInfo.setCellMinTemperature(RandomUtil.randomDouble(100, 200));
                batteryStateInfo.setCellMaxVoltage(RandomUtil.randomDouble(100, 200));
                batteryStateInfo.setCellMinVoltage(RandomUtil.randomDouble(100, 200));
                batteryStateInfo.setCellMaxVoltageNum(RandomUtil.randomInt(1, 10));
                batteryStateInfo.setCellMinVoltageNum(RandomUtil.randomInt(1, 10));
                batteryStateInfo.setCellMaxTemperatureNum(RandomUtil.randomInt(1, 10));
                batteryStateInfo.setCellMinTemperatureNum(RandomUtil.randomInt(1, 10));
                batteryInfo.setBatterySn(RandomUtil.randomInt(100, 200)+"");
                WebSocketUtil.pushMsgToAll(WebSocketMessage.build("batteryInfos", Collections.singletonList(batteryInfo)));
                WebSocketUtil.pushMsgToAll(WebSocketMessage.build("batteryStates", Collections.singletonList(batteryStateInfo)));
                System.out.println("修改完成");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    @Scheduled(fixedRate = 200)
    public void alarm() {
        for (int i = 0; i < 3; i++) {
            Alarm add;
            Alarm remove;
            switch (i) {
                // 模拟站控告警
                case 0:
                    int method = RandomUtil.randomInt(0, 2);
                    if(method == 0) {
                        int code = RandomUtil.randomInt(2000, 2020);
                        add = new Alarm(code + "", 1, "1");
                        add.setLevel(0);
                        add.setDescription("站控告警" + code);
                        Globals.alarmMap.put(add, new Object());
                        Globals.alarmCount.put(add.getSourceDeviceType(), Globals.alarmCount.get(add.getSourceDeviceType())+1);
                        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("alarms", add));
                        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("alarms", Collections.singletonList(add)));
                    } else {
                        ArrayList<Alarm> alarms = new ArrayList<>(Globals.alarmMap.keySet()).stream().filter(alarm -> alarm.getSourceDeviceType() == 1).collect(Collectors.toCollection(ArrayList::new));
                        if(alarms.size() != 0) {
                            int index = RandomUtil.randomInt(0, alarms.size());
                            remove = alarms.get(index);
                            Globals.alarmMap.remove(remove);
                            remove.setIsDelete(true);
                            Globals.alarmCount.put(remove.getSourceDeviceType(), Globals.alarmCount.get(remove.getSourceDeviceType())-1);
                            WebSocketUtil.pushMsgToAll(WebSocketMessage.build("alarms", remove));
                            WebSocketUtil.pushMsgToAll(WebSocketMessage.build("alarms", Collections.singletonList(remove)));
                        }
                    }
                    break;
                case 1:
                    // 模拟充电机告警
                    method = RandomUtil.randomInt(0, 2);
                    if(method == 0) {
                        int code = RandomUtil.randomInt(3000, 3042);
                        add = new Alarm(code + "", 3, "11");
                        add.setLevel(0);
                        add.setDescription("充电机告警" + code);
                        add.setLevel(0);
                        Globals.alarmMap.put(add, new Object());
                        Globals.alarmCount.put(add.getSourceDeviceType(), Globals.alarmCount.get(add.getSourceDeviceType())+1);
                        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("alarms", add));
                        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("alarms", Collections.singletonList(add)));
                    } else {
                        ArrayList<Alarm> alarms = new ArrayList<>(Globals.alarmMap.keySet()).stream().filter(alarm -> alarm.getSourceDeviceType() == 3).collect(Collectors.toCollection(ArrayList::new));
                        if(alarms.size() != 0) {
                            int index = RandomUtil.randomInt(0, alarms.size());
                            remove = alarms.get(index);
                            Globals.alarmMap.remove(remove);
                            remove.setIsDelete(true);
                            Globals.alarmCount.put(remove.getSourceDeviceType(), Globals.alarmCount.get(remove.getSourceDeviceType())-1);
                            WebSocketUtil.pushMsgToAll(WebSocketMessage.build("alarms", remove));
                            WebSocketUtil.pushMsgToAll(WebSocketMessage.build("alarms", Collections.singletonList(remove)));
                        }
                    }
                    break;
                case 2:
                    // 模拟电池告警
                    method = RandomUtil.randomInt(0, 2);
                    if(method == 0) {
                        int code = RandomUtil.randomInt(4000, 4010);
                        add = new Alarm(code + "", 4, "2");
                        add.setLevel(0);
                        add.setDescription("电池告警" + code);
                        Globals.alarmMap.put(add, new Object());
                        Globals.alarmCount.put(add.getSourceDeviceType(), Globals.alarmCount.get(add.getSourceDeviceType())+1);
                        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("alarms", add));
                        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("alarms", Collections.singletonList(add)));
                    } else {
                        ArrayList<Alarm> alarms = new ArrayList<>(Globals.alarmMap.keySet()).stream().filter(alarm -> alarm.getSourceDeviceType() == 4).collect(Collectors.toCollection(ArrayList::new));
                        if(alarms.size() != 0) {
                            int index = RandomUtil.randomInt(0, alarms.size());
                            remove = alarms.get(index);
                            Globals.alarmMap.remove(remove);
                            remove.setIsDelete(true);
                            Globals.alarmCount.put(remove.getSourceDeviceType(), Globals.alarmCount.get(remove.getSourceDeviceType())-1);
                            WebSocketUtil.pushMsgToAll(WebSocketMessage.build("alarms", remove));
                            WebSocketUtil.pushMsgToAll(WebSocketMessage.build("alarms", Collections.singletonList(remove)));
                        }
                    }
                    break;
            }
            ArrayList<AlarmStatistics> alarmStats = new ArrayList<>();
            Globals.alarmCount.forEach((k, v) -> {
                AlarmStatistics alarmStatistics = new AlarmStatistics();
                alarmStatistics.setSourceDeviceType(k);
                alarmStatistics.setCount(v);
                alarmStats.add(alarmStatistics);
            });
            WebSocketUtil.pushMsgToAll(WebSocketMessage.build("alarmStats", alarmStats));
        }
    }

    //    @Scheduled(fixedRate = 1000)
    @Scheduled(fixedRate = 2000)
    public void assist() {

        Assist assist = new Assist();
        assist.setText("辅助信息" + RandomUtil.randomInt(1, 100));
        assist.setMedia("https://img.tukuppt.com/newpreview_music/09/00/06/5c88fa28f2e4662193.mp3");
        int index = RandomUtil.randomInt(1, 17);
        assist.setMedia("http://localhost:8080/static/"+index +".mp3");

        WebSocketUtil.pushMsgToAll(WebSocketMessage.build("assist", assist));
    }

}

