import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/controllers/balance_mask/wallets.dart';
import 'package:pshared/models/ledger/account.dart';
import 'package:pshared/models/payment/chain_network.dart';
import 'package:pshared/models/payment/source_type.dart';
import 'package:pshared/models/payment/wallet.dart';
import 'package:pshared/utils/l10n/chain.dart';

import 'package:pweb/controllers/dashboard/balance/source_copy.dart';
import 'package:pweb/models/state/visibility.dart';
import 'package:pweb/pages/dashboard/buttons/balance/amount.dart';
import 'package:pweb/pages/dashboard/buttons/balance/ledger_amount.dart';
import 'package:pweb/pages/dashboard/buttons/balance/source/actions/ledger.dart';
import 'package:pweb/pages/dashboard/buttons/balance/source/actions/wallet.dart';
import 'package:pweb/pages/dashboard/buttons/balance/source/card_layout.dart';
import 'package:pweb/widgets/refresh_balance/ledger.dart';
import 'package:pweb/widgets/refresh_balance/wallet.dart';

import 'package:pweb/generated/i18n/app_localizations.dart';


class BalanceSourceCard extends StatelessWidget {
  final PaymentSourceType _type;
  final Wallet? _wallet;
  final LedgerAccount? _ledgerAccount;
  final VoidCallback onTap;
  final VoidCallback onAddFunds;
  static const BalanceSourceCopyController _copyController =
      BalanceSourceCopyController();

  const BalanceSourceCard.wallet({
    super.key,
    required Wallet wallet,
    required this.onTap,
    required this.onAddFunds,
  }) : _type = PaymentSourceType.wallet,
       _wallet = wallet,
       _ledgerAccount = null;

  const BalanceSourceCard.ledger({
    super.key,
    required LedgerAccount account,
    required this.onTap,
    required this.onAddFunds,
  }) : _type = PaymentSourceType.ledger,
       _wallet = null,
       _ledgerAccount = account;

  @override
  Widget build(BuildContext context) => switch (_type) {
    PaymentSourceType.wallet => _buildWalletCard(context, _wallet!),
    PaymentSourceType.ledger => _buildLedgerCard(context, _ledgerAccount!),
  };

  Widget _buildWalletCard(BuildContext context, Wallet wallet) {
    final networkLabel =
        (wallet.network == null || wallet.network == ChainNetwork.unspecified)
        ? null
        : wallet.network!.localizedName(context);
    final symbol = wallet.tokenSymbol?.trim();
    final copyState = _copyController.wallet(context, wallet.depositAddress);

    return BalanceSourceCardLayout(
      title: wallet.name,
      subtitle: networkLabel,
      badge: (symbol == null || symbol.isEmpty) ? null : symbol,
      onTap: null,
      copyLabel: copyState.label,
      canCopy: copyState.canCopy,
      onCopy: copyState.canCopy
          ? () async {
              final copied = await _copyController.copy(copyState);
              if (!copied || !context.mounted) return;
              final loc = AppLocalizations.of(context)!;
              ScaffoldMessenger.of(
                context,
              ).showSnackBar(SnackBar(content: Text(loc.addressCopied)));
            }
          : null,
      refreshButton: WalletBalanceRefreshButton(
        walletRef: wallet.id,
        iconOnly: VisibilityState.hidden,
      ),
      actions: WalletSourceActions(
        walletRef: wallet.id,
        onAddFunds: onAddFunds,
      ),
      amount: BalanceAmount(
        wallet: wallet,
        onToggleMask: () {
          context.read<WalletsController>().toggleBalanceMask(wallet.id);
        },
      ),
    );
  }

  Widget _buildLedgerCard(BuildContext context, LedgerAccount account) {
    final loc = AppLocalizations.of(context)!;
    final accountName = account.name.trim();
    final accountCode = account.accountCode.trim();
    final title = accountName.isNotEmpty ? accountName : loc.paymentTypeLedger;
    final badge = account.currency.trim().isEmpty
        ? null
        : account.currency.toUpperCase();
    final copyState = _copyController.ledger(context, accountCode);

    return BalanceSourceCardLayout(
      title: title,
      subtitle: null,
      badge: badge,
      onTap: onTap,
      copyLabel: copyState.label,
      canCopy: copyState.canCopy,
      onCopy: copyState.canCopy
          ? () async {
              final copied = await _copyController.copy(copyState);
              if (!copied || !context.mounted) return;
              ScaffoldMessenger.of(
                context,
              ).showSnackBar(SnackBar(content: Text(loc.addressCopied)));
            }
          : null,
      refreshButton: LedgerBalanceRefreshButton(
        ledgerAccountRef: account.ledgerAccountRef,
        iconOnly: VisibilityState.hidden,
      ),
      actions: LedgerSourceActions(
        ledgerAccountRef: account.ledgerAccountRef,
        onAddFunds: onAddFunds,
        onWalletDetails: onTap,
      ),
      amount: LedgerBalanceAmount(account: account),
    );
  }
}
