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/type.dart';

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

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


class BalanceActionButtonState {
  final String label;
  final IconData icon;
  final VoidCallback onPressed;

  const BalanceActionButtonState({
    required this.label,
    required this.icon,
    required this.onPressed,
  });
}

class BalanceActionsState {
  final BalanceActionButtonState topLeading;
  final BalanceActionButtonState topTrailing;
  final BalanceActionButtonState bottom;

  const BalanceActionsState({
    required this.topLeading,
    required this.topTrailing,
    required this.bottom,
  });
}

class BalanceSourceActionsController {
  const BalanceSourceActionsController();

  BalanceActionsState wallet({
    required BuildContext context,
    required String walletRef,
    required VoidCallback onAddFunds,
  }) {
    final l10n = AppLocalizations.of(context)!;
    return BalanceActionsState(
      topLeading: BalanceActionButtonState(
        label: l10n.operationfryTitle,
        icon: Icons.history_rounded,
        onPressed: () => _openWalletOperationHistory(context, walletRef),
      ),
      topTrailing: BalanceActionButtonState(
        label: l10n.send,
        icon: Icons.send_rounded,
        onPressed: () => _sendWalletPayout(context, walletRef),
      ),
      bottom: BalanceActionButtonState(
        label: '${l10n.details} / ${l10n.addFunds}',
        icon: Icons.account_balance_wallet_rounded,
        onPressed: onAddFunds,
      ),
    );
  }

  BalanceActionsState ledger({
    required BuildContext context,
    required String ledgerAccountRef,
    required VoidCallback onAddFunds,
    required VoidCallback onWalletDetails,
  }) {
    final l10n = AppLocalizations.of(context)!;
    return BalanceActionsState(
      topLeading: BalanceActionButtonState(
        label: '${l10n.operationfryTitle} / ${l10n.details}',
        icon: Icons.receipt_long_rounded,
        onPressed: onWalletDetails,
      ),
      topTrailing: BalanceActionButtonState(
        label: l10n.send,
        icon: Icons.send_rounded,
        onPressed: () => _sendLedgerPayout(context, ledgerAccountRef),
      ),
      bottom: BalanceActionButtonState(
        label: l10n.addFunds,
        icon: Icons.add_card_rounded,
        onPressed: onAddFunds,
      ),
    );
  }

  void _openWalletOperationHistory(BuildContext context, String walletRef) {
    _withSelectedWallet(
      context,
      walletRef,
      () => context.pushNamed(PayoutRoutes.editWallet),
    );
  }

  void _sendWalletPayout(BuildContext context, String walletRef) {
    _withSelectedWallet(
      context,
      walletRef,
      () => context.pushNamed(
        PayoutRoutes.payment,
        queryParameters: PayoutRoutes.buildQueryParameters(
          paymentType: PaymentType.wallet,
        ),
      ),
    );
  }

  void _sendLedgerPayout(BuildContext context, String ledgerAccountRef) {
    context.read<PaymentSourceController>().selectLedgerByRef(ledgerAccountRef);
    context.pushNamed(
      PayoutRoutes.payment,
      queryParameters: PayoutRoutes.buildQueryParameters(
        paymentType: PaymentType.ledger,
      ),
    );
  }

  void _withSelectedWallet(
    BuildContext context,
    String walletRef,
    VoidCallback action,
  ) {
    context.read<PaymentSourceController>().selectWalletByRef(walletRef);
    action();
  }
}
