import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:provider/provider.dart';

import 'package:pshared/controllers/balance_mask/ledger_accounts.dart';
import 'package:pshared/models/ledger/account.dart';

import 'package:pweb/pages/payout_page/wallet/edit/fields/ledger/balance_formatter.dart';
import 'package:pweb/pages/payout_page/wallet/edit/fields/ledger/balance_row.dart';
import 'package:pweb/pages/payout_page/wallet/edit/fields/shared/copyable_value_row.dart';
import 'package:pweb/widgets/refresh_balance/ledger.dart';


class LedgerSection extends StatelessWidget {
  final LedgerAccount ledger;

  const LedgerSection({super.key, required this.ledger});

  @override
  Widget build(BuildContext context) {
    return Consumer<LedgerBalanceMaskController>(
      builder: (context, balanceMask, _) {
        final isMasked = balanceMask.isBalanceMasked(ledger.ledgerAccountRef);
        final accountCode = ledger.accountCode.trim();
        final hasAccountCode = accountCode.isNotEmpty;
        final balance = isMasked
            ? LedgerBalanceFormatter.formatMasked(ledger)
            : LedgerBalanceFormatter.format(context, ledger);

        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Expanded(
                  child: LedgerBalanceRow(
                    balance: balance,
                    isMasked: isMasked,
                    onToggleMask: () {
                      balanceMask.toggleBalanceMask(ledger.ledgerAccountRef);
                    },
                  ),
                ),
                LedgerBalanceRefreshButton(
                  ledgerAccountRef: ledger.ledgerAccountRef,
                ),
              ],
            ),
            const SizedBox(height: 8),
            CopyableValueRow(
              value: hasAccountCode ? accountCode : '-',
              canCopy: hasAccountCode,
              onCopy: hasAccountCode
                  ? () {
                      Clipboard.setData(ClipboardData(text: accountCode));
                    }
                  : null,
              overflow: TextOverflow.ellipsis,
              wrapValueWithFlexible: true,
            ),
          ],
        );
      },
    );
  }
}
