package com.huashe.park.collect.controller;

import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huashe.common.domain.AjaxResult;
import com.huashe.park.collect.config.TopicDataCfg;
import com.huashe.park.collect.core.MqttMessageHandler;
import com.huashe.park.collect.core.MqttTemplate;
import com.huashe.park.collect.model.TopicVO;
import com.huashe.park.common.ByteArrayUtil;
import com.huashe.park.common.SpringBeanUtils;
import com.huashe.park.core.service.IMachineProcessResultService;
import com.huashe.park.core.service.IMachineProcessService;
import com.huashe.park.domain.dto.cons.MqttRequest;
import com.huashe.park.domain.entity.MachineProcess;
import com.huashe.park.domain.entity.MachineProcessResult;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;

import cn.hutool.core.util.HexUtil;

/**
 * 缓存监控
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/topic")
@Anonymous
public class TopicController extends BaseController {

    @Autowired
    private MqttTemplate mqttTemplate;

    @Autowired
    private TopicDataCfg topicDataCfg;

    @Autowired
    private IMachineProcessResultService machineProcessResultService;

    @Autowired
    private IMachineProcessService machineProcessService;

    @PostMapping
    @Anonymous
    public void send(@RequestBody MqttRequest jsonObject) {
        mqttTemplate.send(jsonObject.getTopic(), jsonObject.getTopic() + "/machine/piling/result",
            jsonObject.getPayload(), jsonObject.getQos(), jsonObject.isRetained());
    }

    @PostMapping("/byte/process")
    @Anonymous
    public void sendByte(@RequestBody MqttRequest jsonObject) {
        byte[] bytes = HexUtil.decodeHex(jsonObject.getPayload());
        mqttTemplate.sendByte(jsonObject.getTopic(), jsonObject.getTopic() + "/machine/piling/process", bytes,
            jsonObject.getQos(), jsonObject.isRetained());
    }

    @PostMapping("/byte/result")
    @Anonymous
    public void sendResultByte(@RequestBody MqttRequest jsonObject) {
        byte[] bytes = HexUtil.decodeHex(jsonObject.getPayload());
        mqttTemplate.sendByte(jsonObject.getTopic(), jsonObject.getTopic() + "/machine/piling/result", bytes,
            jsonObject.getQos(), jsonObject.isRetained());
    }

    @PostMapping("/obj/byte/result")
    @Anonymous
    public AjaxResult sendObjResultByte(@RequestBody TopicVO result) throws Exception {
        byte[] bytes = ByteArrayUtil.serializeObject(result.getResult());
        MqttRequest jsonObject = result.getTopic();
        mqttTemplate.sendByte(jsonObject.getTopic(), jsonObject.getTopic() + "/machine/piling/result", bytes,
            jsonObject.getQos(), jsonObject.isRetained());
        return AjaxResult.success(topicDataCfg.byteFormat(bytes, topicDataCfg.getResultTopic().getProtocol()));
    }

    @PostMapping("/obj/byte/process")
    @Anonymous
    public AjaxResult sendObjByte(@RequestBody TopicVO process) throws Exception {
        byte[] bytes = ByteArrayUtil.serializeObject(process.getProcess());
        MqttRequest jsonObject = process.getTopic();
        mqttTemplate.sendByte(jsonObject.getTopic(), jsonObject.getTopic() + "/machine/piling/process", bytes,
            jsonObject.getQos(), jsonObject.isRetained());
        return AjaxResult.success(topicDataCfg.byteFormat(bytes, topicDataCfg.getProcessTopic().getProtocol()));
    }

    @PostMapping("/decode/result")
    @Anonymous
    public AjaxResult decodeResult(@RequestBody String hex) throws Exception {
        byte[] bytes = HexUtil.decodeHex(
            "c800000000019624524dcb696c66313731666c313131786271696f38776d6167656d384c37896159834a41894160e5621923410b368d6a59834a41628db143621923410a368d6a59834a41698db14362192341e56fcc3f90ae7bb00000000000000000e878bd3c0000000000000196245059ca0000019624524dca00000000c2c2499a3f262d4000000000e56fcc3fe56fcc3f000000000000000000000000000020428fae7b3000000000c800000000000196245059ca000001962450d6ac000001962450d6ac0000019624524dd4313131786271696f5f313130445339383431323330303231325f333639");
        return AjaxResult.success(topicDataCfg.byteFormat(bytes, topicDataCfg.getResultTopic().getProtocol()));
    }

    // fffd00001fffd24524dfffd696c66313731666c313131786271696f38776d6167656d384c37fffd6159fffd4a41fffd4160fffd62192341b36fffd6a59fffd4a4162fffdfffd4362192341a36fffd6a59fffd4a4169fffdfffd4362192341fffd6ffffd3ffffdfffd7bfffd00000000fffd78fffd3c0000001fffd245059fffd001fffd24524dfffd0000fffdfffd49fffd3f262d400000fffd6ffffd3ffffd6ffffd3f000000000000002042fffdfffd7b300000fffd000001fffd245059fffd001fffd24505ac001fffd24505ac001fffd24524dfffd313131786271696f5f313130445339383431323330303231325f333639
    @PostMapping("/new/topic")
    @Anonymous
    public void newTopic(@RequestBody MqttRequest mqttRequest) {
        MqttMessageHandler bean = SpringBeanUtils.getBean(mqttRequest.getHandleBean());
        mqttTemplate.subscribe(mqttRequest.getClientKey(), mqttRequest.getClientKey(), mqttRequest.getClientSecret(),
            mqttRequest.getTopic(), mqttRequest.getQos(), bean);
    }
}
