import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

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

import 'package:pweb/pages/payment_methods/form.dart';
import 'package:pweb/pages/payment_methods/widgets/section_title.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;

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

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    final flowProvider = context.watch<PaymentFlowProvider>();
    final hasRecipient = flowProvider.hasRecipient;
    final MethodMap resolvedAvailableTypes = flowProvider.availableTypes;

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

    final selectedType = flowProvider.selectedType;

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