package com.ywt.outpatient.rpc.handler;

import com.ywt.biz.common.util.Checker;
import com.ywt.biz.common.util.HttpUtil;
import com.ywt.biz.common.util.serializers.JsonSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

/**
 * @author daiyihua
 * @create 2018-0904 上午09:27
 * @desc 调用 nutrimeal api抽出来的公共方法
 **/
@Service
public class RestCallerApiProvider {

    @Value("${com.ywt.restServiceMap}")
    private String restServiceMap;
    private final Logger logger = LoggerFactory.getLogger(RestCallerApiProvider.class);

    @Autowired
    private RestTemplateHandler restTemplateHandler;
    private static final String alipayMpRestServiceName = "com.ywt.AlipayMpRestService";

    public HttpUtil.Resp postAlipayMpRestApi(String path, Map<String, Object> map) {
        return postCommon(alipayMpRestServiceName, path, map);
    }
    public HttpUtil.Resp getAlipayMpRestApi(String path, Map<String, String> map) {
        return getCommon(alipayMpRestServiceName, path, map);
    }

    /**
     * 通用 post 方法
     *
     * @param serverName 服务名
     * @param path       路径
     * @param map        参数
     * @return {@link HttpUtil.Resp}
     */
    private HttpUtil.Resp postCommon(String serverName, String path, Map<String, Object> map) {

        Map<String, String> headers = new HashMap<>(16);
        headers.put("Content-Type", "application/json;charset=UTF-8");

        String ipPort = getRestIpPort(serverName);
        if (!Checker.isNone(ipPort)){
            serverName = ipPort;
        }
        logger.info("RestCallerApiProvider>>postCommon(serverName:{}, \tpath:{}, \tmap:{}, \theaders:{})", serverName, path, JsonSerializer.toJson(map), JsonSerializer.toJson(headers));

        RestTemplateHandler.Resp resp = restTemplateHandler.httpPost(serverName, path, map, headers);
        HttpUtil.Resp r = new HttpUtil.Resp();
        r.setCode(resp.getCode());
        r.setContent(resp.getContent());
        return r;
    }

    /**
     * 通用 get 方法
     *
     * @param serverName 服务名
     * @param path       路径
     * @param map        参数
     * @return {@link HttpUtil.Resp}
     */
    private HttpUtil.Resp getCommon(String serverName, String path, Map<String, String> map) {

        String ipPort = getRestIpPort(serverName);
        if (!Checker.isNone(ipPort)){
            serverName = ipPort;
        }
        logger.info("RestCallerApiProvider>>getCommon(serverName:{}, \tpath:{}, \tmap:{})", serverName, path, JsonSerializer.toJson(map));
        RestTemplateHandler.Resp resp = restTemplateHandler.httpGet(serverName, path, map, null);
        HttpUtil.Resp r = new HttpUtil.Resp();
        r.setCode(resp.getCode());
        r.setContent(resp.getContent());
        return r;
    }


    public String getRestServiceMap() {
        return restServiceMap;
    }
    public String getRestIpPort(String restName) {
        String restIpPort = "";
        String restServiceMapParam = getRestServiceMap();
        try {
            if (!Checker.isNone(restServiceMapParam)) {
                List list = JsonSerializer.from(restServiceMapParam, List.class);
                if (!Checker.isNone(list)) {
                    for (Object o : list) {
                        Map<String, Object> map = (Map<String, Object>) o;
                        String restNameValue = (String) map.getOrDefault("restName", "");
                        String restIpPortValue = (String) map.getOrDefault("restIpPort", "");
                        if (!Checker.isNone(restNameValue) && restName.equals(restNameValue)) {
                            return restIpPortValue;
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("RestCallerApiProvider#getRestServiceMap(restServiceMapParam{}参数有误,{})", restServiceMapParam, e.getMessage(), e);
        }
        return restIpPort;
    }
}
