package com.m2pool.lease.constant;

import com.m2pool.lease.dto.ChargeDto;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 币种手续费
 * @Date 2025/10/23 11:15
 * @Author yyb
 */
public enum CoinCharge {

    ETH_USDT("ETH","USDT",   BigDecimal.valueOf(1), BigDecimal.valueOf(1)),
    ETH_ETH("ETH","ETH",   BigDecimal.valueOf(0.00005),BigDecimal.valueOf(0.01)),
    TRON_USDT("TRON","USDT",   BigDecimal.valueOf(1),BigDecimal.valueOf(1)),
    TRON_NEXA("TRON","NEXA",  BigDecimal.valueOf(1000),BigDecimal.valueOf(0.1));

    private final String chain;
    /** 币种参数名 */
    private final String coin;
    /** 手续费 */
    private final BigDecimal amount;
    /** 费率 */
    private final BigDecimal feeRate;

    CoinCharge(String chain, String coin, BigDecimal amount, BigDecimal feeRate) {
        this.chain = chain;
        this.coin = coin;
        this.amount = amount;
        this.feeRate = feeRate;
    }
    /**
     * 根据 chain 和 coin 查找对应的手续费，未找到则返回 1
     * @param chain 链名
     * @param coin 币种名
     * @return 对应的手续费，未找到返回 1
     */
    public static BigDecimal getChargeByChainAndCoin(String chain, String coin) {
        for (CoinCharge charge : CoinCharge.values()) {
            if (charge.chain.equals(chain) && charge.coin.equals(coin)) {
                return charge.amount;
            }
        }
        return BigDecimal.ONE;
    }

    /**
     * 根据 chain 和 coin 查找对应的手续费，未找到则返回 1
     * @param chain 链名
     * @param coin 币种名
     * @return 对应的手续费，未找到返回 1
     */
    public static BigDecimal getDeductibleAmountByChainAndCoin(String chain, String coin) {
        for (CoinCharge charge : CoinCharge.values()) {
            if (charge.chain.equals(chain) && charge.coin.equals(coin)) {
                return charge.amount.divide(charge.feeRate, 4, RoundingMode.HALF_UP);
            }
        }
        return BigDecimal.ONE;
    }


    /**
     * 根据 chain 和 coin  找到手续费和费率，然后比较totalPrice是否小于 费率/手续费
     * @param chain 链名
     * @param coin 币种名
     * @return 对应的手续费，未找到返回 1
     */
    public static BigDecimal getFee(String chain, String coin, BigDecimal totalPrice) {
        BigDecimal fee = BigDecimal.ONE;
        for (CoinCharge charge : CoinCharge.values()) {
            if (charge.chain.equals(chain) && charge.coin.equals(coin)) {
              BigDecimal feeRate = charge.feeRate;
              BigDecimal deductible= charge.amount.divide(feeRate, 4, RoundingMode.HALF_UP);
              if(deductible.compareTo(totalPrice) < 0){
                  fee = BigDecimal.ZERO;
              }else{
                  fee = charge.amount;
              }
            }
        }
        return fee;
    }


    /**
     * 获取枚举类中所有枚举，并封装成 List<ChargeDto>
     * @return 包含所有枚举信息的 ChargeDto 列表
     */
    public static List<ChargeDto> getAllChargesAsDtoList() {
        List<ChargeDto> chargeDtoList = new ArrayList<>();
        for (CoinCharge charge : CoinCharge.values()) {
            chargeDtoList.add(new ChargeDto(
                    charge.amount,
                    charge.chain,
                    charge.coin,
                    charge.feeRate
            ));
        }
        return chargeDtoList;
    }


}
