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

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.alarm.service.enums.TopListener;
import com.usky.alarm.service.job.alarmLinkageJob;
import com.usky.common.core.util.JsonUtils;
import com.usky.alarm.domain.*;
import com.usky.alarm.service.*;
import com.usky.alarm.service.mqtt.MqttStrategy;
import com.usky.alarm.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.List;
import java.util.Map;

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

    @Autowired
    private BaseAlarmService baseAlarmService;

    @Autowired
    private DmpDeviceService dmpDeviceService;

    @Autowired
    private DmpDeviceLinkageService dmpDeviceLinkageService;

    @Autowired
    private alarmLinkageJob AlarmLinkageJob;

    @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")));
        BaseAlarm baseAlarm = new BaseAlarm();
        long timestamp = Long.valueOf(alarams.get(0).get("timeStamp").toString()).longValue();
        LocalDateTime time2 = LocalDateTime.ofEpochSecond(timestamp, 0, ZoneOffset.ofHours(8));
        String topic = mqttBaseVO.getTopic();
        String[] code4 = TopListener.VIDEO_ALERT.getCode().split("/");
        LambdaQueryWrapper<DmpDevice> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.select(DmpDevice::getProductCode,DmpDevice::getDeviceUuid)
                .eq(DmpDevice::getCompanyCode,topic.split("/")[3])
                .eq(DmpDevice::getDeviceId, map.get("devId").toString())
                .eq(DmpDevice::getDeleteFlag, 0);
        List<DmpDevice> list = dmpDeviceService.list(queryWrapper1);
        baseAlarm.setDeviceId(alarams.get(0).get("devId").toString());
        baseAlarm.setAlarmTime(time2);
        String alarmType = dp.get(0).get("serial").toString();
        baseAlarm.setAlarmType(alarmType);
        baseAlarm.setAlarmObject(alarams.get(0).get("deviceName").toString());
        baseAlarm.setAlarmData(dp.get(0).get("status").toString());
        baseAlarm.setAlarmAttribute(dp.get(0).get("property").toString());
        if (alarams.get(0).get("deviceType").equals("1")){
            baseAlarm.setAlarmContent(dp.get(0).get("property").toString()+","+dp.get(0).get("value").toString());
            if(alarmType.equals("812") || alarmType.equals("815") || alarmType.equals("819")){
                baseAlarm.setAlarmGrade(1);
            }else{
                baseAlarm.setAlarmGrade(2);
            }
            baseAlarm.setAlarmAddress(dp.get(0).get("value").toString());
        }else if (alarams.get(0).get("deviceType").equals("2")){
            baseAlarm.setAlarmContent(dp.get(0).get("property").toString()+","+dp.get(0).get("value").toString());
            baseAlarm.setAlarmGrade(2);
            baseAlarm.setAlarmAddress(alarams.get(0).get("deviceName").toString());
        }else if (alarams.get(0).get("deviceType").equals("3")||alarams.get(0).get("deviceType").equals("137")){
            baseAlarm.setAlarmContent(dp.get(0).get("property").toString());
            baseAlarm.setAlarmGrade(1);
            baseAlarm.setAlarmAddress(alarams.get(0).get("deviceName").toString());
            LambdaQueryWrapper<DmpDeviceLinkage> queryWrapper5 = Wrappers.lambdaQuery();
            queryWrapper5.eq(DmpDeviceLinkage::getDeviceId,map.get("devId").toString());
            List<DmpDeviceLinkage> list5 = dmpDeviceLinkageService.list(queryWrapper5);
            if(CollectionUtils.isNotEmpty(list5)){
                LambdaQueryWrapper<DmpDevice> queryWrapper6 = Wrappers.lambdaQuery();
                queryWrapper6.select(DmpDevice::getDeviceId,DmpDevice::getProductCode,DmpDevice::getDeviceUuid)
                        .eq(DmpDevice::getGroupId, list5.get(0).getGroupId())
                        .eq(DmpDevice::getDeviceType, 138)
                        .eq(DmpDevice::getDeleteFlag, 0);
                List<DmpDevice> list6 = dmpDeviceService.list(queryWrapper6);
                JsonObject jsonObject = new JsonObject();
                int index = list6.get(0).getProductCode().indexOf("_");
                String result = list6.get(0).getProductCode().substring(index+1,
                        list6.get(0).getProductCode().toString().length());
                if (CollectionUtils.isNotEmpty(list6)) {
                    jsonObject.addProperty("productId", result);
                    jsonObject.addProperty("tcDeviceId",list6.get(0).getDeviceUuid());
                    jsonObject.addProperty("deviceId",list6.get(0).getDeviceId());
                    jsonObject.addProperty("deviceType","JH");
                    jsonObject.addProperty("linkageAddress",list5.get(0).getLinkageAddress());
                    JSONObject resultVo = AlarmLinkageJob.addLinkage(jsonObject.toString());
                }
            }
        }else if (alarams.get(0).get("deviceType").equals("6")){
            baseAlarm.setAlarmContent(dp.get(0).get("property").toString()+","+dp.get(0).get("stuname").toString());
            baseAlarm.setAlarmGrade(2);
            baseAlarm.setAlarmAddress(alarams.get(0).get("deviceName").toString());
        }else if (alarams.get(0).get("deviceType").equals("7")){
            baseAlarm.setAlarmContent(dp.get(0).get("property").toString());
            baseAlarm.setAlarmGrade(2);
            baseAlarm.setAlarmAddress(alarams.get(0).get("deviceName").toString());
        }else if (alarams.get(0).get("deviceType").equals("131")){
            baseAlarm.setAlarmContent(dp.get(0).get("property").toString());
            baseAlarm.setAlarmGrade(1);
            baseAlarm.setAlarmAddress(alarams.get(0).get("deviceName").toString());
        }else if (alarams.get(0).get("deviceType").equals("128")){
            baseAlarm.setAlarmContent(dp.get(0).get("property").toString());
            baseAlarm.setAlarmGrade(2);
            baseAlarm.setAlarmAddress(alarams.get(0).get("deviceName").toString());
        }else if (alarams.get(0).get("deviceType").equals("129")){
            baseAlarm.setAlarmContent(dp.get(0).get("property").toString());
            baseAlarm.setAlarmGrade(2);
            baseAlarm.setAlarmAddress(alarams.get(0).get("deviceName").toString());
        }else if (alarams.get(0).get("deviceType").equals("130")){
            baseAlarm.setAlarmContent(dp.get(0).get("property").toString());
            baseAlarm.setAlarmGrade(2);
            baseAlarm.setAlarmAddress(alarams.get(0).get("deviceName").toString());
        }else if (alarams.get(0).get("deviceType").equals("136")){
            baseAlarm.setAlarmContent(dp.get(0).get("property").toString());
            baseAlarm.setAlarmGrade(2);
            baseAlarm.setAlarmAddress(alarams.get(0).get("deviceName").toString());
        }else if (topic.indexOf(code4[2]) != -1 && topic.indexOf(code4[5]) != -1){
            baseAlarm.setAlarmContent(dp.get(0).get("property").toString());
            baseAlarm.setAlarmGrade(2);
            baseAlarm.setAlarmAddress(alarams.get(0).get("deviceName").toString());
            baseAlarm.setSitePhoto(dp.get(0).get("value").toString());
        }
        if (CollectionUtils.isNotEmpty(list)) {
            baseAlarm.setProductCode(list.get(0).getProductCode());
            try {
                Boolean addFlag = baseAlarmService.add(baseAlarm);
            }catch (Exception a){

            }
        }
        return null;
    }


}
