import 'package:flutter/material.dart';

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

import 'package:pweb/widgets/sidebar/destinations.dart';
import 'package:pweb/pages/payout_page/send/widgets/state_view.dart';
import 'package:pweb/pages/payout_page/send/content.dart';
import 'package:pweb/models/state/control_state.dart';
import 'package:pweb/models/state/visibility.dart';

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

class PaymentPageBody extends StatelessWidget {
  final ValueChanged<Recipient?>? onBack;
  final Recipient? recipient;
  final RecipientsProvider recipientProvider;
  final String searchQuery;
  final List<Recipient> filteredRecipients;
  final PaymentMethodsProvider methodsProvider;
  final ControlState sendState;
  final int cooldownRemainingSeconds;
  final PayoutDestination fallbackDestination;
  final TextEditingController searchController;
  final FocusNode searchFocusNode;
  final ValueChanged<String> onSearchChanged;
  final ValueChanged<Recipient> onRecipientSelected;
  final VoidCallback onRecipientCleared;
  final VoidCallback onSend;
  final VoidCallback onAddRecipient;
  final VoidCallback onAddPaymentMethod;
  final VisibilityState paymentDetailsVisibility;
  final VoidCallback onTogglePaymentDetails;

  const PaymentPageBody({
    super.key,
    required this.onBack,
    required this.recipient,
    required this.recipientProvider,
    required this.searchQuery,
    required this.filteredRecipients,
    required this.methodsProvider,
    required this.sendState,
    required this.cooldownRemainingSeconds,
    required this.fallbackDestination,
    required this.searchController,
    required this.searchFocusNode,
    required this.onSearchChanged,
    required this.onRecipientSelected,
    required this.onRecipientCleared,
    required this.onSend,
    required this.onAddRecipient,
    required this.onAddPaymentMethod,
    required this.paymentDetailsVisibility,
    required this.onTogglePaymentDetails,
  });

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;

    if (methodsProvider.isLoading) {
      return const PaymentMethodsLoadingView();
    }

    if (methodsProvider.error != null) {
      return PaymentMethodsErrorView(
        message: loc.notificationError(
          methodsProvider.error ?? loc.noErrorInformation,
        ),
      );
    }

    return PaymentPageContent(
      onBack: onBack,
      recipient: recipient,
      recipientProvider: recipientProvider,
      searchQuery: searchQuery,
      filteredRecipients: filteredRecipients,
      fallbackDestination: fallbackDestination,
      sendState: sendState,
      cooldownRemainingSeconds: cooldownRemainingSeconds,
      searchController: searchController,
      searchFocusNode: searchFocusNode,
      onSearchChanged: onSearchChanged,
      onRecipientSelected: onRecipientSelected,
      onRecipientCleared: onRecipientCleared,
      onSend: onSend,
      onAddRecipient: onAddRecipient,
      onAddPaymentMethod: onAddPaymentMethod,
      paymentDetailsVisibility: paymentDetailsVisibility,
      onTogglePaymentDetails: onTogglePaymentDetails,
    );
  }
}
