import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

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

import 'package:pweb/pages/payment_methods/header.dart';
import 'package:pweb/pages/payment_methods/method_selector.dart';
import 'package:pweb/pages/payment_methods/send_button.dart';
import 'package:pweb/pages/dashboard/payouts/payment_form.dart';
import 'package:pweb/pages/payment_methods/widgets/payment_info_section.dart';
import 'package:pweb/pages/payment_methods/widgets/recipient_section.dart';
import 'package:pweb/pages/payment_methods/widgets/section_title.dart';
import 'package:pweb/providers/page_selector.dart';
import 'package:pweb/providers/payment_flow_provider.dart';
import 'package:pweb/providers/payment_methods.dart';
import 'package:pweb/providers/recipient.dart';
import 'package:pweb/utils/dimensions.dart';
import 'package:pweb/generated/i18n/app_localizations.dart';


class PaymentPageBody extends StatelessWidget {
  final ValueChanged<Recipient?>? onBack;
  final TextEditingController searchController;
  final FocusNode searchFocusNode;
  final ValueChanged<String> onSearchChanged;
  final ValueChanged<Recipient> onRecipientSelected;
  final VoidCallback onRecipientCleared;
  final VoidCallback onSend;

  const PaymentPageBody({
    super.key,
    required this.onBack,
    required this.searchController,
    required this.searchFocusNode,
    required this.onSearchChanged,
    required this.onRecipientSelected,
    required this.onRecipientCleared,
    required this.onSend,
  });

  @override
  Widget build(BuildContext context) {
    final dimensions = AppDimensions();
    final pageSelector = context.watch<PageSelectorProvider>();
    final methodsProvider = context.watch<PaymentMethodsProvider>();
    final recipientProvider = context.watch<RecipientProvider>();
    final flowProvider = context.watch<PaymentFlowProvider>();
    final recipient = pageSelector.selectedRecipient;
    final loc = AppLocalizations.of(context)!;

    if (methodsProvider.isLoading) {
      return const Center(child: CircularProgressIndicator());
    }

    if (methodsProvider.error != null) {
      return Center(child: Text(loc.notificationError(methodsProvider.error ?? loc.noErrorInformation)));
    }

    return Align(
      alignment: Alignment.topCenter,
      child: ConstrainedBox(
        constraints: BoxConstraints(maxWidth: dimensions.maxContentWidth),
        child: Material(
          elevation: dimensions.elevationSmall,
          borderRadius: BorderRadius.circular(dimensions.borderRadiusMedium),
          color: Theme.of(context).colorScheme.onSecondary,
          child: Padding(
            padding: EdgeInsets.all(dimensions.paddingLarge),
            child: SingleChildScrollView(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  PaymentBackButton(onBack: onBack, pageSelector: pageSelector),
                  SizedBox(height: dimensions.paddingSmall),
                  PaymentHeader(),
                  SizedBox(height: dimensions.paddingXXLarge),

                  SectionTitle(loc.sourceOfFunds),
                  SizedBox(height: dimensions.paddingSmall),
                  PaymentMethodSelector(
                    methodsProvider: methodsProvider,
                    onMethodChanged: methodsProvider.selectMethod,
                  ),
                  SizedBox(height: dimensions.paddingXLarge),

                  RecipientSection(
                    recipient: recipient,
                    dimensions: dimensions,
                    recipientProvider: recipientProvider,
                    searchController: searchController,
                    searchFocusNode: searchFocusNode,
                    onSearchChanged: onSearchChanged,
                    onRecipientSelected: onRecipientSelected,
                    onRecipientCleared: onRecipientCleared,
                  ),

                  SizedBox(height: dimensions.paddingXLarge),

                  PaymentInfoSection(
                    dimensions: dimensions,
                    pageSelector: pageSelector,
                    flowProvider: flowProvider,
                    recipient: recipient,
                  ),

                  SizedBox(height: dimensions.paddingLarge),
                  const PaymentFormWidget(),

                  SizedBox(height: dimensions.paddingXXXLarge),
                  SendButton(onPressed: onSend),
                  SizedBox(height: dimensions.paddingLarge),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class PaymentBackButton extends StatelessWidget {
  final ValueChanged<Recipient?>? onBack;
  final PageSelectorProvider pageSelector;

  const PaymentBackButton({
    super.key,
    required this.onBack,
    required this.pageSelector,
  });

  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: Alignment.topLeft,
      child: IconButton(
        icon: const Icon(Icons.arrow_back),
        onPressed: () {
          if (onBack != null) {
            onBack!(pageSelector.selectedRecipient);
          } else {
            pageSelector.goBackFromPayment();
          }
        },
      ),
    );
  }
}
