package com.inspect.client.controller;

import com.alibaba.fastjson.JSONObject;
import com.inspect.client.service.ClientService;
import com.inspect.base.core.exception.ServiceException;
import com.inspect.base.core.service.SyncDataToUpstreamService;
import com.inspect.base.core.utils.SystemInfo;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.task.controller.PatrolTaskController;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.annotation.Resource;

import com.inspect.taskstatus.client.FeignTaskClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
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;

@Slf4j
@RestController
@RequestMapping({"/client"})
public class MainClientController {
   @Resource
   private SyncDataToUpstreamService syncDataToUpstreamService;
   @Resource
   private ClientService clientService;
   @Resource
   private PatrolTaskController patrolTaskController;

   @Resource
   private FeignTaskClient feignTaskClient;

   @PostMapping({"/upData"})
   public AjaxResult upData(String type, String msg) {
      try {
         JSONObject.parseObject(msg);
         return syncDataToUpstreamService.postMessage(type, msg);
      } catch (Exception e) {
         final String errorMsg = "REPORT DATA FAIL, DATA FORMAT EXCEPTION:" + e.getMessage();
         throw new ServiceException(errorMsg);
      }
   }

   @PostMapping({"/send"})
   public AjaxResult send(@RequestBody String msg) {
      try {
         JSONObject jsonObject = JSONObject.parseObject(msg);
         AjaxResult ajaxResult = clientService.handleLowMsg(jsonObject);
         return ajaxResult;
      } catch (ServiceException var4) {
         this.log.error("下发上级系统传递的消息捕获异常", var4);
         throw new ServiceException(var4.getMessage());
      } catch (Exception var5) {
         this.log.error("下发上级系统传递的消息捕获异常", var5);
         throw new ServiceException("下发数据失败，msg格式不正确");
      }
   }

   @PostMapping({"/sendImportantMsg"})
   public AjaxResult sendImportantMsg(@RequestBody String msg) {
      try {
         JSONObject jsonObject = JSONObject.parseObject(msg);
          return clientService.handleLowMsg(jsonObject);
      } catch (Exception e) {
         final String errorMsg = "SEND IMPORTANT MSG EXCEPTION:" + e.getMessage();
         log.error(errorMsg);
         throw new ServiceException(errorMsg);
      }
   }

   @PostMapping({"/sendCommand"})
   public AjaxResult sendCommand(@RequestBody String msg) {
      try {
         log.info("SEND_COMMAND_TO_CLIENT param: {}", msg);
         JSONObject.parseObject(msg);
         JSONObject jsonObject = clientService.callDeviceCommand(msg);
         log.info("SEND_COMMAND_TO_CLIENT response: {}", msg);
         return StringUtils.equals(jsonObject.getString("code"), "500")?AjaxResult.fail(500, jsonObject.getString("msg")):AjaxResult.success();
      } catch (ServiceException e) {
         final String errorMsg = "SEND_COMMAND_TO_CLIENT SERVICE EXCEPTION:" + e.getMessage();
         log.error(errorMsg);
         //throw new ServiceException(errorMsg);
         return AjaxResult.fail(500, e.getMessage());
      } catch (Exception e) {
         final String errorMsg = "SEND_COMMAND_TO_CLIENT OTHER EXCEPTION:" + e.getMessage();
         log.error(errorMsg);
         //throw new ServiceException(errorMsg);
         return AjaxResult.fail(500, e.getMessage());
      }
   }

   public static void main(String[] args) {
      SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String date = simpleDate.format(new Date());
      System.out.println(date);
   }

   @GetMapping({"/systemInfo"})
   public AjaxResult systemInfo() {
      try {
         JSONObject systemInfo = SystemInfo.getSystemInfo();
         return AjaxResult.success(systemInfo);
      } catch (Exception e) {
         final String errorMsg = "[CLIENT] SYSTEM INFO EXCEPTION:" + e.getMessage();
         log.error(errorMsg);
         throw new ServiceException("SYSTEM BUSY, TRY LATER!");
      }
   }

   @PostMapping({"/syncModel"})
   public AjaxResult syncModel(@RequestBody String msg) {
      try {
         // 和许继，无人机同步点位
         JSONObject jsonObjectParm = JSONObject.parseObject(msg);
         log.info("========syncModel param: {}", jsonObjectParm);
         if (StringUtils.isEmpty(jsonObjectParm.getString("deviceCode"))) {
            log.info("[=>TCPSERVER] 巡视主机下发同步模型命令: 设备编码为空");
            return AjaxResult.fail(500, "设备编码为空");
         }
         JSONObject jsonObject = new JSONObject();
         jsonObject.put("Type", "61");
         jsonObject.put("ReceiveCode", jsonObjectParm.getString("deviceCode"));
         String msgs = jsonObject.toJSONString();
         AjaxResult ajaxResult = feignTaskClient.sendCommand(msgs);
         log.info("[=>TCPSERVER] 巡视主机下发同步模型命令: {}, 结果: {}", msgs, jsonObject);
         return ajaxResult;
      } catch (ServiceException e) {
         log.error("[=>TCPSERVER] 巡视主机下发同步模型命令服务异常: {}", e.getMessage());
         throw new ServiceException(e.getMessage());
      } catch (Exception e) {
         log.error("[=>TCPSERVER] 巡视主机下发同步模型命令: {}", e.getMessage());
         throw new ServiceException("下发数据失败，msg格式不正确");
      }
   }

   public AjaxResult syncModelDeprecated(@RequestBody String msg) {
      try {
         JSONObject.parseObject(msg);
         JSONObject jsonObject = clientService.callDeviceCommand(msg);
         log.info("[=>TCPSERVER] 巡视主机下发同步模型命令: {}, 结果: {}", msg, jsonObject);
         return StringUtils.equals(jsonObject.getString("code"), "500")?AjaxResult.fail(500, jsonObject.getString("msg")):AjaxResult.success();
      } catch (ServiceException e) {
         log.error("[=>TCPSERVER] 巡视主机下发同步模型命令服务异常: {}", e.getMessage());
         throw new ServiceException(e.getMessage());
      } catch (Exception e) {
         log.error("[=>TCPSERVER] 巡视主机下发同步模型命令: {}", e.getMessage());
         throw new ServiceException("下发数据失败，msg格式不正确");
      }
   }
}
