import 'package:flutter/material.dart';

import 'package:pshared/models/ledger/account.dart';
import 'package:pshared/models/payment/type.dart';
import 'package:pshared/models/recipient/recipient.dart';
import 'package:pshared/models/payment/wallet.dart';

import 'package:pweb/models/dashboard/dashboard_payment_mode.dart';
import 'package:pweb/pages/dashboard/buttons/balance/widget.dart';
import 'package:pweb/pages/dashboard/buttons/balance/providers.dart';
import 'package:pweb/pages/dashboard/buttons/buttons.dart';
import 'package:pweb/pages/dashboard/payouts/multiple/widgets/title.dart';
import 'package:pweb/pages/dashboard/payouts/multiple/widget.dart';
import 'package:pweb/pages/dashboard/payouts/single/widget.dart';
import 'package:pweb/pages/loader.dart';

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


class AppSpacing {
  static const double small = 10;
  static const double medium = 16;
  static const double large = 20;
}

class DashboardPage extends StatefulWidget {
  final ValueChanged<Recipient> onRecipientSelected;
  final void Function(PaymentType type) onGoToPaymentWithoutRecipient;
  final ValueChanged<Wallet> onTopUp;
  final ValueChanged<LedgerAccount> onLedgerAddFunds;
  final ValueChanged<Wallet> onWalletTap;
  final ValueChanged<LedgerAccount> onLedgerTap;

  const DashboardPage({
    super.key,
    required this.onRecipientSelected,
    required this.onGoToPaymentWithoutRecipient,
    required this.onTopUp,
    required this.onLedgerAddFunds,
    required this.onWalletTap,
    required this.onLedgerTap,
  });

  @override
  State<DashboardPage> createState() => _DashboardPageState();
}

class _DashboardPageState extends State<DashboardPage> {
  DashboardPayoutMode _payoutMode = DashboardPayoutMode.single;

  void _setActive(DashboardPayoutMode mode) {
    setState(() {
      _payoutMode = mode;
    });
  }

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;
    final showSingle = _payoutMode == DashboardPayoutMode.single;
    final showMultiple = _payoutMode == DashboardPayoutMode.multiple;
    return PageViewLoader(
      child: SafeArea(
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Row(
                children: [
                  Expanded(
                    flex: 0,
                    child: TransactionRefButton(
                      onTap: () => _setActive(DashboardPayoutMode.single),
                      isActive: showSingle,
                      label: l10n.sendSingle,
                      icon: Icons.person_add,
                    ),
                  ),
                  const SizedBox(width: AppSpacing.small),
                  Expanded(
                    flex: 0,
                    child: TransactionRefButton(
                      onTap: () => _setActive(DashboardPayoutMode.multiple),
                      isActive: showMultiple,
                      label: l10n.sendMultiple,
                      icon: Icons.group_add,
                    ),
                  ),
                ],
              ),
              const SizedBox(height: AppSpacing.medium),
              BalanceWidgetProviders(
                child: BalanceWidget(
                  onTopUp: widget.onTopUp,
                  onLedgerAddFunds: widget.onLedgerAddFunds,
                  onWalletTap: widget.onWalletTap,
                  onLedgerTap: widget.onLedgerTap,
                ),
              ),
              const SizedBox(height: AppSpacing.small),
              if (showMultiple) TitleMultiplePayout(),
              const SizedBox(height: AppSpacing.medium),
              if (showSingle)
                SinglePayoutForm(
                  onRecipientSelected: widget.onRecipientSelected,
                  onGoToPayment: widget.onGoToPaymentWithoutRecipient,
                ),
              if (showMultiple) MultiplePayoutForm(),
            ],
          ),
        ),
      ),
    );
  }
}
