import 'package:flutter/material.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/pages/dashboard/buttons/balance/balance.dart';
import 'package:pweb/pages/dashboard/buttons/buttons.dart';
import 'package:pweb/pages/dashboard/payouts/multiple/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;

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

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

class _DashboardPageState extends State<DashboardPage> {
  bool _showContainerSingle = true;
  bool _showContainerMultiple = false;

  void _setActive(bool single) {
    setState(() {
      _showContainerSingle = single;
      _showContainerMultiple = !single;
    });
  }

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