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/status.dart';
import 'package:pshared/models/recipient/type.dart';

import 'package:pweb/pages/address_book/form/method_tile.dart';
import 'package:pweb/pages/address_book/form/widgets/button.dart';
import 'package:pweb/pages/address_book/form/widgets/email_field.dart';
import 'package:pweb/pages/address_book/form/widgets/header.dart';
import 'package:pweb/pages/address_book/form/widgets/name_field.dart';
import 'package:pweb/utils/payment/label.dart';

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


class FormView extends StatelessWidget {
  final GlobalKey<FormState> formKey;
  final TextEditingController nameCtrl;
  final TextEditingController emailCtrl;
  final RecipientType type;
  final RecipientStatus status;
  final MethodMap methods;
  final ValueChanged<RecipientType> onTypeChanged;
  final ValueChanged<RecipientStatus> onStatusChanged;
  final void Function(PaymentType, PaymentMethodData?) onMethodsChanged;
  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.type,
    required this.status,
    required this.methods,
    required this.onTypeChanged,
    required this.onStatusChanged,
    required this.onMethodsChanged,
    required this.onSave,
    required this.isEditing,
    required this.onBack,
    this.maxWidth = 500,
    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);

    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),
                    ...PaymentType.values.map(
                      (p) => AddressBookPaymentMethodTile(
                        type: p,
                        title: getPaymentTypeLabel(context, p),
                        methods: methods,
                        onChanged: (data) => onMethodsChanged(p, data),
                      ),
                    ),
                    SizedBox(height: spacingSave),
                    SaveButton(onSave: onSave),
                    SizedBox(height: spacingBottom),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}