import 'package:flutter/material.dart';

import 'package:pshared/models/payment/type.dart';
import 'package:pshared/models/recipient/payment_method_draft.dart';
import 'package:pweb/pages/address_book/form/widgets/payment_methods/panel/panel.dart';

import 'package:pweb/pages/address_book/form/widgets/payment_methods/selector_row.dart';
import 'package:pweb/pages/payout_page/send/widgets/payment_info/details_builder.dart';
import 'package:pweb/pages/payout_page/send/widgets/payment_info/header.dart';
import 'package:pweb/pages/payout_page/send/widgets/payment_info/methods_state.dart';
import 'package:pweb/models/state/control_state.dart';
import 'package:pweb/models/state/visibility.dart';
import 'package:pweb/utils/dimensions.dart';
import 'package:pweb/utils/payment/availability.dart';


class PaymentInfoMethodsSection extends StatelessWidget {
  final AppDimensions dimensions;
  final String title;
  final VisibilityState titleVisibility;
  final String detailsLabel;
  final PaymentInfoMethodsState state;
  final VoidCallback? onAddMethod;
  final VisibilityState paymentDetailsVisibility;
  final VoidCallback onTogglePaymentDetails;
  final ValueChanged<RecipientMethodDraft> onEntrySelected;

  const PaymentInfoMethodsSection({
    super.key,
    required this.dimensions,
    required this.title,
    required this.titleVisibility,
    required this.detailsLabel,
    required this.state,
    required this.onAddMethod,
    required this.paymentDetailsVisibility,
    required this.onTogglePaymentDetails,
    required this.onEntrySelected,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        PaymentInfoHeader(
          dimensions: dimensions,
          title: title,
          visibility: titleVisibility,
        ),
        PaymentMethodSelectorRow(
          types: state.types,
          selectedType: state.selectedType,
          selectedIndex: state.hasSelection ? state.selectedIndex : null,
          methods: state.methodsMap,
          detailsBuilder: buildPaymentInfoDetailsText,
          onSelected: _handleSelected,
          onAddPressed: onAddMethod,
          disabledTypes: disabledPaymentTypes,
        ),
        if (state.hasSelection) ...[
          SizedBox(height: dimensions.paddingSmall),
          Align(
            alignment: Alignment.centerRight,
            child: TextButton.icon(
              onPressed: onTogglePaymentDetails,
              icon: Icon(
                paymentDetailsVisibility == VisibilityState.visible
                    ? Icons.expand_less
                    : Icons.expand_more,
              ),
              label: Text(detailsLabel),
            ),
          ),
          if (paymentDetailsVisibility == VisibilityState.visible) ...[
            SizedBox(height: dimensions.paddingSmall),
            PaymentMethodPanel(
              selectedType: state.selectedType,
              selectedIndex: state.selectedIndex!,
              entries: state.selectedEntries,
              editState: ControlState.disabled,
              deleteVisibility: VisibilityState.hidden,
            ),
          ],
        ],
      ],
    );
  }

  void _handleSelected(PaymentType type, int index) {
    final entries = state.methodsMap[type] ?? const <RecipientMethodDraft>[];
    if (index < 0 || index >= entries.length) return;
    onEntrySelected(entries[index]);
  }
}
