import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

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

import 'package:pweb/controllers/payouts/multiple_payouts.dart';
import 'package:pweb/controllers/payouts/payout_verification.dart';
import 'package:pweb/models/payment/source_funds.dart';
import 'package:pweb/pages/dashboard/payouts/multiple/actions.dart';
import 'package:pweb/pages/dashboard/payouts/multiple/panels/source_quote/summary.dart';
import 'package:pweb/pages/dashboard/payouts/multiple/widgets/quote_status.dart';
import 'package:pweb/pages/payout_page/send/widgets/send_button.dart';
import 'package:pweb/widgets/payment/source_of_funds_panel.dart';
import 'package:pweb/widgets/payment/source_wallet_selector/view.dart';
import 'package:pweb/widgets/cooldown_hint.dart';
import 'package:pweb/widgets/refresh_balance/wallet.dart';
import 'package:pweb/models/state/control_state.dart';

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

class SourceQuotePanel extends StatelessWidget {
  const SourceQuotePanel({super.key, required this.controller});

  final MultiplePayoutsController controller;

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;
    final sourceController = context.watch<PaymentSourceController>();
    final verificationController = context
        .watch<PayoutVerificationController>();
    final quotationProvider = context.watch<MultiQuotationProvider>();
    final verificationContextKey =
        quotationProvider.quotation?.quoteRef ??
        quotationProvider.quotation?.idempotencyKey;
    final isCooldownActive = verificationController.isCooldownActiveFor(
      verificationContextKey,
    );
    final canSend = controller.canSend && !isCooldownActive;
    return SourceOfFundsPanel(
      title: l10n.sourceOfFunds,
      sourceSelector: SourceWalletSelector(
        sourceController: sourceController,
        isBusy: controller.isBusy,
      ),
      visibleStates: const <SourceOfFundsVisibleState>{
        SourceOfFundsVisibleState.headerAction,
        SourceOfFundsVisibleState.summary,
        SourceOfFundsVisibleState.quoteStatus,
        SourceOfFundsVisibleState.sendAction,
      },
      stateWidgets: <SourceOfFundsVisibleState, Widget>{
        SourceOfFundsVisibleState.headerAction: _MultipleRefreshAction(
          sourceController: sourceController,
        ),
        SourceOfFundsVisibleState.summary: SourceQuoteSummary(
          controller: controller,
          spacing: 12,
        ),
        SourceOfFundsVisibleState.quoteStatus: MultipleQuoteStatusCard(
          controller: controller,
        ),
        SourceOfFundsVisibleState.sendAction: _MultipleSendAction(
          controller: controller,
          canSend: canSend,
          isCooldownActive: isCooldownActive,
          verificationController: verificationController,
          verificationContextKey: verificationContextKey,
        ),
      },
    );
  }
}

class _MultipleRefreshAction extends StatelessWidget {
  const _MultipleRefreshAction({required this.sourceController});

  final PaymentSourceController sourceController;

  @override
  Widget build(BuildContext context) {
    final selectedWallet = sourceController.selectedWallet;
    if (selectedWallet == null) {
      return const SizedBox.shrink();
    }

    return WalletBalanceRefreshButton(walletRef: selectedWallet.id);
  }
}

class _MultipleSendAction extends StatelessWidget {
  const _MultipleSendAction({
    required this.controller,
    required this.canSend,
    required this.isCooldownActive,
    required this.verificationController,
    required this.verificationContextKey,
  });

  final MultiplePayoutsController controller;
  final bool canSend;
  final bool isCooldownActive;
  final PayoutVerificationController verificationController;
  final String? verificationContextKey;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          SendButton(
            onPressed: () => handleMultiplePayoutSend(context, controller),
            state: controller.isSending
                ? ControlState.loading
                : canSend
                ? ControlState.enabled
                : ControlState.disabled,
          ),
          if (isCooldownActive) ...[
            const SizedBox(height: 8),
            CooldownHint(
              seconds: verificationController.cooldownRemainingSecondsFor(
                verificationContextKey,
              ),
            ),
          ],
        ],
      ),
    );
  }
}
