package com.example.zxweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.zxweb.common.api.vo.Result;
import com.example.zxweb.common.constant.enums.IotApiEnum;
import com.example.zxweb.utils.AssertUtils;
import com.example.zxweb.utils.IotUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Description
 * @Author ZhouWenTao
 * @Date 2023/9/8 11:43
 */
@RestController
@RequestMapping("/iot")
@Slf4j
@Api(tags = "设备管理")
public class IotController {
    @ApiOperation(value = "获取单个设备信息")
    @GetMapping(value = "/devices")
    public Result<?> devices(@RequestParam(value = "serial",defaultValue = "")String serial,@RequestParam(value = "productId",defaultValue = "")String productId){
        AssertUtils.notEmpty(serial,"请输入-[serial]");
        //AssertUtils.notEmpty(productId,"请输入-[productId]");
        JSONObject requestBody=new JSONObject();
        requestBody.put("serial",serial);
        requestBody.put("productId",productId);
        //请求数据
        return jsonObjectToResult(IotUtils.getApi(IotApiEnum.getPathByText("获取单个设备信息"), requestBody));
    }

    @ApiOperation(value = "获取设备列表")
    @GetMapping("/devices/list")
    public Result<?> devicesList(@RequestParam(value = "serial",defaultValue = "")String serial,@RequestParam(value = "productId",defaultValue = "")String productId,
                                 @RequestParam(value = "modelCode",defaultValue = "")String modelCode,@RequestParam(value = "authority",defaultValue = "")String authority,
                                 @RequestParam(value = "pageNo",defaultValue = "")String pageNo,@RequestParam(value = "pageSize",defaultValue = "")String pageSize,
                                 @RequestParam(value = "startTime",defaultValue = "")String startTime,@RequestParam(value = "endTime",defaultValue = "")String endTime,
                                 @RequestParam(value = "sort",defaultValue = "")String sort){
        AssertUtils.notEmpty(serial,"请输入-[serial]");
        JSONObject requestBody=new JSONObject();
        requestBody.put("serial",serial);
        requestBody.put("productId",productId);
        requestBody.put("modelCode",modelCode);
        requestBody.put("authority",authority);
        requestBody.put("pageNo",pageNo);
        requestBody.put("pageSize",pageSize);
        requestBody.put("startTime",startTime);
        requestBody.put("endTime",endTime);
        requestBody.put("sort",sort);
        return jsonObjectToResult(IotUtils.getApi(IotApiEnum.getPathByText("获取设备列表"),requestBody));
    }

    @ApiOperation(value = "查询设备历史数据")
    @GetMapping("/devices/datastreams")
    public Result<?> devicesDatastreams(@RequestParam(value = "serial",defaultValue = "")String serial,@RequestParam(value = "productId",defaultValue = "")String productId,
                                 @RequestParam(value = "dataStreamName",defaultValue = "")String dataStreamName,
                                 @RequestParam(value = "pageNo",defaultValue = "")String pageNo,@RequestParam(value = "pageSize",defaultValue = "")String pageSize,
                                 @RequestParam(value = "startTime",defaultValue = "")String startTime,@RequestParam(value = "endTime",defaultValue = "")String endTime,
                                 @RequestParam(value = "sort",defaultValue = "")String sort){
        AssertUtils.notEmpty(serial,"请输入-[serial]");
        AssertUtils.notEmpty(dataStreamName,"请输入-[dataStreamName]");
        JSONObject requestBody=new JSONObject();
        requestBody.put("serial",serial);
        requestBody.put("productId",productId);
        requestBody.put("dataStreamName",dataStreamName);
        requestBody.put("pageNo",pageNo);
        requestBody.put("pageSize",pageSize);
        requestBody.put("startTime",startTime);
        requestBody.put("endTime",endTime);
        requestBody.put("sort",sort);
        return jsonObjectToResult(IotUtils.getApi(IotApiEnum.getPathByText("查询设备历史数据"),requestBody));
    }

    @ApiOperation(value = "查询消息是否下达到设备")
    @GetMapping("/devices/datastreams/state")
    public Result<?> devicesDatastreamsState(@RequestParam(value = "serial",defaultValue = "")String serial,@RequestParam(value = "productId",defaultValue = "")String productId,
                                        @RequestParam(value = "streamCinId",defaultValue = "")String streamCinId){
        AssertUtils.notEmpty(serial,"请输入-[serial]");
        AssertUtils.notEmpty(productId,"请输入-[productId]");
        AssertUtils.notEmpty(streamCinId,"请输入-[streamCinId]");
        JSONObject requestBody=new JSONObject();
        requestBody.put("serial",serial);
        requestBody.put("productId",productId);
        requestBody.put("streamCinId",streamCinId);
        return jsonObjectToResult(IotUtils.getApi(IotApiEnum.getPathByText("查询消息是否下达到设备"),requestBody));
    }

