
package com.sf.oauth.utils;

import cn.hutool.core.util.StrUtil;
import com.sf.oauth.exception.AuthException;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AuthUtils {
    private static final Charset DEFAULT_ENCODING;
    private static final String HMAC_SHA1 = "HmacSHA1";
    private static final String HMAC_SHA_256 = "HmacSHA256";

    public AuthUtils() {
    }

    public static String urlEncode(String value) {
        if (value == null) {
            return "";
        } else {
            try {
                String encoded = URLEncoder.encode(value, DEFAULT_ENCODING.displayName());
                return encoded.replace("+", "%20").replace("*", "%2A").replace("~", "%7E").replace("/", "%2F");
            } catch (UnsupportedEncodingException var2) {
                throw new AuthException("Failed To Encode Uri", var2);
            }
        }
    }

    public static String urlDecode(String value) {
        if (value == null) {
            return "";
        } else {
            try {
                return URLDecoder.decode(value, DEFAULT_ENCODING.displayName());
            } catch (UnsupportedEncodingException var2) {
                throw new AuthException("Failed To Decode Uri", var2);
            }
        }
    }


    public static String parseMapToString(Map<String, String> params, boolean encode) {
        if (null != params && !params.isEmpty()) {
            List<String> paramList = new ArrayList();
            params.forEach((k, v) -> {
                if (null == v) {
                    paramList.add(k + "=");
                } else {
                    paramList.add(k + "=" + (encode ? urlEncode(v) : v));
                }

            });
            return String.join("&", paramList);
        } else {
            return "";
        }
    }

    public static boolean isHttpProtocol(String url) {
        if (StrUtil.isEmpty(url)) {
            return false;
        } else {
            return url.startsWith("http://") || url.startsWith("http%3A%2F%2F");
        }
    }

    public static boolean isHttpsProtocol(String url) {
        if (StrUtil.isEmpty(url)) {
            return false;
        } else {
            return url.startsWith("https://") || url.startsWith("https%3A%2F%2F");
        }
    }

    public static boolean isLocalHost(String url) {
        return StrUtil.isEmpty(url) || url.contains("127.0.0.1") || url.contains("localhost");
    }

    public static boolean isHttpsProtocolOrLocalHost(String url) {
        if (StrUtil.isEmpty(url)) {
            return false;
        } else {
            return isHttpsProtocol(url) || isLocalHost(url);
        }
    }


    public static String getTimestamp() {
        return String.valueOf(System.currentTimeMillis() / 1000L);
    }


    public static String md5(String str) {
        MessageDigest md = null;
        StringBuilder buffer = null;

        try {
            md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes(StandardCharsets.UTF_8));
            byte[] byteData = md.digest();
            buffer = new StringBuilder();
            byte[] var4 = byteData;
            int var5 = byteData.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                byte byteDatum = var4[var6];
                buffer.append(Integer.toString((byteDatum & 255) + 256, 16).substring(1));
            }
        } catch (Exception var8) {
        }

        return null == buffer ? "" : buffer.toString();
    }

    static {
        DEFAULT_ENCODING = StandardCharsets.UTF_8;
    }
}
