package com.m2pool.lease.utils;

import cn.hutool.core.net.URLEncoder;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.m2pool.common.core.utils.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;

public class WalletRuleCheckUtils {

    /**
     * 校验地址
     * @param chainOrCoin
     * @param address
     * @return
     */
    public static boolean checkAddress(String chainOrCoin, String address) {
        if (address == null) {
            return false;
        }
        chainOrCoin = chainOrCoin.toUpperCase();
        switch (chainOrCoin) {
            case "TRON":
                return checkTRON(address);
            case "ETH":
                return checkETH(address);
            case "XTM":
                return checkXTM(address);
            case "IRON":
                return checkIRON(address);
            case "XEL":
                return checkXEL(address);
            case "RVN":
                return checkRVN(address);
            case "ERG":
                return checkERG(address);
            case "CFX":
                return checkCFX(address);
            case "NEXA":
                return checkNEXA(address);
            case "XNA":
                return checkXNA(address);
            case "KLS":
                return checkKLS(address);
            case "CLORE":
                return checkCLORE(address);
            default:
                return false;
        }
    }

    // ---------------TRON校验-------------------

    private static final String API_KEY = "0925bdd7-7def-4a46-b65f-e17f02dc67c7";

    /**
     * 调用 Tronscan API 获取账户信息
     * @param address 要查询的账户地址
     * @return API 响应结果
     */
    public static boolean checkTRON(String address) {
        String baseUrl = "https://apilist.tronscanapi.com/api/accountv2";
        // 对地址进行 URL 编码
        String encodedAddress = URLEncoder.createDefault().encode(address, StandardCharsets.UTF_8);
        // 构建请求 URL
        String url = baseUrl + "?address=" + encodedAddress;
        // 发送带有请求头的 GET 请求
        HttpResponse response = HttpRequest.get(url)
                .header("TRON-PRO-API-KEY", API_KEY)
                .execute();

        String s = parseResult(response.body());
        if (!StringUtils.isEmpty(s) && "some parameters are invalid or out of range".equals(s)){
            return false;
        }
        return true;
    }

    /**
     * 解析 API 响应结果，获取 result 字段
     * @param response API 响应结果
     * @return result 字段的值
     */
    private static String parseResult(String response) {
        JSONObject jsonObject = JSONUtil.parseObj(response);
        return jsonObject.getStr("message");
    }

    // ---------------TRON校验-------------------



    // ---------------ETH校验 目前只做了基础校验还需要EIP-55 校验-------------------
    private static final Pattern ETH_BASIC_PATTERN = Pattern.compile("^0x[a-fA-F0-9]{40}$");

    /**
     * 基础格式校验以太坊地址
     * @param address 待校验的以太坊地址
     * @return 如果地址格式正确返回 true，否则返回 false
     */
    public static boolean checkETH(String address) {
        if (address == null) {
            return false;
        }
        return ETH_BASIC_PATTERN.matcher(address).matches();
    }
    // ---------------ETH校验-------------------



    // ---------------XTM校验-------------------
    private static final Pattern XTM_PATTERN = Pattern.compile("^P[0-9a-zA-Z]{34}$");
    public static boolean checkXTM(String address) {
        return StringUtils.isNotEmpty( address) && XTM_PATTERN.matcher(address).matches();
    }

    // ---------------IRON校验-------------------
    private static final Pattern IRON_PATTERN = Pattern.compile("^i[0-9a-zA-Z]{33}$");
    public static boolean checkIRON(String address) {
        return StringUtils.isNotEmpty( address) && IRON_PATTERN.matcher(address).matches();
    }

    // ---------------XEL校验-------------------
    private static final Pattern XEL_PATTERN = Pattern.compile("^XEL[0-9a-zA-Z]{33}$");
    public static boolean checkXEL(String address) {
        return StringUtils.isNotEmpty( address) && XEL_PATTERN.matcher(address).matches();
    }

    // ---------------RVN校验-------------------
    private static final Pattern RVN_PATTERN = Pattern.compile("^R[0-9a-zA-Z]{33}$");
    public static boolean checkRVN(String address) {
        return StringUtils.isNotEmpty( address) && RVN_PATTERN.matcher(address).matches();
    }

    // ---------------ERG校验-------------------
    private static final Pattern ERG_PATTERN = Pattern.compile("^9[0-9a-zA-Z]{50}$");
    public static boolean checkERG(String address) {
        return StringUtils.isNotEmpty( address) && ERG_PATTERN.matcher(address).matches();
    }

    // ---------------CFX校验-------------------
    private static final Pattern CFX_PATTERN = Pattern.compile("^cfx:[a-z0-9]{42}$");
    public static boolean checkCFX(String address) {
        return StringUtils.isNotEmpty( address) && CFX_PATTERN.matcher(address).matches();
    }

    // ---------------NEXA校验-------------------
    private static final Pattern NEXA_PATTERN = Pattern.compile("^nexa:[0-9a-zA-Z]{40}$");
    private static final Pattern NEXA_PATTERN2 = Pattern.compile("^[0-9a-zA-Z]{40}$");
    public static boolean checkNEXA(String address) {
       if (address.startsWith("nexa:")){
           return StringUtils.isNotEmpty(address) && NEXA_PATTERN.matcher(address).matches();
       }else{
           return StringUtils.isNotEmpty(address) && NEXA_PATTERN2.matcher(address).matches();
       }
    }

    // ---------------XNA校验-------------------
    private static final Pattern XNA_PATTERN = Pattern.compile("^X[0-9a-zA-Z]{33}$");
    public static boolean checkXNA(String address) {
        return StringUtils.isNotEmpty( address) && XNA_PATTERN.matcher(address).matches();
    }

    // ---------------KLS校验-------------------
    private static final Pattern KLS_PATTERN = Pattern.compile("^k[0-9a-zA-Z]{33}$");
    public static boolean checkKLS(String address) {
        return StringUtils.isNotEmpty( address) && KLS_PATTERN.matcher(address).matches();
    }

    // ---------------CLORE校验-------------------
    private static final Pattern CLORE_PATTERN = Pattern.compile("^C[0-9a-zA-Z]{33}$");
    public static boolean checkCLORE(String address) {
        return StringUtils.isNotEmpty( address) && CLORE_PATTERN.matcher(address).matches();
    }
    public static void main(String[] args) {

    }
}
