package com.usky.fire.service.mqtt.alarm;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.core.util.JsonUtils;
import com.usky.fire.domain.*;
import com.usky.fire.service.*;
import com.usky.fire.service.mqtt.MqttStrategy;
import com.usky.fire.service.util.OnlineMethod;
import com.usky.fire.service.vo.MqttBaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Timer;

/**
 * @author yq
 * @date 2022/3/4 15:07
 */
@Service("alarm")
public class Alarm implements MqttStrategy {

    @Autowired
    private AlarmFireService alarmFireService;

    @Autowired
    private BaseDeviceService baseDeviceService;

    @Autowired
    private BaseDeviceAttributeService baseDeviceAttributeService;

    @Autowired
    private BaseCompanyService baseCompanyService;

    @Autowired
    private AlarmSprayWaterService alarmSprayWaterService;

    @Autowired
    private AlarmHydrantWaterService alarmHydrantWaterService;

    @Autowired
    private AlarmSmokeControlService alarmSmokeControlService;

    @Autowired
    private AlarmElectricalFireService alarmElectricalFireService;

    @Autowired
    private AlarmVideoService alarmVideoService;

    @Override
    public String disposeMessage(MqttBaseVO mqttBaseVO) {
        Map map = JsonUtils.fromJson(mqttBaseVO.getData().toString(), Map.class);
        List<Map<String, Object>> alarams = (List) JSONObject.parseArray(JSONObject.toJSONString(map.get("alarams")));
        List<Map<String, Object>> dp = (List) JSONObject.parseArray(JSONObject.toJSONString(alarams.get(0).get("dp")));
        LambdaQueryWrapper<BaseDevice> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.select(BaseDevice::getCompanyId, BaseDevice::getDeviceType)
                .eq(BaseDevice::getDeviceCode, map.get("devId").toString())
                .eq(BaseDevice::getEnable, 1);
        List<BaseDevice> list = baseDeviceService.list(queryWrapper1);
        List<BaseDeviceAttribute> list1 = new ArrayList<>();
        List<BaseCompany> list2 = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            LambdaQueryWrapper<BaseDeviceAttribute> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.select(BaseDeviceAttribute::getSystemType)
                    .eq(BaseDeviceAttribute::getDeviceCode, map.get("devId").toString());
            if (list.get(0).getDeviceType() == 6) {
                if (!dp.get(0).get("property").toString().equals("离线")) {
                    queryWrapper.eq(BaseDeviceAttribute::getPortCode, dp.get(0).get("serial"));
                }
            }
            list1 = baseDeviceAttributeService.list(queryWrapper);

            LambdaQueryWrapper<BaseCompany> queryWrapper2 = Wrappers.lambdaQuery();
            queryWrapper2.select(BaseCompany::getCompanyName).
                    eq(BaseCompany::getCompanyId, list.get(0).getCompanyId());
            list2 = baseCompanyService.list(queryWrapper2);
        }
        if (CollectionUtils.isNotEmpty(list) && CollectionUtils.isNotEmpty(list1)) {
            switch (list1.get(0).getSystemType()) {
                case 1:
                    AlarmFire alarmFire = new AlarmFire();
                    String deviceName = map.get("deviceName").toString();
                    alarmFire.setDeviceCode(map.get("devId").toString());
                    alarmFire.setDeviceName(map.get("deviceName").toString());
                    alarmFire.setAlarmType(OnlineMethod.getAlarmType(dp.get(0).get("property").toString(), list.get(0).getDeviceType()));
                    String timestamp1 = map.get("timeStamp").toString();
                    if (StringUtils.isNotBlank(timestamp1)) {
                        long timestamp = Long.valueOf(timestamp1).longValue();
                        LocalDateTime time2 = LocalDateTime.ofEpochSecond(timestamp, 0, ZoneOffset.ofHours(8));
                        alarmFire.setAlarmTime(time2);
                    }
//                    alarmFire.setAddress();
                    alarmFire.setAlarmStatus(dp.get(0).get("type").toString());
                    alarmFire.setSystemType(1);
                    if (dp.get(0).get("value").toString().indexOf("端口号") != -1) {
                        String[] values = dp.get(0).get("value").toString().split("，");
                        int index = 0;
                        boolean pd = false;
                        for (int i = 0; i < values.length; i++) {
                            if (values[i].indexOf("端口号") != -1) {
                                index = i;
                                pd = true;
                            }
                        }
                        if (pd == true) {
                            String portNum = values[index].split("：")[1];
                            alarmFire.setPortNum(portNum);
                        }
                    }
                    alarmFire.setCompanyId(list.get(0).getCompanyId());
                    if (CollectionUtils.isNotEmpty(list2)) {
                        alarmFire.setCompanyName(list2.get(0).getCompanyName());
                    }
                    alarmFire.setAttributeValue1(dp.get(0).get("type").toString());
                    alarmFire.setAttributeValue2(dp.get(0).get("property").toString());
                    alarmFire.setAttributeValue3(dp.get(0).get("address").toString());
                    alarmFire.setAttributeValue4(dp.get(0).get("value").toString());
                    alarmFire.setPointType(0);
                    if (dp.get(0).get("value").toString().indexOf("主机号") != -1) {
                        String[] values = dp.get(0).get("value").toString().split("，");
                        int index = 0;
                        boolean pd = false;
                        for (int i = 0; i < values.length; i++) {
                            if (values[i].indexOf("主机号") != -1) {
                                index = i;
                                pd = true;
                            }
                        }
                        if (pd == true) {
                            String hostNum = values[index].split("：")[1];
                            alarmFire.setHostNum(Integer.parseInt(hostNum));
                        }
                    }

                    if (dp.get(0).get("value").toString().indexOf("回路号") != -1) {
                        String[] values = dp.get(0).get("value").toString().split("，");
                        int index = 0;
                        boolean pd = false;
                        for (int i = 0; i < values.length; i++) {
                            if (values[i].indexOf("回路号") != -1) {
                                index = i;
                                pd = true;
                            }
                        }
                        if (pd == true) {
                            String loopNum = values[index].split("：")[1];
                            alarmFire.setLoopNum(Integer.parseInt(loopNum));
                        }
                    }

                    if (dp.get(0).get("value").toString().indexOf("点位号") != -1) {
                        String[] values = dp.get(0).get("value").toString().split("，");
                        int index = 0;
                        boolean pd = false;
                        for (int i = 0; i < values.length; i++) {
                            if (values[i].indexOf("点位号") != -1) {
                                index = i;
                                pd = true;
                            }
                        }
                        if (pd == true) {
                            String ptNum = values[index].split("：")[1];
                            alarmFire.setPtNum(Integer.parseInt(ptNum));
                        }
                    }
                    alarmFire.setHandlingStatus(0);
                    alarmFireService.save(alarmFire);
                    break;
                case 2:
                    AlarmSprayWater alarmSprayWater = new AlarmSprayWater();
                    alarmSprayWater.setDeviceCode(map.get("devId").toString());
                    alarmSprayWater.setDeviceName(map.get("deviceName").toString());
                    alarmSprayWater.setAlarmType(OnlineMethod.getAlarmType(dp.get(0).get("property").toString(), list.get(0).getDeviceType()));
                    if (StringUtils.isNotBlank(map.get("timeStamp").toString())) {
                        LocalDateTime time2 = LocalDateTime.ofEpochSecond(Long.valueOf(map.get("timeStamp").toString()).longValue(), 0, ZoneOffset.ofHours(8));
                        alarmSprayWater.setAlarmTime(time2);
                    }
//                    alarmSprayWater.setAddress();
                    alarmSprayWater.setAlarmStatus(dp.get(0).get("property").toString());
                    alarmSprayWater.setSystemType(2);
                    alarmSprayWater.setPortNum("0");
                    alarmSprayWater.setCompanyId(list.get(0).getCompanyId());
                    if (CollectionUtils.isNotEmpty(list2)) {
                        alarmSprayWater.setCompanyName(list2.get(0).getCompanyName());
                    }
                    alarmSprayWater.setAttributeValue4(dp.get(0).get("value").toString());
                    alarmSprayWater.setHandlingStatus(0);
                    alarmSprayWaterService.save(alarmSprayWater);
                    break;
                case 3:
                    AlarmHydrantWater alarmHydrantWater = new AlarmHydrantWater();
                    alarmHydrantWater.setDeviceCode(map.get("devId").toString());
                    alarmHydrantWater.setDeviceName(map.get("deviceName").toString());
                    alarmHydrantWater.setAlarmType(OnlineMethod.getAlarmType(dp.get(0).get("property").toString(), list.get(0).getDeviceType()));
                    if (StringUtils.isNotBlank(map.get("timeStamp").toString())) {
                        LocalDateTime time2 = LocalDateTime.ofEpochSecond(Long.valueOf(map.get("timeStamp").toString()).longValue(), 0, ZoneOffset.ofHours(8));
                        alarmHydrantWater.setAlarmTime(time2);
                    }
//                    alarmHydrantWater.setAddress();
                    alarmHydrantWater.setAlarmStatus(dp.get(0).get("property").toString());
                    alarmHydrantWater.setSystemType(3);
                    alarmHydrantWater.setPortNum("0");
                    alarmHydrantWater.setCompanyId(list.get(0).getCompanyId());
                    if (CollectionUtils.isNotEmpty(list2)) {
                        alarmHydrantWater.setCompanyName(list2.get(0).getCompanyName());
                    }
                    alarmHydrantWater.setAttributeValue4(dp.get(0).get("value").toString());
                    alarmHydrantWater.setHandlingStatus(0);
                    alarmHydrantWaterService.save(alarmHydrantWater);
                    break;
                case 4:
                    AlarmSmokeControl alarmSmokeControl = new AlarmSmokeControl();
                    alarmSmokeControl.setDeviceCode(map.get("devId").toString());
                    alarmSmokeControl.setDeviceName(map.get("deviceName").toString());
                    alarmSmokeControl.setAlarmType(OnlineMethod.getAlarmType(dp.get(0).get("property").toString(), list.get(0).getDeviceType()));
                    if (StringUtils.isNotBlank(map.get("timeStamp").toString())) {
                        LocalDateTime time2 = LocalDateTime.ofEpochSecond(Long.valueOf(map.get("timeStamp").toString()).longValue(), 0, ZoneOffset.ofHours(8));
                        alarmSmokeControl.setAlarmTime(time2);
                    }
//                    alarmSmokeControl.setAddress();
                    alarmSmokeControl.setAlarmStatus(dp.get(0).get("property").toString());
                    alarmSmokeControl.setSystemType(4);
                    alarmSmokeControl.setPortNum("0");
                    alarmSmokeControl.setCompanyId(list.get(0).getCompanyId());
                    if (CollectionUtils.isNotEmpty(list2)) {
                        alarmSmokeControl.setCompanyName(list2.get(0).getCompanyName());
                    }
                    alarmSmokeControl.setAttributeValue4(dp.get(0).get("value").toString());
                    alarmSmokeControl.setHandlingStatus(0);
                    alarmSmokeControlService.save(alarmSmokeControl);
                    break;
                case 5:
                    AlarmElectricalFire alarmElectricalFire = new AlarmElectricalFire();
                    alarmElectricalFire.setDeviceCode(map.get("devId").toString());
                    alarmElectricalFire.setDeviceName(map.get("deviceName").toString());
                    alarmElectricalFire.setAlarmType(OnlineMethod.getAlarmType(dp.get(0).get("property").toString(), list.get(0).getDeviceType()));
                    if (StringUtils.isNotBlank(map.get("timeStamp").toString())) {
                        LocalDateTime time2 = LocalDateTime.ofEpochSecond(Long.valueOf(map.get("timeStamp").toString()).longValue(), 0, ZoneOffset.ofHours(8));
                        alarmElectricalFire.setAlarmTime(time2);
                    }
//                    alarmElectricalFire.setAddress();
                    alarmElectricalFire.setAlarmStatus(dp.get(0).get("property").toString());
                    alarmElectricalFire.setSystemType(5);
                    alarmElectricalFire.setPortNum("0");
                    alarmElectricalFire.setCompanyId(list.get(0).getCompanyId());
                    if (CollectionUtils.isNotEmpty(list2)) {
                        alarmElectricalFire.setCompanyName(list2.get(0).getCompanyName());
                    }
                    alarmElectricalFire.setAttributeValue4(dp.get(0).get("value").toString());
                    alarmElectricalFire.setHandlingStatus(0);
                    alarmElectricalFireService.save(alarmElectricalFire);
                    break;
                case 6:
                    AlarmVideo alarmVideo = new AlarmVideo();
                    alarmVideo.setDeviceCode(map.get("devId").toString());
                    alarmVideo.setDeviceName(map.get("deviceName").toString());
                    alarmVideo.setAlarmType(dp.get(0).get("property").toString());
                    if (StringUtils.isNotBlank(map.get("timeStamp").toString())) {
                        LocalDateTime time2 = LocalDateTime.ofEpochSecond(Long.valueOf(map.get("timeStamp").toString()).longValue(), 0, ZoneOffset.ofHours(8));
                        alarmVideo.setAlarmTime(time2);
                    }
//                    alarmVideo.setAddress();
                    alarmVideo.setAlarmStatus(dp.get(0).get("property").toString());
                    alarmVideo.setSystemType(6);
                    alarmVideo.setPortNum("0");
                    alarmVideo.setCompanyId(list.get(0).getCompanyId());
                    if (CollectionUtils.isNotEmpty(list2)) {
                        alarmVideo.setCompanyName(list2.get(0).getCompanyName());
                    }
                    alarmVideo.setSitePhoto(dp.get(0).get("value").toString());
                    alarmVideo.setHandlingStatus(0);
                    alarmVideoService.save(alarmVideo);
                    break;
                default:
                    throw new BusinessException("未定义类型");
            }
        }
        return null;
    }


}
