package com.yihu.hos.rest.common.http;

import com.yihu.hos.core.datatype.StringUtil;
import com.yihu.hos.core.http.HTTPResponse;
import com.yihu.hos.core.http.HttpClientKit;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.ssl.SSLContexts;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.util.Map;
import java.util.Properties;

//TODO:对于Basic认证内容，暂时未验证，可以使用http://user:password@host的方式尝试访问
public class EHRHttpHelper {
    public static String defaultHttpUrl;
    public static String clientId;
    public static String clientKey;
    private static SSLConnectionSocketFactory defaultSSL;
    private static String defaultHttpUser;
    private static String defaultHttpPassword;

    static {
        //默认配置
        try {
            final String defaultPropertiesPath = "config/http.properties";
            Resource resource = new ClassPathResource(defaultPropertiesPath);
            EncodedResource encRes = new EncodedResource(resource, "UTF-8");
            Properties props = PropertiesLoaderUtils.loadProperties(encRes);

            defaultHttpUrl = props.getProperty("httpUrl");
            defaultHttpUser = props.getProperty("httpUser");
            defaultHttpPassword = props.getProperty("httpPassword");
            clientId = props.getProperty("clientId");
            clientKey = props.getProperty("clientKey");
            String sslKeystore = props.getProperty("sslKeystore");
            String sslPassword = props.getProperty("sslPassword");
            if (StringUtil.isEmpty(sslKeystore)) {
                String home = System.getProperty("catalina.home").replace('\\', '/');
                String homeUrl = home.substring(0, home.lastIndexOf('/') + 1);
                // sslKeystore = homeUrl + "tomcat.keystore";
                sslKeystore = "E://tomcat.keystore";
            }

            if (sslKeystore.length() > 0 && sslPassword != null && sslPassword.length() > 0) {
                HttpClientKit.addKeyStore(defaultHttpUrl, sslKeystore, sslPassword);
            }

            SSLContext sslContext = SSLContexts.custom()
                    .loadTrustMaterial(new File(sslKeystore), sslPassword.toCharArray(),
                            new TrustSelfSignedStrategy())
                    .build();
            defaultSSL = new SSLConnectionSocketFactory(
                    sslContext,
                    new String[]{"TLSv1"},
                    null,
                    SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            HttpClientKit.addKeyStoreSSL(defaultHttpUrl, defaultSSL);
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }

    }

    /**************************
     * Get方法
     ******************************************/
    public static HTTPResponse get(String url) {
        return get(url, null);
    }

    public static HTTPResponse get(String url, Map<String, String> params) {
        return get(url, params, null);
    }

    public static HTTPResponse get(String url, Map<String, String> params, Map<String, String> header) {
        return get(url, params, header, false);
    }

    public static HTTPResponse get(String url, Map<String, String> params, Map<String, String> header, Boolean isCheck) {
        if (isCheck) {
            return get(url, params, header, defaultHttpUser, defaultHttpPassword);
        } else {
            return get(url, params, header, null, null);
        }
    }

    public static HTTPResponse get(String url, Map<String, String> params, Map<String, String> header, String user, String password) {
        return HttpClientKit.request("GET", url, params, header);
    }

    /**************************
     * Post方法
     ******************************************/
    public static HTTPResponse post(String url) {
        return post(url, null);
    }

    public static HTTPResponse post(String url, Map<String, String> params) {
        return post(url, params, null);
    }

    public static HTTPResponse post(String url, Map<String, String> params, Map<String, String> header) {
        return post(url, params, header, false);
    }

    public static HTTPResponse post(String url, Map<String, String> params, Map<String, String> header, Boolean isCheck) {
        if (isCheck) {
            return post(url, params, header, defaultHttpUser, defaultHttpPassword);
        } else {
            return post(url, params, header, null, null);
        }
    }

    public static HTTPResponse post(String url, Map<String, String> params, Map<String, String> header, String user, String password) {
        return HttpClientKit.request("POST", url, params, header);
    }

    public static HTTPResponse postFile(String url, Map<String, String> formParams, String filePath, Map<String, String> header) {
        return HttpClientKit.postFile(url, filePath, formParams);
    }

    public static HTTPResponse postFile(String url, Map<String, String> formParams, File file, Map<String, String> header) {
        return HttpClientKit.postFile(url, file.getAbsolutePath(), formParams);
    }

    /**************************
     * Put方法
     ******************************************/
    public static HTTPResponse put(String url) {
        return put(url, null, null);
    }

    public static HTTPResponse put(String url, Map<String, String> params) {
        return put(url, params, null);
    }

    public static HTTPResponse put(String url, Map<String, String> params, Map<String, String> header) {
        return put(url, params, header, false);
    }

    public static HTTPResponse put(String url, Map<String, String> params, Map<String, String> header, Boolean isCheck) {
        if (isCheck) {
            return put(url, params, header, defaultHttpUser, defaultHttpPassword);
        } else {
            return put(url, params, header, null, null);
        }
    }

    public static HTTPResponse put(String url, Map<String, String> params, Map<String, String> header, String user, String password) {
        return HttpClientKit.request("PUT", url, params, header);
    }

    /**************************
     * Delete方法
     **************************************/
    public static HTTPResponse delete(String url) {
        return delete(url, null, null);
    }

    public static HTTPResponse delete(String url, Map<String, String> params) {
        return delete(url, params, null);
    }

    public static HTTPResponse delete(String url, Map<String, String> params, Map<String, String> header) {
        return delete(url, params, header, false);
    }

    public static HTTPResponse delete(String url, Map<String, String> params, Map<String, String> header, Boolean isCheck) {
        if (isCheck) {
            return delete(url, params, header, defaultHttpUser, defaultHttpPassword);
        } else {
            return delete(url, params, header, null, null);
        }
    }

    public static HTTPResponse delete(String url, Map<String, String> params, Map<String, String> header, String user, String password) {

        return HttpClientKit.request("DELETE", url, params, header);
    }
}
