package com.gyee.wisdom.xfai.net3.transport;


import com.gyee.wisdom.xfai.net3.config.ConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class DataAdapterClient {

    @Autowired
    private ConfigProperties configProperties;

    private RestTemplate restTemplate;

    private String adpUrl;

    private String getAdpUrl() {
        if (adpUrl == null) {
            adpUrl = configProperties.getServiceUrl();
        }

        return adpUrl;
    }

    private String adpLatestUrl;

    private String getAdpLatestUrl() {
        if (adpLatestUrl == null) {
            adpLatestUrl = getAdpUrl() + "/latest?keys=";
        }
        return adpLatestUrl;
    }

    private RestTemplate getRestTemplate() {
        if (restTemplate == null) {
            //复杂构造函数的使用
            SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
            requestFactory.setConnectTimeout(10000);// 设置超时
            requestFactory.setReadTimeout(10000);
            restTemplate = new RestTemplate(requestFactory);
        }

        return restTemplate;
    }


    public Object getLatest(String tagId) throws Exception {

        String url = getAdpLatestUrl() + tagId;
        ResponseEntity<Object> resp = getRestTemplate().getForEntity(url, Object.class);
        if (resp.getStatusCode() == HttpStatus.OK) {
            HashMap<String, HashMap<String, Object>> map = (HashMap<String, HashMap<String, Object>>) resp.getBody();
            if (map != null && map.size() > 0) {
                for (Map.Entry<String, HashMap<String, Object>> entry : map.entrySet()) {
                    try {
                        return entry.getValue().get("doubleValue");
                    } catch (Exception ex) {
                        log.error(ex.getMessage());
                        //System.out.println(ex.getMessage());
                    }
                }
            }
        } else {
            log.error(resp.getStatusCodeValue() + "");
        }

        return null;
    }
    public  HashMap<String,Integer> getRun(String tagId) {
        HashMap<String,Integer> runing = new HashMap<>();
        String url = getAdpLatestUrl() + tagId;
        Object resp = getRestTemplate().getForObject(url, Object.class);
        if(resp!=null){
            HashMap<String, HashMap<String, Object>> map = (HashMap<String, HashMap<String, Object>>) resp;
            map.forEach((key, value) -> {
                String obj=value.get("doubleValue").toString();
                int ts=Integer.parseInt(obj.substring(0,obj.indexOf(".")));
                runing.put(key,ts);
            });
            return runing;
        }
        return null;
    }
}
