package com.lutao.cloud.auth.sso.util;


import com.alibaba.nacos.common.utils.HttpMethod;
import com.lutao.cloud.common.core.util.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import java.net.URI;
import java.util.*;

@Slf4j
public class SSOSigner {


    /**
     * 构建KIAM签名Header
     *
     * @param url        请求地址
     * @param httpMethod 请求方式
     * @param reqStr     请求报文
     * @return headerMap
     * @throws Exception 异常
     */
    public static Map<String, Object> buildKiamTokenHeader(String url, String httpMethod, String reqStr, String appKey, String appPwd, String signMethod) throws Exception {
        Map<String, Object> headerMap = new HashMap<>(8);
        Long ts = Calendar.getInstance().getTime().getTime();
        String once = RandomStringUtils.randomAlphanumeric(32);
        try {
            headerMap.put("ts", ts);
            headerMap.put("once", once);
            headerMap.put("appKey", appKey);
            headerMap.put("signMethod", signMethod);

            URI uri = new URI(url);
            String query = uri.getQuery();
            if(StringUtils.isNotEmpty(query)) {
                for (String param : query.split("&")) {
                    headerMap.put(param.split("=")[0], param.split("=")[1]);
                }
            }

            StringBuilder sb = new StringBuilder();

            String[] split = url.split("/");
            String id = split[split.length - 1];
            if (httpMethod.equals(HttpMethod.PUT) || httpMethod.equals(HttpMethod.POST)) {
                sb.append("&bodyData=").append(reqStr).append("&signMethod=").append(signMethod);
                if (httpMethod.equalsIgnoreCase(HttpMethod.PUT)) {
                    if (StringUtils.isNotEmpty(id)) {
                        sb.append("&ID=");
                        sb.append(id);
                    }
                }
            } else if (httpMethod.equals(HttpMethod.DELETE)) {
                if (StringUtils.isNotEmpty(reqStr)) {
                    sb.append("&bodyData=").append(reqStr);
                }
                sb.append("&signMethod=").append(signMethod);
                if (StringUtils.isNotEmpty(id)) {
                    sb.append("&ID=");
                    sb.append(id);
                }
            } else {
                for (String key : headerMap.keySet()) {
                    sb.append("&").append(key).append("=").append(headerMap.get(key));
                }

                sb = new StringBuilder(sb.substring(1));
            }

            headerMap.put("signData", TokenUtils.getSignature(appPwd, sb.toString()));
        } catch (Exception e) {
            log.error("构建签名Header异常！", e);
        }
        return headerMap;
    }

    public static void main(String[] args) throws Exception {
        String appPwd = "Cic6H6kAOs/NVF1OoIb5zpJk0rQEXqJDn8luAzhxaxFH7ascFarhIiXj3dursJZW";
        String appKey = "010000000000110080001";
        String signMethod = "SHA-256";
        Map<String, Object> stringObjectMap = buildKiamTokenHeader("http://10.18.127.46:11080/ums/service/V1/res/users?startTime=1990-06-21T08:00:00.000Z", "GET", null, appKey, appPwd, signMethod);
        System.out.println(stringObjectMap);
    }

}