import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/controllers/payment/source.dart';

import 'package:pweb/models/payment/source_funds.dart';
import 'package:pweb/pages/payout_page/send/widgets/method_selector.dart';
import 'package:pweb/utils/dimensions.dart';
import 'package:pweb/widgets/payment/source_of_funds_panel.dart';
import 'package:pweb/widgets/refresh_balance/ledger.dart';
import 'package:pweb/widgets/refresh_balance/wallet.dart';


class PaymentSourceOfFundsCard extends StatelessWidget {
  final AppDimensions dimensions;
  final String title;

  const PaymentSourceOfFundsCard({
    super.key,
    required this.dimensions,
    required this.title,
  });

  @override
  Widget build(BuildContext context) {
    return SourceOfFundsPanel(
      title: title,
      selectorSpacing: dimensions.paddingSmall,
      sourceSelector: const PaymentMethodSelector(),
      visibleStates: const <SourceOfFundsVisibleState>{
        SourceOfFundsVisibleState.headerAction,
      },
      stateWidgets: <SourceOfFundsVisibleState, Widget>{
        SourceOfFundsVisibleState
            .headerAction: Consumer<PaymentSourceController>(
          builder: (context, provider, _) {
            final selectedWallet = provider.selectedWallet;
            if (selectedWallet != null) {
              return WalletBalanceRefreshButton(walletRef: selectedWallet.id);
            }

            final selectedLedger = provider.selectedLedgerAccount;
            if (selectedLedger != null) {
              return LedgerBalanceRefreshButton(
                ledgerAccountRef: selectedLedger.ledgerAccountRef,
              );
            }

            return const SizedBox.shrink();
          },
        ),
      },
    );
  }
}
