import 'package:flutter/material.dart';

import 'package:go_router/go_router.dart';

import 'package:provider/provider.dart';

import 'package:pshared/models/payment/methods/ledger.dart';
import 'package:pshared/models/payment/type.dart';
import 'package:pshared/models/recipient/recipient.dart';
import 'package:pshared/provider/payment/flow.dart';
import 'package:pshared/provider/payment/provider.dart';
import 'package:pshared/provider/payment/quotation/quotation.dart';
import 'package:pshared/provider/recipient/provider.dart';

import 'package:pweb/app/router/payout_routes.dart';
import 'package:pweb/widgets/sidebar/destinations.dart';
import 'package:pweb/widgets/dialogs/payment_status_dialog.dart';
import 'package:pweb/controllers/payments/page.dart';
import 'package:pweb/controllers/payments/page_ui.dart';
import 'package:pweb/controllers/payouts/payout_verification.dart';
import 'package:pweb/utils/payment/verification_flow.dart';


void initializePaymentPage(
  BuildContext context,
  PaymentType? initialPaymentType, {
  String? destinationLedgerAccountRef,
}) {
  final flowProvider = context.read<PaymentFlowProvider>();
  final recipientsProvider = context.read<RecipientsProvider>();

  flowProvider.setPreferredType(initialPaymentType);

  final destinationRef = destinationLedgerAccountRef?.trim();
  if (destinationRef != null && destinationRef.isNotEmpty) {
    recipientsProvider.setCurrentObject(null);
    flowProvider.setPreferredType(PaymentType.ledger);
    flowProvider.setManualPaymentData(
      LedgerPaymentMethod(ledgerAccountRef: destinationRef),
    );
    return;
  }

  flowProvider.setManualPaymentData(null);
}

void handleSearchChanged(PaymentPageUiController uiController, String query) {
  uiController.setQuery(query);
}

void handleRecipientSelected(
  BuildContext context,
  PaymentPageUiController uiController,
  Recipient recipient,
) {
  final recipientProvider = context.read<RecipientsProvider>();
  recipientProvider.setCurrentObject(recipient.id);
  uiController.clearSearch();
}

void handleRecipientCleared(
  BuildContext context,
  PaymentPageUiController uiController,
) {
  final recipientProvider = context.read<RecipientsProvider>();
  recipientProvider.setCurrentObject(null);
  uiController.clearSearch();
}

Future<void> handleSendPayment({
  required State state,
  required PayoutDestination fallbackDestination,
}) async {
  final context = state.context;
  final paymentProvider = context.read<PaymentProvider>();
  final quotationProvider = context.read<QuotationProvider>();
  final controller = context.read<PaymentPageController>();
  final verificationController = context.read<PayoutVerificationController>();
  if (paymentProvider.isLoading) return;
  final verificationContextKey =
      quotationProvider.quotation?.quoteRef ??
      quotationProvider.quotation?.idempotencyKey;

  final verified = await runPayoutVerification(
    context: context,
    controller: verificationController,
    contextKey: verificationContextKey,
  );
  if (!verified || !state.mounted) return;

  final isSuccess = await controller.sendPayment();
  if (!state.mounted) return;

  await showPaymentStatusDialog(context, isSuccess: isSuccess);
  if (!state.mounted) return;

  if (isSuccess) {
    controller.handleSuccess();
    context.goToPayout(fallbackDestination);
  }
}

void handleAddRecipient(BuildContext context) {
  final recipients = context.read<RecipientsProvider>();
  recipients.setCurrentObject(null);
  context.pushToPayout(PayoutDestination.addrecipient);
}

void handleAddPaymentMethod(BuildContext context) {
  final recipients = context.read<RecipientsProvider>();
  final recipient = recipients.currentObject;
  if (recipient == null) return;
  recipients.setCurrentObject(recipient.id);
  context.pushNamed(PayoutRoutes.editRecipient);
}
