import 'package:flutter/material.dart';

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

import 'package:pweb/models/state/control_state.dart';
import 'package:pweb/models/state/visibility.dart';
import 'package:pweb/pages/address_book/form/widgets/payment_methods/panel/panel_container.dart';
import 'package:pweb/pages/address_book/form/widgets/payment_methods/panel/panel_entry_form.dart';
import 'package:pweb/pages/address_book/form/widgets/payment_methods/panel/panel_header.dart';
import 'package:pweb/utils/payment/method_delete_confirmation.dart';

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


class PaymentMethodPanel extends StatelessWidget {
  final PaymentType selectedType;
  final int selectedIndex;
  final List<RecipientMethodDraft> entries;
  final ValueChanged<int>? onRemove;
  final void Function(int, PaymentMethodData)? onChanged;
  final ControlState editState;
  final VisibilityState deleteVisibility;
  final double padding;

  const PaymentMethodPanel({
    super.key,
    required this.selectedType,
    required this.selectedIndex,
    required this.entries,
    this.onRemove,
    this.onChanged,
    this.editState = ControlState.enabled,
    this.deleteVisibility = VisibilityState.visible,
    this.padding = 16,
  }) : assert(editState == ControlState.disabled || onChanged != null),
       assert(deleteVisibility == VisibilityState.hidden || onRemove != null);

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;
    final entry = selectedIndex >= 0 && selectedIndex < entries.length
        ? entries[selectedIndex]
        : null;
    final showDelete =
        entry != null &&
        deleteVisibility == VisibilityState.visible &&
        onRemove != null;

    return PaymentMethodPanelContainer(
      padding: padding,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          PaymentMethodPanelHeader(
            selectedType: selectedType,
            title: l10n.paymentMethodDetails,
            deleteLabel: l10n.delete,
            showDelete: showDelete,
            onDelete: showDelete
                ? () => confirmPaymentMethodDelete(
                    context,
                    () => onRemove!(selectedIndex),
                  )
                : null,
          ),
          const SizedBox(height: 12),
          if (entry != null)
            PaymentMethodPanelEntryForm(
              selectedType: selectedType,
              selectedIndex: selectedIndex,
              entry: entry,
              isEditable: editState == ControlState.enabled,
              onChanged: onChanged == null
                  ? null
                  : (data) => onChanged!(selectedIndex, data),
            ),
        ],
      ),
    );
  }
}
