import 'package:flutter/material.dart';

import 'package:pshared/models/payment/methods/card.dart';
import 'package:pshared/models/payment/methods/iban.dart';
import 'package:pshared/models/payment/methods/russian_bank.dart';
import 'package:pshared/models/payment/methods/wallet.dart';
import 'package:pshared/models/payment/type.dart';
import 'package:pshared/models/recipient/recipient.dart';
import 'package:pshared/models/recipient/status.dart';
import 'package:pshared/models/recipient/type.dart';

import 'package:pweb/pages/address_book/form/view.dart';
import 'package:pweb/services/amplitude.dart';

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


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

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

  @override
  State<AdressBookRecipientForm> createState() => _AdressBookRecipientFormState();
}

class _AdressBookRecipientFormState extends State<AdressBookRecipientForm> {
  final _formKey = GlobalKey<FormState>();
  late TextEditingController _nameCtrl;
  late TextEditingController _emailCtrl;
  RecipientType _type = RecipientType.internal;
  RecipientStatus _status = RecipientStatus.ready;
  final Map<PaymentType, Object?> _methods = {};

  @override
  void initState() {
    super.initState();
    final r = widget.recipient;
    _nameCtrl = TextEditingController(text: r?.name ?? "");
    _emailCtrl = TextEditingController(text: r?.email ?? "");
    _type = r?.type ?? RecipientType.internal;
    _status = r?.status ?? RecipientStatus.ready;

    if (r?.card != null) _methods[PaymentType.card] = r!.card;
    if (r?.iban != null) _methods[PaymentType.iban] = r!.iban;
    if (r?.wallet != null) _methods[PaymentType.wallet] = r!.wallet;
    if (r?.bank != null) _methods[PaymentType.bankAccount] = r!.bank;
  }

  //TODO Change when registration is ready
  void _save() {
    if (!_formKey.currentState!.validate() || _methods.isEmpty) {
      AmplitudeService.recipientAddCompleted(
        _type,
        _status,
        _methods.keys.toSet(),
      );
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text(AppLocalizations.of(context)!.recipientFormRule),
        ),
      );
      return;
    }

    final recipient = Recipient(
      name: _nameCtrl.text,
      email: _emailCtrl.text,
      type: _type,
      status: _status,
      avatarUrl: null,
      card: _methods[PaymentType.card] as CardPaymentMethod?,
      iban: _methods[PaymentType.iban] as IbanPaymentMethod?,
      wallet: _methods[PaymentType.wallet] as WalletPaymentMethod?,
      bank: _methods[PaymentType.bankAccount] as RussianBankAccountPaymentMethod?,
    );

    widget.onSaved?.call(recipient);
  }

  @override
  Widget build(BuildContext context) {
    return FormView(
      formKey: _formKey,
      nameCtrl: _nameCtrl,
      emailCtrl: _emailCtrl,
      type: _type,
      status: _status,
      methods: _methods,
      onTypeChanged: (t) => setState(() => _type = t),
      onStatusChanged: (s) => setState(() => _status = s),
      onMethodsChanged: (type, data) {
        setState(() {
          if (data != null) {
            _methods[type] = data;
          } else {
            _methods.remove(type);
          }
        });
      },
      onSave: _save,
      isEditing: widget.recipient != null,
      onBack: () {
        widget.onSaved?.call(null);
      },
    );
  }
}