import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/models/recipient/recipient.dart';
import 'package:pshared/provider/payment/flow.dart';
import 'package:pshared/provider/payment/quotation/quotation.dart';
import 'package:pshared/provider/recipient/pmethods.dart';
import 'package:pshared/provider/recipient/provider.dart';

import 'package:pweb/pages/payout_page/send/body.dart';
import 'package:pweb/utils/recipient/filtering.dart';
import 'package:pweb/widgets/sidebar/destinations.dart';
import 'package:pweb/controllers/payments/page_ui.dart';
import 'package:pweb/controllers/payouts/payout_verification.dart';
import 'package:pweb/models/state/control_state.dart';


class PaymentPageView extends StatelessWidget {
  final PaymentPageUiController uiController;
  final ValueChanged<Recipient?>? onBack;
  final PayoutDestination fallbackDestination;
  final ValueChanged<String> onSearchChanged;
  final ValueChanged<Recipient> onRecipientSelected;
  final VoidCallback onRecipientCleared;
  final VoidCallback onSend;
  final VoidCallback onAddRecipient;
  final VoidCallback onAddPaymentMethod;

  const PaymentPageView({
    super.key,
    required this.uiController,
    required this.onBack,
    required this.fallbackDestination,
    required this.onSearchChanged,
    required this.onRecipientSelected,
    required this.onRecipientCleared,
    required this.onSend,
    required this.onAddRecipient,
    required this.onAddPaymentMethod,
  });

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider.value(
      value: uiController,
      child: Builder(
        builder: (context) {
          final uiController = context.watch<PaymentPageUiController>();
          final methodsProvider = context.watch<PaymentMethodsProvider>();
          final recipientProvider = context.watch<RecipientsProvider>();
          final flowProvider = context.watch<PaymentFlowProvider>();
          final quotationProvider = context.watch<QuotationProvider>();
          final verificationController = context
              .watch<PayoutVerificationController>();
          final verificationContextKey =
              quotationProvider.quotation?.quoteRef ??
              quotationProvider.quotation?.idempotencyKey;
          final recipient = recipientProvider.currentObject;
          final filteredRecipients = filterRecipients(
            recipients: recipientProvider.recipients,
            query: uiController.query,
          );
          final hasDestinationSelection =
              flowProvider.selectedPaymentData != null;
          final sendState =
              verificationController.isCooldownActiveFor(verificationContextKey)
              ? ControlState.disabled
              : (!hasDestinationSelection
                    ? ControlState.disabled
                    : ControlState.enabled);

          return PaymentPageBody(
            onBack: onBack,
            fallbackDestination: fallbackDestination,
            recipient: recipient,
            recipientProvider: recipientProvider,
            searchQuery: uiController.query,
            filteredRecipients: filteredRecipients,
            methodsProvider: methodsProvider,
            sendState: sendState,
            cooldownRemainingSeconds: verificationController
                .cooldownRemainingSecondsFor(verificationContextKey),
            searchController: uiController.searchController,
            searchFocusNode: uiController.searchFocusNode,
            onSearchChanged: onSearchChanged,
            onRecipientSelected: onRecipientSelected,
            onRecipientCleared: onRecipientCleared,
            onSend: onSend,
            onAddRecipient: onAddRecipient,
            onAddPaymentMethod: onAddPaymentMethod,
            paymentDetailsVisibility: uiController.paymentDetailsVisibility,
            onTogglePaymentDetails: uiController.togglePaymentDetails,
          );
        },
      ),
    );
  }
}
