package com.xueyi.message.handler;

import com.alibaba.fastjson2.JSONObject;
import com.xueyi.common.core.constant.basic.SecurityConstants;
import com.xueyi.common.core.web.result.R;
import com.xueyi.common.mqtt.annotation.MqttService;
import com.xueyi.common.mqtt.annotation.MqttTopic;
import com.xueyi.message.api.transfer.domain.vo.DmDeployScheduleVo;
import com.xueyi.system.api.device.domain.vo.DeviceTenantSourceMergeVo;
import com.xueyi.system.api.device.feign.RemoteDeviceTenantMergeService;
import com.xueyi.system.api.digitalmans.feign.RemoteDigitalmanMqttService;
import com.xueyi.system.api.version.feign.RemoteReleaseManagerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;


@MqttService
@Component
public class MqttTopicHandle {

    private static final Logger log = LoggerFactory.getLogger(MqttTopicHandle.class);
    @Autowired
    private RemoteDeviceTenantMergeService remoteDeviceTenantMergeService;

    @Autowired
    private RemoteDigitalmanMqttService  remoteDigitalmanMqttService;

    @Autowired
    private RemoteReleaseManagerService remoteReleaseManagerService;

    /**
     * 以下内容是使用范例
     // 这里的 # 号是通配符
     @MqttTopic("test/#")
     public void test(Message<?> message){
     log.info("test="+message.getPayload());
     }

     // 这里的 + 号是通配符
     @MqttTopic("topic/+/+/up")
     public void up(Message<?> message){
     log.info("up="+message.getPayload());
     }

     // 注意 你必须先订阅
     @MqttTopic("topic/1/2/down")
     public void down(Message<?> message){
     log.info("down="+message.getPayload());
     }
     */

    @MqttTopic("digital_man/+/reply/setup")
    public void setup(Message message){
     log.info("down="+message.getPayload());
    }

    @MqttTopic("/digital_man/heart_beat")
    //@TenantIgnore(tenantLine = true)
    public void heartbeat(Message message){
        System.err.println("rev heartbeat mqtt msg:"+message.getPayload().toString());

        String messageStr =  message.getPayload().toString();
        JSONObject heartBeatObj = JSONObject.parseObject(messageStr);
        String devId = heartBeatObj.getString("devId");
        DeviceTenantSourceMergeVo vo = remoteDeviceTenantMergeService.selectDeviceTenantSourceMerge(devId);
        remoteDigitalmanMqttService.heartbeat(messageStr, vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);

    }

    @MqttTopic("/digital_man/log_upload_notification")
    public void logUpload(Message message){
        System.err.println("rev logUpload mqtt msg:"+message.getPayload().toString());

        String messageStr =  message.getPayload().toString();
        JSONObject heartBeatObj = JSONObject.parseObject(messageStr);
        String devId = heartBeatObj.getString("devId");
        DeviceTenantSourceMergeVo vo = remoteDeviceTenantMergeService.selectDeviceTenantSourceMerge(devId);
        remoteDigitalmanMqttService.logUpload(messageStr, vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);
    }

    @MqttTopic("/digital_man/reply/setup")
    public void setupByDevId(Message message) {
        log.info("rev setup mqtt msg:" + message.getPayload().toString());
        DmDeployScheduleVo vo =  JSONObject.parseObject(message.getPayload().toString(), DmDeployScheduleVo.class);
        R<Integer> result = remoteReleaseManagerService.syncDeployStatus(vo);

    }
}
