import 'package:decimal/decimal.dart';

/// 充值钱包（含充值地址） - POST uc/asset/wallet/address
class DepositWallet {
  final String id;
  final Decimal balance;
  final String address;
  final WalletCoin? coin;

  DepositWallet({
    required this.id,
    required this.balance,
    required this.address,
    this.coin,
  });

  factory DepositWallet.fromJson(Map<String, dynamic> json) {
    return DepositWallet(
      id: json['id']?.toString() ?? '',
      balance: Decimal.tryParse(json['balance']?.toString() ?? '0') ?? Decimal.zero,
      address: json['address']?.toString() ?? '',
      coin: json['coin'] != null
          ? WalletCoin.fromJson(json['coin'] as Map<String, dynamic>)
          : null,
    );
  }
}

/// 币种信息
class WalletCoin {
  /// 币种全称（JSON: name）
  final String fullName;

  /// 币种代号（JSON: unit）— 用于区分网络（TUSDT/EUSDT/USDT-BEP20）
  final String code;

  /// 中文名称（JSON: nameCn）— 显示名（USDT-TRC20）
  final String name;

  /// 是否支持充值 "0"/"1"
  final String canRecharge;

  /// 是否支持提现 "0"/"1"
  final String canWithdraw;

  /// 最小充值额
  final Decimal minRechargeAmount;

  /// 最大充值额
  final Decimal maxRechargeAmount;

  /// 最小提现额
  final Decimal minWithdrawAmount;

  /// 最大提现额
  final Decimal maxWithdrawAmount;

  /// 最小手续费
  final Decimal minTxFee;

  /// 最大手续费
  final Decimal maxTxFee;

  /// 提现手续费（JSON: withdrawFeeValue）
  final Decimal fee;

  WalletCoin({
    this.fullName = '',
    this.code = '',
    this.name = '',
    this.canRecharge = '0',
    this.canWithdraw = '0',
    Decimal? minRechargeAmount,
    Decimal? maxRechargeAmount,
    Decimal? minWithdrawAmount,
    Decimal? maxWithdrawAmount,
    Decimal? minTxFee,
    Decimal? maxTxFee,
    Decimal? fee,
  })  : minRechargeAmount = minRechargeAmount ?? Decimal.zero,
        maxRechargeAmount = maxRechargeAmount ?? Decimal.zero,
        minWithdrawAmount = minWithdrawAmount ?? Decimal.zero,
        maxWithdrawAmount = maxWithdrawAmount ?? Decimal.zero,
        minTxFee = minTxFee ?? Decimal.zero,
        maxTxFee = maxTxFee ?? Decimal.zero,
        fee = fee ?? Decimal.zero;

  /// 是否支持充值
  bool get isRechargeEnabled => canRecharge == '1';

  /// 是否支持提现
  bool get isWithdrawEnabled => canWithdraw == '1';

  /// 网络类型显示名
  String get networkName {
    if (code == 'TUSDT') return 'TRC20';
    if (code == 'EUSDT') return 'ERC20';
    if (code.contains('BEP20')) return 'BEP20';
    return code;
  }

  factory WalletCoin.fromJson(Map<String, dynamic> json) {
    return WalletCoin(
      fullName: json['name']?.toString() ?? '',
      code: json['unit']?.toString() ?? '',
      name: json['nameCn']?.toString() ?? '',
      canRecharge: json['canRecharge']?.toString() ?? '0',
      canWithdraw: json['canWithdraw']?.toString() ?? '0',
      minRechargeAmount: Decimal.tryParse(json['minRechargeAmount']?.toString() ?? ''),
      maxRechargeAmount: Decimal.tryParse(json['maxRechargeAmount']?.toString() ?? ''),
      minWithdrawAmount: Decimal.tryParse(json['minWithdrawAmount']?.toString() ?? ''),
      maxWithdrawAmount: Decimal.tryParse(json['maxWithdrawAmount']?.toString() ?? ''),
      minTxFee: Decimal.tryParse(json['minTxFee']?.toString() ?? ''),
      maxTxFee: Decimal.tryParse(json['maxTxFee']?.toString() ?? ''),
      fee: Decimal.tryParse(json['withdrawFeeValue']?.toString() ?? ''),
    );
  }
}
