package com.ruoyi.dj.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.dj.mqtt.client.DJICloudMQTTClient;
import com.ruoyi.dj.mqtt.cmd.CmdConstant;
import com.ruoyi.dj.mqtt.common.CommonFieldsVo;
import com.ruoyi.dj.mqtt.dto.DataParamDto;
import com.ruoyi.dj.mqtt.dto.DataParamDto01;
import com.ruoyi.dj.mqtt.dto.RemoteServiceDto;
import com.ruoyi.dj.mqtt.utils.GatewayOperationLogUtil;
import com.ruoyi.dj.mqtt.utils.HandleResultUtil;
import com.ruoyi.dj.mqtt.utils.PayloadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@Api(tags = "远程调试相关")
@RequestMapping("/dj")
public class MqttController {

    @Resource
    private DJICloudMQTTClient djiCloudMQTTClient;

    private static final String SERVICES_TOPIC = "thing/product/?/services";

    /**
     * 发送和Service相关的无数据命令并等待其回复
     *
     * @return
     */
    @ApiOperation("发送无数据命令")
    @PostMapping("/remote/debug/service")
    public AjaxResult debugModeOpen(@RequestBody RemoteServiceDto remoteServiceDto) {
        String topic = SERVICES_TOPIC.replace("?", remoteServiceDto.getGateway());
        CommonFieldsVo commonFieldVo = CommonFieldsVo.createCommonFieldVo(remoteServiceDto.getMethod(), remoteServiceDto.getGateway());
        String payload = PayloadUtil.generatePayload(commonFieldVo, null);
        log.info("远程调试相关发送数据：" + payload);
        String response = djiCloudMQTTClient.publishMessage(topic, payload, commonFieldVo);
        log.info("远程调试相关返回数据：" + response);
        AjaxResult ajaxResult = HandleResultUtil.handleResult(response, null,"操作成功");
        // 记录日志
        GatewayOperationLogUtil.generateLog(remoteServiceDto.getGateway(), remoteServiceDto.getMethod(), payload, response);
        return ajaxResult;
    }

    /**
     * 电池维护开关切换
     *
     * @return
     */
    @ApiOperation("电池维护开关切换")
    @PostMapping("/remote/debug/batteryMaintenanceSwitch")
    public AjaxResult batteryMaintenanceSwitch(@RequestBody DataParamDto dataParam) {
        Map map = new HashMap();
        if (dataParam.getAction().equals("supplement_light_open")) {
            map.put("msg", "补光灯已打开");
        } else if (dataParam.getAction().equals("supplement_light_close")) {
            map.put("msg", "补光灯已关闭");
        }
        String topic = SERVICES_TOPIC.replace("?", dataParam.getGateway());
        CommonFieldsVo commonFieldVo = CommonFieldsVo.createCommonFieldVo(CmdConstant.BATTERY_MAINTENANCE_SWITCH, dataParam.getGateway());
        String payload = PayloadUtil.generatePayload(commonFieldVo, dataParam.getAction());
        String response = djiCloudMQTTClient.publishMessage(topic, payload, commonFieldVo);
        return HandleResultUtil.handleResult(response, map,"电池维护开关切换切换成功");
    }

    /**
     * 空调模式开关切换
     *
     * @return
     */
    @ApiOperation("空调模式开关切换")
    @PostMapping("/remote/debug/airConditionerModeSwitch")
    public AjaxResult airConditionerModeSwitch(@RequestBody DataParamDto01 dataParam) {
        String topic = SERVICES_TOPIC.replace("?", dataParam.getGateway());
        CommonFieldsVo commonFieldVo = CommonFieldsVo.createCommonFieldVo(CmdConstant.AIR_CONDITIONER_MODE_SWITCH, dataParam.getGateway());
        HashMap<Object, Object> map = new HashMap<>();
        map.put("action", dataParam.getAction());
        String payload = PayloadUtil.generatePayload(commonFieldVo, map);
        String response = djiCloudMQTTClient.publishMessage(topic, payload, commonFieldVo);
        return HandleResultUtil.handleResult(response, null,"空调模式开关切换成功");
    }


    /**
     * 增强图传开关
     *
     * @return
     */
    @ApiOperation("增强图传开关")
    @PostMapping("/remote/debug/sdrWorkmodeSwitch")
    public AjaxResult sdrWorkmodeSwitch(@RequestBody DataParamDto dataParam) {
        String topic = SERVICES_TOPIC.replace("?", dataParam.getGateway());
        CommonFieldsVo commonFieldVo = CommonFieldsVo.createCommonFieldVo(CmdConstant.SDR_WORKMODE_SWITCH, dataParam.getGateway());
        Map<String, Object> map = new HashMap<>();
        map.put("link_workmode", dataParam.getAction());
        String payload = PayloadUtil.generatePayload(commonFieldVo, map);
        String response = djiCloudMQTTClient.publishMessage(topic, payload, commonFieldVo);
        return HandleResultUtil.handleResult(response, null,"增强图传开关成功");
    }
}
