import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/models/payment/type.dart';
import 'package:pshared/models/recipient/recipient.dart';
import 'package:pshared/provider/recipient/methods_cache.dart';
import 'package:pshared/provider/recipient/provider.dart';

import 'package:pweb/controllers/organization/address_book_recipient_form.dart';
import 'package:pweb/controllers/organization/address_book_recipient_form_selection.dart';
import 'package:pweb/pages/address_book/form/body.dart';
import 'package:pweb/providers/address_book_recipient_form.dart';
import 'package:pweb/utils/payment/availability.dart';


class AddressBookRecipientForm extends StatefulWidget {
  final Recipient? recipient;
  final ValueChanged<Recipient?>? onSaved;

  const AddressBookRecipientForm({super.key, this.recipient, this.onSaved});

  @override
  State<AddressBookRecipientForm> createState() => _AddressBookRecipientFormState();
}

class _AddressBookRecipientFormState extends State<AddressBookRecipientForm> {
  final _formKey = GlobalKey<FormState>();
  late TextEditingController _nameCtrl;
  late TextEditingController _emailCtrl;
  late final String _initialName;
  late final String _initialEmail;

  static const List<PaymentType> _supportedTypes = visiblePaymentTypes;

  @override
  void initState() {
    super.initState();
    final r = widget.recipient;
    _initialName = r?.name ?? '';
    _initialEmail = r?.email ?? '';
    _nameCtrl = TextEditingController(text: _initialName);
    _emailCtrl = TextEditingController(text: _initialEmail);
  }

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProxyProvider2<
          RecipientsProvider,
          RecipientMethodsCacheProvider,
          AddressBookRecipientFormProvider
        >(
          create: (_) => AddressBookRecipientFormProvider(
            recipient: widget.recipient,
          ),
          update: (_, recipientsProvider, methodsCache, formProvider) =>
              formProvider!..updateProviders(
                recipientsProvider: recipientsProvider,
                methodsCache: methodsCache,
              ),
        ),
        ChangeNotifierProxyProvider<
          RecipientMethodsCacheProvider,
          AddressBookRecipientFormController
        >(
          create: (_) => AddressBookRecipientFormController(
            supportedTypes: _supportedTypes,
          ),
          update: (_, methodsCache, controller) => controller!
            ..update(
              recipient: widget.recipient,
              methodsCache: methodsCache,
            ),
        ),
        ChangeNotifierProxyProvider<
          AddressBookRecipientFormController,
          AddressBookRecipientFormSelectionController
        >(
          create: (_) => AddressBookRecipientFormSelectionController(),
          update: (_, formController, selectionController) =>
              selectionController!..update(formController),
        ),
      ],
      child: Builder(
        builder: (context) {
          final formState = context.read<AddressBookRecipientFormProvider>();
          final controller = context.read<AddressBookRecipientFormController>();
          return AddressBookRecipientFormBody(
            formKey: _formKey,
            nameCtrl: _nameCtrl,
            emailCtrl: _emailCtrl,
            isEditing: widget.recipient != null,
            onSave: (form) => controller.saveForm(
              context: context,
              formKey: _formKey,
              formState: form,
              name: _nameCtrl.text,
              email: _emailCtrl.text,
              onSaved: widget.onSaved,
            ),
            onBack: () => controller.handleBack(
              context: context,
              formKey: _formKey,
              formState: formState,
              name: _nameCtrl.text,
              email: _emailCtrl.text,
              onSaved: widget.onSaved,
            ),
          );
        },
      ),
    );
  }
}
