import 'package:flutter/material.dart';

import 'package:go_router/go_router.dart';

import 'package:provider/provider.dart';

import 'package:pshared/controllers/payment/source.dart';
import 'package:pshared/models/payment/source_type.dart';
import 'package:pshared/models/payment/type.dart';
import 'package:pshared/provider/recipient/provider.dart';

import 'package:pweb/app/router/payout_routes.dart';

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


class TopUpButton extends StatelessWidget {
  const TopUpButton({super.key});

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    final source = context.watch<PaymentSourceController>();
    final selectedType = source.selectedType;
    final selectedLedger = source.selectedLedgerAccount;
    final canTopUp =
        selectedType == PaymentSourceType.wallet ||
        (selectedType == PaymentSourceType.ledger && selectedLedger != null);

    return ElevatedButton(
      style: ElevatedButton.styleFrom(shadowColor: null, elevation: 0),
      onPressed: !canTopUp
          ? null
          : () {
              if (selectedType == PaymentSourceType.wallet) {
                context.pushToWalletTopUp();
                return;
              }

              if (selectedType == PaymentSourceType.ledger &&
                  selectedLedger != null) {
                context.read<RecipientsProvider>().setCurrentObject(null);
                context.pushNamed(
                  PayoutRoutes.payment,
                  queryParameters: PayoutRoutes.buildQueryParameters(
                    paymentType: PaymentType.ledger,
                    destinationLedgerAccountRef:
                        selectedLedger.ledgerAccountRef,
                  ),
                );
              }
            },
      child: Text(loc.topUpBalance),
    );
  }
}