    @ApiOperation(value = "查询事件历史数据")
    @GetMapping("/devices/events")
    public Result<?> devicesEvents(@RequestParam(value = "serial",defaultValue = "")String serial,@RequestParam(value = "productId",defaultValue = "")String productId,
                                   @RequestParam(value = "dataStreamName",defaultValue = "")String dataStreamName,
                                   @RequestParam(value = "pageNo",defaultValue = "1")String pageNo,@RequestParam(value = "pageSize",defaultValue = "10")String pageSize,
                                   @RequestParam(value = "startTime",defaultValue = "")String startTime,@RequestParam(value = "endTime",defaultValue = "")String endTime,
                                   @RequestParam(value = "sort",defaultValue = "")String sort){
        AssertUtils.notEmpty(serial,"请输入-[serial]");
        AssertUtils.notEmpty(dataStreamName,"请输入-[dataStreamName]");
        JSONObject requestBody=new JSONObject();
        requestBody.put("serial",serial);
        requestBody.put("productId",productId);
        requestBody.put("dataStreamName",dataStreamName);
        requestBody.put("pageNo",pageNo);
        requestBody.put("pageSize",pageSize);
        requestBody.put("startTime",startTime);
        requestBody.put("endTime",endTime);
        requestBody.put("sort",sort);
        return jsonObjectToResult(IotUtils.getApi(IotApiEnum.getPathByText("查询事件历史数据"),requestBody));
    }



    @ApiOperation(value = "向设备发送消息")
    @PostMapping("/devices/datastreams")
    public Result<?> devicesDatastreams(@RequestBody JSONObject requestBody){
        return jsonObjectToResult(IotUtils.postApi(IotApiEnum.getPathByText("向设备发送消息"),requestBody));
    }

    @ApiOperation(value = "向设备发送方法")
    @PostMapping("/devices/methods")
    public Result<?> devicesMethods(@RequestBody JSONObject requestBody){
        return jsonObjectToResult(IotUtils.postApi(IotApiEnum.getPathByText("向设备发送方法"),requestBody));
    }

    @ApiOperation(value = "查询方法历史数据")
    @GetMapping("/devices/methods")
    public Result<?> devicesMethods(@RequestParam(value = "serial",defaultValue = "")String serial,@RequestParam(value = "productId",defaultValue = "")String productId,
                                   @RequestParam(value = "methodName",defaultValue = "")String methodName,
                                   @RequestParam(value = "pageNo",defaultValue = "1")String pageNo,@RequestParam(value = "pageSize",defaultValue = "10")String pageSize,
                                   @RequestParam(value = "startTime",defaultValue = "")String startTime,@RequestParam(value = "endTime",defaultValue = "")String endTime,
                                   @RequestParam(value = "sort",defaultValue = "")String sort){
        AssertUtils.notEmpty(serial,"请输入设备序列号-[serial]");
        //AssertUtils.notEmpty(methodName,"请输入方法名称-[methodName]");
        JSONObject requestBody=new JSONObject();
        requestBody.put("serial",serial);
        requestBody.put("productId",productId);
        requestBody.put("methodName",methodName);
        requestBody.put("pageNo",pageNo);
        requestBody.put("pageSize",pageSize);
        requestBody.put("startTime",startTime);
        requestBody.put("endTime",endTime);
        requestBody.put("sort",sort);
        return jsonObjectToResult(IotUtils.getApi(IotApiEnum.getPathByText("查询方法历史数据"),requestBody));
    }

    @ApiOperation(value = "查询方法历史数据(最新一条)")
    @GetMapping("/devices/methods/news")
    public Result<?> devicesMethodsNews(@RequestParam(value = "serial",defaultValue = "")String serial,
                                    @RequestParam(value = "methodName",defaultValue = "")String methodName,
                                    @RequestParam(value = "sort",defaultValue = "")String sort){
        AssertUtils.notEmpty(serial,"请输入设备序列号-[serial]");
        //AssertUtils.notEmpty(methodName,"请输入方法名称-[methodName]");
        JSONObject requestBody=new JSONObject();
        requestBody.put("serial",serial);
        requestBody.put("methodName",methodName);
        requestBody.put("sort",sort);
        return Result.OK(IotUtils.devicesMethodsNews(requestBody));
    }

    public static Result<?> jsonObjectToResult(JSONObject jsonObject) {
        Integer code = jsonObject.getInteger("code");
        String message = jsonObject.getString("message");
        String data = jsonObject.getString("data");
        if (code!=0) {
            return Result.error(code, StringUtils.isEmpty(message)?data:message);
        }
        if (StringUtils.isEmpty(message)) {
            return Result.OK(jsonObject.getJSONObject("data"));
        }
        return Result.OK(message);
    }
}
