package com.sifang.modules.q7link.utils;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.opensdk.BaseRequest;
import com.amazonaws.opensdk.SdkRequestConfig;
import com.amazonaws.opensdk.config.ConnectionConfiguration;
import com.amazonaws.opensdk.config.TimeoutConfiguration;
import com.q7link.openapi.Openapi;
import com.q7link.openapi.model.*;
import com.sifang.modules.sys.common.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Q7linkUtils {

    private static Logger logger = LoggerFactory.getLogger(Q7linkUtils.class);

    private static Openapi openapi;



    /**
     * 根据code获取openid
     */
    public static Openid getCode(String code) {
        GetCodeRequest request = new GetCodeRequest();
        request.sdkRequestConfig(getSdkRequestConfig(request));
        request.setCode(code);
        GetCodeResult result = openapi().getCode(request);
        Openid res = result.getOpenid();
        logger.info(" code: {}, getCode : {}", code, res);
        return res;
    }

    public static String getDataList(String req) {
        PostListRequest request = new PostListRequest();
        ApiParams apiParams = new ApiParams();
        apiParams.setJson(req);
        request.sdkRequestConfig(getSdkRequestConfig(request));
        request.setApiParams(apiParams);
        PostListResult postListResult = openapi().postList(request);
        ApiResponse apiResponse = postListResult.getApiResponse();
        logger.info("apiResponse :{}", apiResponse);
        if (apiResponse == null) {
            return "";
        }
        String result = apiResponse.getJson();
        if (StringUtil.isBlank(result)) {
            return "";
        }
//        JsonObject json = new Gson().fromJson(result, JsonObject.class);
        return result;
    }



    public static Openapi openapi() {
        if (openapi == null) {
            BasicAWSCredentials awsCredentials = new BasicAWSCredentials(Q7linkConstants.ACCESS_KEY_ID,
                    Q7linkConstants.ACCESS_SECRET);
            openapi = Openapi.builder()
                    .connectionConfiguration(new ConnectionConfiguration()
                            .maxConnections(100)
                            .connectionMaxIdleMillis(10000))
                    .timeoutConfiguration(new TimeoutConfiguration()
                            .httpRequestTimeout(29000)
                            .totalExecutionTimeout(29000)
                            .socketTimeout(29000))
                    .iamCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                    .build();
        }
        return openapi;
    }

    public static SdkRequestConfig getSdkRequestConfig(BaseRequest request) {
        return getSdkRequestConfig(request, false);
    }

    /**
     * 获取sdk请求配置
     *
     * @param request    请求
     * @param ignoreWarn 是否忽略警告：true为忽略
     * @return SdkRequestConfig
     */
    public static SdkRequestConfig getSdkRequestConfig(BaseRequest request, boolean ignoreWarn) {
        return getSdkRequestConfig(request, ignoreWarn, false);
    }

    /**
     * 获取sdk请求配置
     * <pre>
     *   支持设置忽略警告
     *   支持单据保存为草稿
     * </pre>
     *
     * @param request     请求
     * @param ignoreWarn  是否忽略警告：true为忽略
     * @param saveAsDraft 是否保存为草稿：true为保存为草稿
     * @return SdkRequestConfig
     */
    public static SdkRequestConfig getSdkRequestConfig(BaseRequest request, boolean ignoreWarn, boolean saveAsDraft) {
        return request.sdkRequestConfig().copyBuilder()
                .customHeader("Content-Type", "application/json")
                .customHeader("Access-Key-Id", Q7linkConstants.ACCESS_KEY_ID)
                .customHeader("Open-Id", Q7linkConstants.OPEN_ID)
                .customHeader("Ignore-Warn", String.valueOf(ignoreWarn))
                .customHeader("Save-As-Draft", String.valueOf(saveAsDraft))
                .build();
    }


}
