package com.sptg.third.biz.topsailiot.util;

import com.sptg.common.core.constant.HttpConstant;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.third.api.contants.UrlContants;
import com.sptg.third.api.entity.ApiCommonResult;
import com.sptg.third.api.entity.topsailiot.*;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

/**
 * @author zwx
 * @create 2020/12/6 11:22
 */
public class TopsailHttpUtil {

    private TopsailSysConfig topsailSysConfig;
    private RestTemplate restTemplate = new RestTemplate();

    public TopsailHttpUtil(TopsailSysConfig topsailSysConfig) {
        this.topsailSysConfig = topsailSysConfig;
    }

    /**
     * 获取项目列表
     * @return
     */
    public ApiCommonResult<List<TopsailProject>> getProjectList(){
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add(HttpHeaders.CONTENT_TYPE, HttpConstant.APPLICATION_JSON_UTF8);
        String url = topsailSysConfig.getApiUrl() + UrlContants.Topsail.GET_PROJECT_LIST + "?" + "appkey=" + topsailSysConfig.getAppKey() + "&userid=" + topsailSysConfig.getUserNumber();
        //  执行HTTP请求
        ResponseEntity<ApiCommonResult<List<TopsailProject>>> response = null;
        URI uri = URI.create(url);
        ParameterizedTypeReference<ApiCommonResult<List<TopsailProject>>> reference = new ParameterizedTypeReference<ApiCommonResult<List<TopsailProject>>>() {};
        response = restTemplate.exchange(uri, HttpMethod.GET, null, reference);
        return response.getBody();
    }

    /**
     * 批量查询设备最新数据
     * @return
     */
    public ApiCommonResult<List<TopsailDevice>> getDeviceDatas(Integer projectId){
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add(HttpHeaders.CONTENT_TYPE, HttpConstant.APPLICATION_JSON_UTF8);
//        Map<String,Object> paramMap = new HashMap<>();
//        paramMap.put("projectid", projectId);
        String url = topsailSysConfig.getApiUrl() + UrlContants.Topsail.GET_DEVICES_DATAS + "?"
                + "appkey=" + topsailSysConfig.getAppKey() + "&userid=" + topsailSysConfig.getUserNumber()
                + "&project_id="+projectId;
        //  执行HTTP请求
        ResponseEntity<ApiCommonResult<List<TopsailDevice>>> response = null;
        URI uri = URI.create(url);
        ParameterizedTypeReference<ApiCommonResult<List<TopsailDevice>>> reference = new ParameterizedTypeReference<ApiCommonResult<List<TopsailDevice>>>() {};
        response = this.restTemplate.exchange(uri, HttpMethod.GET, null, reference);
        return response.getBody();
    }

    /**
     * 查询单个设备最新数据
     * @param deviceId
     * @return
     */
    public ApiCommonResult<TopsailDevice> getDeviceByDeviceId(String deviceId){
        String url = topsailSysConfig.getApiUrl() + UrlContants.Topsail.GET_DEVICES_ + deviceId
                + "?" + "appkey=" + topsailSysConfig.getAppKey() + "&userid=" + topsailSysConfig.getUserNumber();
        //  执行HTTP请求
        ResponseEntity<ApiCommonResult<TopsailDevice>> response = null;
        ParameterizedTypeReference<ApiCommonResult<TopsailDevice>> reference = new ParameterizedTypeReference<ApiCommonResult<TopsailDevice>>() {};
        URI uri = URI.create(url);
        response = this.restTemplate.exchange(uri, HttpMethod.GET, null, reference);
        return response.getBody();
    }

    /**
     * 查询单个设备历史数据
     * @param deviceId
     * @return
     */
    public ApiCommonResult<List<TopsailDevice>> getDevicesHistoryByDeviceId(String deviceId){
        String url = topsailSysConfig.getApiUrl() + UrlContants.Topsail.GET_DEVICES_HISTORY_
                + "?" + "appkey=" + topsailSysConfig.getAppKey() + "&userid=" + topsailSysConfig.getUserNumber()
                + "&deviceid=" + deviceId;
        //  执行HTTP请求
        ResponseEntity<ApiCommonResult<List<TopsailDevice>>> response = null;
        ParameterizedTypeReference<ApiCommonResult<List<TopsailDevice>>> reference = new ParameterizedTypeReference<ApiCommonResult<List<TopsailDevice>>>() {};
        URI uri = URI.create(url);
        response = this.restTemplate.exchange(uri, HttpMethod.GET, null, reference);
        return response.getBody();
    }

