import 'package:flutter/material.dart';

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

import 'package:pweb/pages/payment_methods/form.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/utils/dimensions.dart';
import 'package:pweb/utils/payment/selector_type.dart';

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


class PaymentInfoSection extends StatelessWidget {
  final AppDimensions dimensions;
  final PageSelectorProvider pageSelector;
  final PaymentFlowProvider flowProvider;
  final Recipient? recipient;

  const PaymentInfoSection({
    super.key,
    required this.dimensions,
    required this.pageSelector,
    required this.flowProvider,
    required this.recipient,
  });

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    final hasRecipient = recipient != null;
    final availableTypes = hasRecipient
        ? pageSelector.getAvailablePaymentTypes()
        : {for (final type in PaymentType.values) type: type};

    if (hasRecipient && availableTypes.isEmpty) {
      return Text(loc.recipientNoPaymentDetails);
    }

    final selectedType = flowProvider.selectedType;

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SectionTitle(loc.paymentInfo),
        SizedBox(height: dimensions.paddingSmall),
        PaymentTypeSelector(
          availableTypes: availableTypes,
          selectedType: selectedType,
          onSelected: (type) => flowProvider.selectType(
            type,
            resetManualData: !hasRecipient,
          ),
        ),
        SizedBox(height: dimensions.paddingMedium),
        PaymentMethodForm(
          selectedType: selectedType,
          onChanged: (data) {
            if (!hasRecipient) {
              flowProvider.setManualPaymentData(data);
            }
          },
          initialData: hasRecipient ? availableTypes[selectedType] : flowProvider.manualPaymentData,
          isEditable: !hasRecipient,
        ),
      ],
    );
  }
}
