package com.usky.fire.service.listener;


import com.usky.fire.service.config.mqtt.MqttInConfig;
import com.usky.fire.service.enums.TopListener;
import com.usky.fire.service.mqtt.SimpleContext;
import com.usky.fire.service.vo.MqttBaseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.MessageHandler;
import org.springframework.stereotype.Component;

/**
 * @author yq
 * @date 2021/11/3 8:13
 */
@ConditionalOnProperty(prefix = "mqtt", value = {"enabled"}, havingValue = "true")
@Slf4j
@Component
public class MqttListener {

    public static final String MESSAGE_NAME = "messageInput";

    @Autowired
    private SimpleContext simpleContext;

    /**
     * 处理消息-消费者
     *
     * @return
     */
    @Bean(MESSAGE_NAME)
    @ServiceActivator(inputChannel = MqttInConfig.CHANNEL_NAME_INPUT)
    public MessageHandler handler() {
        return message -> {
            String payload = message.getPayload().toString();
            //进行接口推送
            String[] code1 = TopListener.FIRE_ALERT.getCode().split("/");
            String[] code2 = TopListener.WATER_ALERT.getCode().split("/");
            String[] code3 = TopListener.RTU_ALERT.getCode().split("/");
            String[] code4 = TopListener.VIDEO_ALERT.getCode().split("/");
            String[] code5 = TopListener.SMOKE_ALERT.getCode().split("/");
            String[] code6 = TopListener.ELECTRICAL_ALERT.getCode().split("/");
            String[] code7 = TopListener.LIQUID_ALERT.getCode().split("/");
            String[] infoCode1 = TopListener.FIRE_INFO.getCode().split("/");
            String[] infoCode2 = TopListener.WATER_INFO.getCode().split("/");
            String[] infoCode3 = TopListener.SMOKE_INFO.getCode().split("/");
            String[] infoCode4 = TopListener.LIQUID_INFO.getCode().split("/");
            String[] infoCode5 = TopListener.RTU_INFO.getCode().split("/");
            String[] infoCode6 = TopListener.ELECTRICAL_INFO.getCode().split("/");
            String[] infoCode7 = TopListener.MH_COVER_INFO.getCode().split("/");
            String[] infoCode8 = TopListener.LR_SMOKE_INFO.getCode().split("/");
            Object mqttReceivedTopic = message.getHeaders().get("mqtt_receivedTopic");
            if (null != mqttReceivedTopic) {
                String topic = mqttReceivedTopic.toString();
                MqttBaseVO mqttBaseVO = new MqttBaseVO();
                mqttBaseVO.setTopic(topic);
                if (TopListener.MH_WATER_INFO.getCode().equals(topic)) {
                    mqttBaseVO.setDescribe("cy");
                    mqttBaseVO.setData(payload);
                } else if (TopListener.MH_WATER_ALERT.getCode().equals(topic)) {
                    mqttBaseVO.setDescribe("cy");
                    mqttBaseVO.setData(payload);
                } else if (TopListener.MH_WATER_STATISTICS.getCode().equals(topic)) {
                    mqttBaseVO.setDescribe("cy");
                    mqttBaseVO.setData(payload);
                } else if (TopListener.DEVICE_DETAIL.getCode().equals(topic)) {
                    mqttBaseVO.setDescribe("mhwater");
                    mqttBaseVO.setData(payload);
                } else if (TopListener.DEVICE_INFO.getCode().equals(topic)) {
                    mqttBaseVO.setDescribe("mhwater");
                    mqttBaseVO.setData(payload);
                } else if (TopListener.DEVICE_ALERT.getCode().equals(topic)) {
                    mqttBaseVO.setDescribe("mhwater");
                    mqttBaseVO.setData(payload);
                } else if (TopListener.DEVICE_AJ.getCode().equals(topic)) {
                    mqttBaseVO.setDescribe("mhwater");
                    mqttBaseVO.setData(payload);
                } else if ((topic.indexOf(code1[2]) != -1 && topic.indexOf(code1[5]) != -1) ||
                        (topic.indexOf(code2[2]) != -1 && topic.indexOf(code2[5]) != -1) ||
                        (topic.indexOf(code3[2]) != -1 && topic.indexOf(code3[5]) != -1) ||
                        (topic.indexOf(code4[2]) != -1 && topic.indexOf(code4[5]) != -1) ||
                        (topic.indexOf(code5[2]) != -1 && topic.indexOf(code5[5]) != -1) ||
                        (topic.indexOf(code6[2]) != -1 && topic.indexOf(code6[5]) != -1) ||
                        (topic.indexOf(code7[2]) != -1 && topic.indexOf(code7[5]) != -1)) {
                    mqttBaseVO.setDescribe("alarm");
                    mqttBaseVO.setData(payload);
                }else if((topic.indexOf(infoCode1[2]) != -1 && topic.indexOf(infoCode1[5]) != -1) ||
                        (topic.indexOf(infoCode2[2]) != -1 && topic.indexOf(infoCode2[5]) != -1) ||
                        (topic.indexOf(infoCode3[2]) != -1 && topic.indexOf(infoCode3[5]) != -1) ||
                        (topic.indexOf(infoCode4[2]) != -1 && topic.indexOf(infoCode4[5]) != -1) ||
                        (topic.indexOf(infoCode5[2]) != -1 && topic.indexOf(infoCode5[5]) != -1) ||
                        (topic.indexOf(infoCode6[2]) != -1 && topic.indexOf(infoCode6[5]) != -1) ||
                        (topic.indexOf(infoCode7[2]) != -1 && topic.indexOf(infoCode7[5]) != -1) ||
                        (topic.indexOf(infoCode8[2]) != -1 && topic.indexOf(infoCode8[5]) != -1)) {
                    mqttBaseVO.setDescribe("info");
                    mqttBaseVO.setData(payload);
                }else {
                    mqttBaseVO.setDescribe("fireInfoAndAlarm");
                    mqttBaseVO.setData(payload);
                }
                //统一处理数据
                simpleContext.getResource(mqttBaseVO);
            }
        };
    }
}