    /**
     * 批量查询最新预警信息
     * @param projectId
     * @return
     */
    public ApiCommonResult<List<TopsailAlarm>> getDevicesAlarms(Integer projectId){
        String url = topsailSysConfig.getApiUrl() + UrlContants.Topsail.GET_DEVICES_ALARMS
                + "?" + "appkey=" + topsailSysConfig.getAppKey() + "&userid=" + topsailSysConfig.getUserNumber()
                + "&project_id=" + projectId;
        //  执行HTTP请求
        ResponseEntity<ApiCommonResult<List<TopsailAlarm>>> response = null;
        ParameterizedTypeReference<ApiCommonResult<List<TopsailAlarm>>> reference = new ParameterizedTypeReference<ApiCommonResult<List<TopsailAlarm>>>() {};
        URI uri = URI.create(url);
        response = this.restTemplate.exchange(uri, HttpMethod.GET, null, reference);
        return response.getBody();
    }

    /**
     * 单个设备预警历史
     * @param deviceId
     * @return
     */
    public ApiCommonResult<List<TopsailAlarm>> getDevicesAlarmByDeviceId(String deviceId){
        String url = topsailSysConfig.getApiUrl() + UrlContants.Topsail.GET_DEVICES_ALARM_ + deviceId
                + "?" + "appkey=" + topsailSysConfig.getAppKey() + "&userid=" + topsailSysConfig.getUserNumber();
        //  执行HTTP请求
        ResponseEntity<ApiCommonResult<List<TopsailAlarm>>> response = null;
        ParameterizedTypeReference<ApiCommonResult<List<TopsailAlarm>>> reference = new ParameterizedTypeReference<ApiCommonResult<List<TopsailAlarm>>>() {};
        URI uri = URI.create(url);
        response = this.restTemplate.exchange(uri, HttpMethod.GET, null, reference);
        return response.getBody();
    }

    /**
     * 批量设备配置信息
     * @param projectId
     * @param deviceType
     * @return
     */
    public ApiCommonResult<List<TopsailDeviceConfig>> getDevicesConfigs(Integer projectId, Integer deviceType){
        String url = topsailSysConfig.getApiUrl() + UrlContants.Topsail.GET_DEVICES_CONFIGS
                + "?" + "appkey=" + topsailSysConfig.getAppKey() + "&userid=" + topsailSysConfig.getUserNumber()
                + "&project_id=" + projectId + "&deviceType=" + deviceType;
        //  执行HTTP请求
        ResponseEntity<ApiCommonResult<List<TopsailDeviceConfig>>> response = null;
        ParameterizedTypeReference<ApiCommonResult<List<TopsailDeviceConfig>>> reference = new ParameterizedTypeReference<ApiCommonResult<List<TopsailDeviceConfig>>>() {};
        URI uri = URI.create(url);
        response = this.restTemplate.exchange(uri, HttpMethod.GET, null, reference);
        return response.getBody();
    }

    /**
     * 单个设备配置下发
     * @param deviceId
     * @param deviceConfig
     * @return
     */
    public ApiCommonResult<String> sendDevicesConfigByDeviceId(String deviceId, TopsailDeviceConfig deviceConfig){
        String configData = JsonUtils.objectToJson(deviceConfig);
        String url = topsailSysConfig.getApiUrl() + UrlContants.Topsail.SEND_DEVICES_CONFIG_
                + "?" + "appkey=" + topsailSysConfig.getAppKey() + "&userid=" + topsailSysConfig.getUserNumber()
                + "&deviceid=" + deviceId + "&configData="+configData;
        //  执行HTTP请求
        ResponseEntity<ApiCommonResult<String>> response = null;
        ParameterizedTypeReference<ApiCommonResult<String>> reference = new ParameterizedTypeReference<ApiCommonResult<String>>() {};
        URI uri = URI.create(url);
        response = this.restTemplate.exchange(uri, HttpMethod.GET, null, reference);
        return response.getBody();
    }

    /**
     * 某设备配置信息查询
     * @param imei
     * @return
     */
    public ApiCommonResult<TopsailConfigData> getDeviceConfigData(String imei){
        String url = topsailSysConfig.getApiUrl() + UrlContants.Topsail.GET_DEVICES_CONFIGDATA
                + "?" + "appkey=" + topsailSysConfig.getAppKey() + "&userid=" + topsailSysConfig.getUserNumber()
                + "&imei=" + imei;
        //  执行HTTP请求
        ResponseEntity<ApiCommonResult<TopsailConfigData>> response = null;
        ParameterizedTypeReference<ApiCommonResult<TopsailConfigData>> reference = new ParameterizedTypeReference<ApiCommonResult<TopsailConfigData>>() {};
        URI uri = URI.create(url);
        response = this.restTemplate.exchange(uri, HttpMethod.GET, null, reference);
        return response.getBody();
    }
}
