package com.xinda.mq.service.impl;

import com.xinda.common.core.mq.DeviceReportBo;
import com.xinda.common.enums.TopicType;
import com.xinda.common.utils.StringUtils;
import com.xinda.common.utils.gateway.mq.TopicsUtils;
import com.xinda.iot.domain.Device;
import com.xinda.iot.service.IDeviceService;
import com.xinda.mq.model.ReportDataBo;
import com.xinda.mq.service.IDataHandler;
import com.xinda.mq.service.IMqttMessagePublish;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author gsb
 * @date 2023/2/27 14:42
 */
@Component
@Slf4j
public class DeviceOtherMsgHandler {

    @Resource
    private TopicsUtils topicsUtils;
    @Resource
    private IDataHandler dataHandler;
    @Resource
    private IMqttMessagePublish messagePublish;
    @Resource
    private IDeviceService deviceService;

    /**
     * true: 使用netty搭建的mqttBroker  false: 使用emq
     */
    @Value("${server.broker.enabled}")
    private Boolean enabled;


    /**
     * 非属性消息消息处理入口
     *
     * @param bo
     */
    public void messageHandler(DeviceReportBo bo) {
        String type = "";
        String name = "";
        if (bo.getTopicName().startsWith(TopicType.PROPERTY_XINDA_DEV.getTopicSuffix()) || bo.getTopicName().startsWith(TopicType.FUNCTION_XINDA_DOWN.getTopicSuffix())) {
            name = topicsUtils.parseNewTopicName(bo.getTopicName());
        }else{
            name = topicsUtils.parseTopicName(bo.getTopicName());
        }
        if (StringUtils.isEmpty(name) || name.endsWith(TopicType.FUNCTION_GET.getTopicSuffix()) || name.startsWith(TopicType.FUNCTION_XINDA_DOWN.getTopicSuffix())  ) return;
        ReportDataBo data = this.buildReportData(bo);
        TopicType topicType = TopicType.getType(name);
        switch (topicType) {
            case INFO_POST:
                dataHandler.reportDevice(data);
                break;
            case NTP_POST:
                messagePublish.publishNtp(data);
                break;
            case FUNCTION_POST:
                data.setShadow(false);
                data.setType(2);
                data.setRuleEngine(true);
                dataHandler.reportData(data);
                break;
            case EVENT_POST:
                data.setType(3);
                data.setRuleEngine(true);
                dataHandler.reportEvent(data);
                break;
        }
    }

    /**
     * 组装数据
     */
    private ReportDataBo buildReportData(DeviceReportBo bo) {
        String message = new String(bo.getData());
        log.info("收到设备信息[{}]", message);
        String serialNumber = "";
        ReportDataBo dataBo = new ReportDataBo();
        dataBo.setMessage(message);
        Long productId;
        if (bo.getTopicName().startsWith(TopicType.PROPERTY_XINDA_DEV.getTopicSuffix()) || bo.getTopicName().startsWith(TopicType.FUNCTION_XINDA_DOWN.getTopicSuffix())) {
            serialNumber = topicsUtils.parseNewSerialNumber(bo.getTopicName());
            productId = deviceService.selectDeviceBySerialNumber(serialNumber).getProductId();
        }else{
            serialNumber = topicsUtils.parseSerialNumber(bo.getTopicName());
            productId = topicsUtils.parseProductId(bo.getTopicName());
        }
        dataBo.setProductId(productId);
        dataBo.setSerialNumber(serialNumber);
        dataBo.setRuleEngine(false);
        return dataBo;
    }
}
