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 com.example.zxweb.vo.QueryCelueVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author ZhouWenTao
 * @Date 2023/9/12 10:28
 */
@RestController
@RequestMapping("/gateway")
@Slf4j
@Api(tags = "网关设备管理")
public class GatewayController {
    //网关设备
    private static final List<String> gatewayDevIdList=new ArrayList<>(Arrays.asList("0cefafd605f7","0cefafd295a4"));
    /**
     * 获取网关策略列表
     * @param queryCelueVO 入参
     * @return 结果
     */
    @GetMapping(value = "/celueListQuery")
    public Result<?> celueListQuery(QueryCelueVO queryCelueVO){
        String devid = queryCelueVO.getDevid();
        Integer pageNum = queryCelueVO.getPage();
        Integer pageSize = queryCelueVO.getLimit();
        AssertUtils.notEmpty(devid,"请输入-[devid]");
        String format = String.format("{\"serial\":\"%s\",\"productId\":\"\",\"methodName\":\"celueListQuery\",\"inputData\":{\"devid\":\"%s\"}}", devid,devid);
        JSONObject requestBody = JSONObject.parseObject(format);
        JSONObject responseBody = IotUtils.postApi(IotApiEnum.getPathByText("向设备发送方法"), requestBody);
        //JSONObject responseBody= JSONObject.parseObject("{\t\"code\":\"0\",\t\"data\":{\t\t\"outputData\":{\t\t\t\"devid\":\"0cefafd605f7\",\t\t\t\"code\":1,\t\t\t\"list\":[\t\t\t\t{\t\t\t\t\t\"devid\":\"0cefafd605f7\",\t\t\t\t\t\"triggerCondition\":\"body0001.body_exist.status.report_status=\\\"0\\\"##900\",\t\t\t\t\t\"name\":\"策略1\",\t\t\t\t\t\"action\":\"light00001.light.controlLight.ctrl_ch0_status=\\\"0\\\"&&chazuo000001.plug.controlPlug.ctrl_ch0_status=\\\"0\\\"\",\t\t\t\t\t\"startTime\":\"2023-11-12 10:00:00\",\t\t\t\t\t\"endTime\":\"2023-11-12 11:30:00\",\t\t\t\t\t\"celueid\":\"100000000001\"\t\t\t\t},\t\t\t\t{\t\t\t\t\t\"devid\":\"0cefafd605f7\",\t\t\t\t\t\"triggerCondition\":\"body0001.body_exist.status.report_status=\\\"0\\\"##900\",\t\t\t\t\t\"name\":\"策略2\",\t\t\t\t\t\"action\":\"light00001.light.controlLight.ctrl_ch0_status=\\\"0\\\"&&chazuo000001.plug.controlPlug.ctrl_ch0_status=\\\"0\\\"\",\t\t\t\t\t\"startTime\":\"2023-11-12 10:00:00\",\t\t\t\t\t\"endTime\":\"2023-11-12 11:30:00\",\t\t\t\t\t\"celueid\":\"100000000002\"\t\t\t\t}\t\t\t]\t\t}\t}}");
        JSONObject data = responseBody.getJSONObject("data");
        if (data!=null) {
            JSONObject outputData = data.getJSONObject("outputData");
            if (outputData!=null) {
                List<JSONObject> list = outputData.getJSONArray("list").toJavaList(JSONObject.class);
                if (pageNum==0) {
                    pageNum=1;
                }
                List<JSONObject> subList = list.stream().skip((long) (pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
                outputData.put("list",subList);
                outputData.put("total",list.size());
                data.put("outputData",outputData);
                responseBody.put("data",data);
            }
        }
        return jsonObjectToResult(responseBody);
    }


    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);
    }
}
