package com.liquidnet.service.adam.support;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

/**
 * 静默登录 v2：otp = hex(HMAC-SHA256(secret, mobile))，与 v1 DES 隔离。A/B 两侧均不带时间戳。
 */
@Component
public class SilentMobileOtpV2Support {

    private static final String HMAC_SHA256 = "HmacSHA256";

    @Value("${liquidnet.adam.silent-otp-v2-secret:}")
    private String secret;

    public boolean verify(String mobile, String otp) {
        if (StringUtils.isBlank(secret) || StringUtils.isBlank(mobile) || StringUtils.isBlank(otp)) {
            return false;
        }
        if (!otp.matches("^[0-9a-fA-F]{64}$")) {
            return false;
        }
        byte[] otpBytes = hexToBytes(otp);
        if (otpBytes == null) {
            return false;
        }
        byte[] expected = sign(mobile);
        return expected != null && MessageDigest.isEqual(expected, otpBytes);
    }

    public String signHex(String mobile) {
        byte[] raw = sign(mobile);
        return raw == null ? null : bytesToHex(raw);
    }

    private byte[] sign(String mobile) {
        try {
            Mac mac = Mac.getInstance(HMAC_SHA256);
            mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), HMAC_SHA256));
            return mac.doFinal(mobile.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            return null;
        }
    }

    private static byte[] hexToBytes(String hex) {
        if (hex.length() % 2 != 0) {
            return null;
        }
        byte[] out = new byte[hex.length() / 2];
        for (int i = 0; i < hex.length(); i += 2) {
            out[i / 2] = (byte) Integer.parseInt(hex.substring(i, i + 2), 16);
        }
        return out;
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            sb.append(String.format("%02x", b & 0xff));
        }
        return sb.toString();
    }
}
