import 'package:flutter/material.dart';

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

import 'package:pweb/pages/address_book/form/widgets/feilds/email.dart';
import 'package:pweb/pages/address_book/form/widgets/header.dart';
import 'package:pweb/pages/address_book/form/widgets/feilds/name.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/address_book/form/widgets/save_button.dart';

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


class FormView extends StatelessWidget {
  final GlobalKey<FormState> formKey;
  final TextEditingController nameCtrl;
  final TextEditingController emailCtrl;
  final List<PaymentType> types;
  final PaymentType selectedType;
  final int? selectedIndex;
  final Map<PaymentType, List<RecipientMethodDraft>> methods;
  final void Function(PaymentType type, int index) onMethodSelected;
  final ValueChanged<PaymentType> onMethodAdd;
  final Set<PaymentType> disabledTypes;
  final ValueChanged<int> onMethodRemove;
  final void Function(int, PaymentMethodData) onMethodChanged;
  final VoidCallback onSave;
  final bool isEditing;
  final VoidCallback onBack;

  final double maxWidth;
  final double elevation;
  final double borderRadius;
  final EdgeInsetsGeometry padding;
  final double spacingHeader;
  final double spacingFields;
  final double spacingDivider;
  final double spacingSave;
  final double spacingBottom;
  final TextStyle? titleTextStyle;

  const FormView({
    super.key,
    required this.formKey,
    required this.nameCtrl,
    required this.emailCtrl,
    required this.types,
    required this.selectedType,
    required this.selectedIndex,
    required this.methods,
    required this.onMethodSelected,
    required this.onMethodAdd,
    this.disabledTypes = const {},
    required this.onMethodRemove,
    required this.onMethodChanged,
    required this.onSave,
    required this.isEditing,
    required this.onBack,
    this.maxWidth = 800,
    this.elevation = 4,
    this.borderRadius = 16,
    this.padding = const EdgeInsets.all(20),
    this.spacingHeader = 20,
    this.spacingFields = 12,
    this.spacingDivider = 40,
    this.spacingSave = 30,
    this.spacingBottom = 16,
    this.titleTextStyle,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final entries = methods[selectedType] ?? const <RecipientMethodDraft>[];
    final hasSelection = selectedIndex != null &&
        selectedIndex! >= 0 &&
        selectedIndex! < entries.length;

    return Align(
      alignment: Alignment.topCenter,
      child: ConstrainedBox(
        constraints: BoxConstraints(maxWidth: maxWidth),
        child: Material(
          elevation: elevation,
          borderRadius: BorderRadius.circular(borderRadius),
          color: theme.colorScheme.onSecondary,
          child: Padding(
            padding: padding,
            child: Form(
              key: formKey,
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    HeaderWidget(
                      isEditing: isEditing,
                      onBack: onBack,
                    ),
                    SizedBox(height: spacingHeader),
                    NameField(controller: nameCtrl),
                    SizedBox(height: spacingFields),
                    EmailField(controller: emailCtrl),
                    Divider(height: spacingDivider),
                    Text(
                      AppLocalizations.of(context)!.choosePaymentMethod,
                      style: titleTextStyle ??
                          theme.textTheme.titleMedium
                              ?.copyWith(fontWeight: FontWeight.bold),
                    ),
                    SizedBox(height: spacingFields),
                    PaymentMethodSelectorRow(
                      types: types,
                      selectedType: selectedType,
                      selectedIndex: selectedIndex,
                      methods: methods,
                      onSelected: onMethodSelected,
                      onAdd: onMethodAdd,
                      disabledTypes: disabledTypes,
                    ),
                    if (hasSelection) ...[
                      SizedBox(height: spacingFields),
                      PaymentMethodPanel(
                        selectedType: selectedType,
                        selectedIndex: selectedIndex!,
                        entries: entries,
                        onRemove: onMethodRemove,
                        onChanged: onMethodChanged,
                      ),
                    ],
                    SizedBox(height: spacingSave),
                    SaveButton(onSave: onSave),
                    SizedBox(height: spacingBottom),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
