import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

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

import 'package:pweb/providers/payment_flow_provider.dart';
import 'package:pweb/pages/payment_methods/widgets/payment_page_body.dart';
import 'package:pweb/providers/page_selector.dart';
import 'package:pweb/providers/payment_methods.dart';
import 'package:pweb/providers/recipient.dart';


class PaymentPage extends StatefulWidget {
  final ValueChanged<Recipient?>? onBack;

  const PaymentPage({super.key, this.onBack});

  @override
  State<PaymentPage> createState() => _PaymentPageState();
}

class _PaymentPageState extends State<PaymentPage> {
  late final TextEditingController _searchController;
  late final FocusNode _searchFocusNode;
  late final PaymentFlowProvider _flowProvider;

  @override
  void initState() {
    super.initState();
    _searchController = TextEditingController();
    _searchFocusNode = FocusNode();
    final pageSelector = context.read<PageSelectorProvider>();
    _flowProvider = PaymentFlowProvider(
      initialType: pageSelector.getDefaultPaymentType(),
    );

    WidgetsBinding.instance.addPostFrameCallback((_) => _initializePaymentPage());
  }

  @override
  void dispose() {
    _searchController.dispose();
    _searchFocusNode.dispose();
    _flowProvider.dispose();
    super.dispose();
  }

  void _initializePaymentPage() {
    final pageSelector = context.read<PageSelectorProvider>();
    final methodsProvider = context.read<PaymentMethodsProvider>();
    final recipientProvider = context.read<RecipientProvider>();

    pageSelector.handleWalletAutoSelection();

    if (methodsProvider.methods.isEmpty && !methodsProvider.isLoading) {
      methodsProvider.loadMethods();
    }

    if (recipientProvider.recipients.isEmpty && !recipientProvider.isLoading) {
      recipientProvider.loadRecipients();
    }

    _flowProvider.syncWithSelector(pageSelector);
  }

  void _handleSearchChanged(String query) {
    context.read<RecipientProvider>().setQuery(query);
  }

  void _handleRecipientSelected(Recipient recipient) {
    final pageSelector = context.read<PageSelectorProvider>();
    final recipientProvider = context.read<RecipientProvider>();

    recipientProvider.selectRecipient(recipient);
    pageSelector.selectRecipient(recipient);
    _flowProvider.reset(pageSelector);
    _clearSearchField();
  }

  void _handleRecipientCleared() {
    final pageSelector = context.read<PageSelectorProvider>();
    final recipientProvider = context.read<RecipientProvider>();

    recipientProvider.selectRecipient(null);
    pageSelector.selectRecipient(null);
    _flowProvider.reset(pageSelector);
    _clearSearchField();
  }

  void _clearSearchField() {
    _searchController.clear();
    _searchFocusNode.unfocus();
    context.read<RecipientProvider>().setQuery('');
  }

  void _handleSendPayment() {
    // TODO: Handle Payment logic
    // AmplitudeService.paymentInitiated();
  }

  @override
  Widget build(BuildContext context) {
    final pageSelector = context.watch<PageSelectorProvider>();
    _flowProvider.syncWithSelector(pageSelector);

    return ChangeNotifierProvider.value(
      value: _flowProvider,
      child: PaymentPageBody(
        onBack: widget.onBack,
        searchController: _searchController,
        searchFocusNode: _searchFocusNode,
        onSearchChanged: _handleSearchChanged,
        onRecipientSelected: _handleRecipientSelected,
        onRecipientCleared: _handleRecipientCleared,
        onSend: _handleSendPayment,
      ),
    );
  }
}
