package com.gyee.runeconomy.service.EarlyWarning;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gyee.runeconomy.model.vo.PoliceVo;
import com.gyee.runeconomy.model.vo.alarmWtVo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class ApiClient {
    private final RestTemplate restTemplate = new RestTemplate();

    public String getDataFromApi(String begin, String end, String deviceId, String modelId, String deviceType, String alarmIds, String timeType) {
        try {
            // 构建 URL
            String baseUrl = "http://10.220.1.5:6015/alarm/history/findCtFeatureStatByWtId";
            String charset = StandardCharsets.UTF_8.name();  // 获取字符集名称
            String url = String.format("%s?begin=%s&end=%s&deviceid=%s&modelId=%s&deviceType=%s&alarmIds=%s&timeType=%s",
                    baseUrl,
                    URLEncoder.encode(begin, charset),
                    URLEncoder.encode(end, charset),
                    URLEncoder.encode(deviceId, charset),
                    URLEncoder.encode(modelId, charset),
                    URLEncoder.encode(deviceType, charset),
                    URLEncoder.encode(alarmIds, charset),
                    URLEncoder.encode(timeType, charset)
            );

            // 发送 GET 请求
            URI uri = new URI(url);
            ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);

            // 返回响应内容
            return response.getBody();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String getgzFromApi(String begin, String end, String alarmType,String stationid,String deviceid, String description,String modelId,
                               String deviceType,String components, String subcomponents,String alarmId, String isclose, String pageNum,String pageSize) {
        try {
            // 构建 URL
            String baseUrl = "http://10.220.1.5:6015/alarm/history/findAlarmlist";
            String charset = StandardCharsets.UTF_8.name();  // 获取字符集名称
            String url = String.format("%s?begin=%s&end=%s&alarmType=%s&stationid=%s&deviceid=%s&description=%s&modelId=%s&deviceType=%s&components=%s&subcomponents=%s&alarmId=%s&isclose=%s&pageNum=%s&pageSize=%s",
                    baseUrl,
                    URLEncoder.encode(begin, charset),
                    URLEncoder.encode(end, charset),
                    URLEncoder.encode(alarmType, charset),
                    URLEncoder.encode(stationid, charset),
                    URLEncoder.encode(deviceid, charset),
                    URLEncoder.encode(description, charset),
                    URLEncoder.encode(modelId, charset),
                    URLEncoder.encode(deviceType, charset),
                    URLEncoder.encode(components, charset),
                    URLEncoder.encode(subcomponents, charset),
                    URLEncoder.encode(alarmId, charset),
                    URLEncoder.encode(isclose, charset),
                    URLEncoder.encode(pageNum, charset),
                    URLEncoder.encode(pageSize, charset)
            );

            // 发送 GET 请求
            URI uri = new URI(url);
            ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);

            // 返回响应内容
            return response.getBody();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 功率曲线故障
     * @author gyee
     * @param wpId
     * @param wtId
     * @param modelId
     * @param description
     * @param begin
     * @param end
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    public String getglqxgzFromApi(String wpId,String wtId, String modelId,String description ,String begin, String end, String pageNum,String pageSize,String type) {
        try {
            // 构建 URL
            String baseUrl = "http://10.220.1.5:6015//shutdownevent2/queryshutdowneventlist";
            String charset = StandardCharsets.UTF_8.name();  // 获取字符集名称
            String url = String.format("%s?wpId=%s&wtId=%s&modelId=%s&description=%s&begin=%s&end=%s&pageNum=%s&pageSize=%s&type=%s",
                    baseUrl,
                    URLEncoder.encode(wpId, charset),
                    URLEncoder.encode(wtId, charset),
                    URLEncoder.encode(modelId, charset),
                    URLEncoder.encode(description, charset),
                    URLEncoder.encode(begin, charset),
                    URLEncoder.encode(end, charset),
                    URLEncoder.encode(pageNum, charset),
                    URLEncoder.encode(pageSize, charset),
                    URLEncoder.encode(type, charset)
                    );

            // 发送 GET 请求
            URI uri = new URI(url);
            ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);

            // 返回响应内容
            return response.getBody();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static PoliceVo parseJson(String json) throws IOException {
        // 创建ObjectMapper实例
        ObjectMapper objectMapper = new ObjectMapper();

        // 设置ObjectMapper来忽略未知的属性
        objectMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        // 解析JSON并转换为PoliceVo对象
        PoliceVo policeVo = objectMapper.readValue(json, PoliceVo.class);

        return policeVo;
    }

    public static alarmWtVo wtparseJson(String json) throws IOException {
        // 创建ObjectMapper实例
        ObjectMapper objectMapper = new ObjectMapper();

        // 设置ObjectMapper来忽略未知的属性
        objectMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        // 解析JSON并转换为PoliceVo对象
        alarmWtVo alarmWtVo = objectMapper.readValue(json, alarmWtVo.class);

        return alarmWtVo;
    }
}
