import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/provider/payment/flow.dart';

import 'package:pweb/pages/payout_page/send/widgets/payment_info/methods_section.dart';
import 'package:pweb/pages/payout_page/send/widgets/payment_info/methods_state.dart';
import 'package:pweb/pages/payout_page/send/widgets/payment_info/manual_details.dart';
import 'package:pweb/pages/payout_page/send/widgets/payment_info/no_methods.dart';
import 'package:pweb/pages/payout_page/send/widgets/payment_info/no_recipient.dart';
import 'package:pweb/models/state/visibility.dart';
import 'package:pweb/utils/dimensions.dart';
import 'package:pweb/utils/payment/availability.dart';

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


class PaymentInfoSection extends StatelessWidget {
  final AppDimensions dimensions;
  final VisibilityState titleVisibility;
  final VoidCallback? onAddMethod;
  final VisibilityState paymentDetailsVisibility;
  final VoidCallback onTogglePaymentDetails;

  const PaymentInfoSection({
    super.key,
    required this.dimensions,
    this.titleVisibility = VisibilityState.visible,
    this.onAddMethod,
    required this.paymentDetailsVisibility,
    required this.onTogglePaymentDetails,
  });

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    final flowProvider = context.watch<PaymentFlowProvider>();
    final manualData = flowProvider.manualPaymentData;

    if (!flowProvider.hasRecipient && manualData == null) {
      return PaymentInfoNoRecipientSection(
        dimensions: dimensions,
        title: loc.paymentInfo,
        titleVisibility: titleVisibility,
      );
    }

    if (!flowProvider.hasRecipient && manualData != null) {
      return PaymentInfoManualDetailsSection(
        dimensions: dimensions,
        title: loc.paymentInfo,
        titleVisibility: titleVisibility,
        data: manualData,
      );
    }

    final methods = flowProvider.methodsForRecipient;
    final types = visiblePaymentTypes;

    if (methods.isEmpty) {
      return PaymentInfoNoMethodsSection(
        dimensions: dimensions,
        title: loc.paymentInfo,
        titleVisibility: titleVisibility,
        emptyMessage: loc.recipientNoPaymentDetails,
        types: types,
        onAddMethod: onAddMethod,
      );
    }

    final state = buildPaymentInfoMethodsState(
      flowProvider: flowProvider,
      types: types,
    );

    return PaymentInfoMethodsSection(
      dimensions: dimensions,
      title: loc.paymentInfo,
      titleVisibility: titleVisibility,
      detailsLabel: loc.paymentMethodDetails,
      state: state,
      onAddMethod: onAddMethod,
      paymentDetailsVisibility: paymentDetailsVisibility,
      onTogglePaymentDetails: onTogglePaymentDetails,
      onEntrySelected: (entry) {
        final existing = entry.existing;
        if (existing != null) {
          flowProvider.selectMethod(existing);
        }
      },
    );
  }
}
