package com.fastbee.deviceData.api.renke.service;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fastbee.deviceData.api.renke.domin.UpdateDevice;
import com.fastbee.deviceData.api.renke.domin.UpdateNodeInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.net.URLEncoder;

/**
 * 建大仁科气象设备服务
 */
@Service
public class RenkeMetDeviceService {

    @Autowired
    private RenKeAuthorizationService authorizationService;




    /**
     * 获取气象设备全部信息
     * @param deviceStr 设备地址，支持多个用英文逗号分隔
     * @return JSONObject
     */
    public JSONObject getDeviceAllInfo(String deviceStr){

        HttpResponse response = HttpRequest.get("http://api.farm.0531yun.cn/api/v2.0/met/device/getDeviceAllInfo?deviceStr="+deviceStr)
                .header("token", authorizationService.getToken())
                .execute();
        String respBodyStr = response.body();
        if(StringUtils.isBlank(respBodyStr)) {
            throw new RuntimeException("获取设备实时数据失败!");
        }
        return JSONUtil.parseObj(respBodyStr);
    }
    /**
     * 根据气象设备地址获取节点信息
     */
    public  JSONObject getAllInsectEquipmentInformation(String deviceAddr){

        //获取设备实时数据
        //请求参数
        HttpResponse response = HttpRequest.get("http://api.farm.0531yun.cn/api/v2.0/met/device/listTargetNodeInfo?deviceAddr="+deviceAddr)
                .header("token", authorizationService.getToken())
                .execute();
        String respBodyStr = response.body();
        if(StringUtils.isBlank(respBodyStr)) {
            throw new RuntimeException("获取设备实时数据失败!");
        }
        return JSONUtil.parseObj(respBodyStr);
    }
    /**
     * 根据气象设备地址获取已启用的节点信息
     */
    public  JSONObject listTargetEnabledNode(String deviceAddr){

        //获取设备实时数据
        //请求参数
        HttpResponse response = HttpRequest.get("http://api.farm.0531yun.cn/api/v2.0/met/device/listTargetEnabledNode?deviceAddr="+deviceAddr)
                .header("token", authorizationService.getToken())
                .execute();
        String respBodyStr = response.body();
        if(StringUtils.isBlank(respBodyStr)) {
            throw new RuntimeException("获取设备实时数据失败!");
        }
        return JSONUtil.parseObj(respBodyStr);
    }
    /**
     * 根据气象节点编号获取遥调信息
     */
    public  JSONObject listTargetEnabledNode(String deviceAddr,String nodeId){
        // 请求基础URL
        String baseUrl = "http://api.farm.0531yun.cn/api/v2.0/met/device/listTargetEnabledNode";

        StringBuilder queryParams = new StringBuilder();
        if (deviceAddr != null && !deviceAddr.isEmpty()) {
            queryParams.append("deviceAddr=").append(URLEncoder.encode(deviceAddr));
        }
        if (nodeId != null && !nodeId.isEmpty()) {
            if (queryParams.length() > 0) queryParams.append("&");
            queryParams.append("nodeId=").append(URLEncoder.encode(nodeId));
        }
        String fullUrl = baseUrl + "?" + queryParams;
        HttpResponse response = HttpRequest.get(fullUrl)
                .header("token", authorizationService.getToken())
                .execute();
        String respBodyStr = response.body();
        if(StringUtils.isBlank(respBodyStr)) {
            throw new RuntimeException("获取虫情设备历史记录失败!");
        }
        return JSONUtil.parseObj(respBodyStr);
    }
    /**
     * 修改指定气象设备全部节点的可用状态
     */
    public  JSONObject updateAllOfNodesEnable(String deviceAddr,String enable){
        // 请求基础URL
        String baseUrl = "http://api.farm.0531yun.cn/api/v2.0/met/device/updateAllOfNodesEnable";

        StringBuilder queryParams = new StringBuilder();
        if (deviceAddr != null && !deviceAddr.isEmpty()) {
            queryParams.append("deviceAddr=").append(URLEncoder.encode(deviceAddr));
        }
        if (enable != null && !enable.isEmpty()) {
            if (queryParams.length() > 0) queryParams.append("&");
            queryParams.append("enable=").append(URLEncoder.encode(enable));
        }
        String fullUrl = baseUrl + "?" + queryParams.toString();
        HttpResponse response = HttpRequest.get(fullUrl)
                .header("token", authorizationService.getToken())
                .execute();
        String respBodyStr = response.body();
        if(StringUtils.isBlank(respBodyStr)) {
            throw new RuntimeException("获取虫情设备历史记录失败!");
        }
        return JSONUtil.parseObj(respBodyStr);
    }

    /**
     * 修改气象设备信息
     */
    public JSONObject updateDevice(UpdateDevice updateDevice){
        //获取设备实时数据
        //请求参数
        HttpResponse response = HttpRequest.post("http://api.farm.0531yun.cn/api/v2.0/met/device/updateDevice")
                .header("token", authorizationService.getToken()).body(JSONUtil.toJsonStr(updateDevice))
                .execute();
        String respBodyStr = response.body();
        if(StringUtils.isBlank(respBodyStr)) {
            throw new RuntimeException("修改气象设备信息失败!");
        }
        return JSONUtil.parseObj(respBodyStr);
    }

    /**
     * 更新气象节点信息
     */
    public JSONObject updateNodeInfo(UpdateNodeInfo updateNodeInfo){
        //获取设备实时数据
        //请求参数
        HttpResponse response = HttpRequest.post("http://api.farm.0531yun.cn/api/v2.0/met/device/updateNodeInfo")
                .header("token", authorizationService.getToken()).body(JSONUtil.toJsonStr(updateNodeInfo))
                .execute();
        String respBodyStr = response.body();
        if(StringUtils.isBlank(respBodyStr)) {
            throw new RuntimeException("修改气象设备信息失败!");
        }
        return JSONUtil.parseObj(respBodyStr);
    }
    /**
     * 根据条件获取历史数据
     */
    public JSONObject getWormDataList(String deviceAddr,String nodeId,String beginTime,String endTime,Integer pages,Integer limit){
        // 请求基础URL
        String baseUrl = "http://api.farm.0531yun.cn/api/v2.0/met/history/getHistoryDataList";

        StringBuilder queryParams = new StringBuilder();
        if (deviceAddr != null && !deviceAddr.isEmpty()) {
            queryParams.append("deviceAddr=").append(URLEncoder.encode(deviceAddr));
        }
        if (beginTime != null && !beginTime.isEmpty()) {
            if (queryParams.length() > 0) queryParams.append("&");
            queryParams.append("beginTime=").append(URLEncoder.encode(beginTime));
        }
        if (endTime != null && !endTime.isEmpty()) {
            if (queryParams.length() > 0) queryParams.append("&");
            queryParams.append("endTime=").append(URLEncoder.encode(endTime));
        }
        if (pages != null) {
            if (queryParams.length() > 0) queryParams.append("&");
            queryParams.append("pages=").append(pages);
        }
        if (limit != null) {
            if (queryParams.length() > 0) queryParams.append("&");
            queryParams.append("limit=").append(limit);
        }
        String fullUrl = baseUrl + "?" + queryParams.toString();
        HttpResponse response = HttpRequest.get(fullUrl)
                .header("token", authorizationService.getToken())
                .execute();
        String respBodyStr = response.body();
        if(StringUtils.isBlank(respBodyStr)) {
            throw new RuntimeException("获取虫情设备历史记录失败!");
        }
        return JSONUtil.parseObj(respBodyStr);
    }

}
