import 'package:flutter/material.dart';

import 'package:pshared/models/payment/methods/russian_bank.dart';

import 'package:pweb/utils/text_field_styles.dart';

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


class RussianBankForm extends StatefulWidget {
  final void Function(RussianBankAccountPaymentMethod) onChanged;
  final RussianBankAccountPaymentMethod? initialData;
  final bool isEditable;

  const RussianBankForm({
    super.key,
    required this.onChanged,
    this.initialData,
    required this.isEditable,
  });

  @override
  State<RussianBankForm> createState() => _RussianBankFormState();
}

class _RussianBankFormState extends State<RussianBankForm> {
  final _formKey = GlobalKey<FormState>();

  late final TextEditingController _recipientNameController;
  late final TextEditingController _innController;
  late final TextEditingController _kppController;
  late final TextEditingController _bankNameController;
  late final TextEditingController _bikController;
  late final TextEditingController _accountNumberController;
  late final TextEditingController _correspondentAccountController;

  @override
  void initState() {
    super.initState();
    _recipientNameController = TextEditingController(text: widget.initialData?.recipientName ?? '');
    _innController = TextEditingController(text: widget.initialData?.inn ?? '');
    _kppController = TextEditingController(text: widget.initialData?.kpp ?? '');
    _bankNameController = TextEditingController(text: widget.initialData?.bankName ?? '');
    _bikController = TextEditingController(text: widget.initialData?.bik ?? '');
    _accountNumberController = TextEditingController(text: widget.initialData?.accountNumber ?? '');
    _correspondentAccountController = TextEditingController(text: widget.initialData?.correspondentAccount ?? '');

    WidgetsBinding.instance.addPostFrameCallback((_) => _emitIfValid());
  }

  void _emitIfValid() {
    if (_formKey.currentState?.validate() ?? false) {
      widget.onChanged(
        RussianBankAccountPaymentMethod(
          recipientName: _recipientNameController.text,
          inn: _innController.text,
          kpp: _kppController.text,
          bankName: _bankNameController.text,
          bik: _bikController.text,
          accountNumber: _accountNumberController.text,
          correspondentAccount: _correspondentAccountController.text,
        ),
      );
    }
  }

  @override
  void didUpdateWidget(covariant RussianBankForm oldWidget) {
    super.didUpdateWidget(oldWidget);
    final newData = widget.initialData;
    final oldData = oldWidget.initialData;

    if (newData == null && oldData != null) {
      _recipientNameController.clear();
      _innController.clear();
      _kppController.clear();
      _bankNameController.clear();
      _bikController.clear();
      _accountNumberController.clear();
      _correspondentAccountController.clear();
      return;
    }

    if (newData != null && newData != oldData) {
      final hasRecipientNameChange = newData.recipientName != _recipientNameController.text;
      final hasInnChange = newData.inn != _innController.text;
      final hasKppChange = newData.kpp != _kppController.text;
      final hasBankNameChange = newData.bankName != _bankNameController.text;
      final hasBikChange = newData.bik != _bikController.text;
      final hasAccountNumberChange = newData.accountNumber != _accountNumberController.text;
      final hasCorrespondentAccountChange = newData.correspondentAccount != _correspondentAccountController.text;

      if (hasRecipientNameChange) _recipientNameController.text = newData.recipientName;
      if (hasInnChange) _innController.text = newData.inn;
      if (hasKppChange) _kppController.text = newData.kpp;
      if (hasBankNameChange) _bankNameController.text = newData.bankName;
      if (hasBikChange) _bikController.text = newData.bik;
      if (hasAccountNumberChange) _accountNumberController.text = newData.accountNumber;
      if (hasCorrespondentAccountChange) _correspondentAccountController.text = newData.correspondentAccount;

      if (hasRecipientNameChange ||
          hasInnChange ||
          hasKppChange ||
          hasBankNameChange ||
          hasBikChange ||
          hasAccountNumberChange ||
          hasCorrespondentAccountChange) {
        WidgetsBinding.instance.addPostFrameCallback((_) => _emitIfValid());
      }
    }
  }


  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;

    return Form(
      key: _formKey,
      onChanged: _emitIfValid,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          TextFormField(
            readOnly: !widget.isEditable,
            controller: _recipientNameController,
            decoration: getInputDecoration(context, l10n.recipientName, widget.isEditable),
            style: getTextFieldStyle(context, widget.isEditable),
            validator: (val) => (val == null || val.isEmpty) ? l10n.enterRecipientName : null,
          ),
          const SizedBox(height: 12),
          TextFormField(
            readOnly: !widget.isEditable,
            controller: _innController,
            decoration: getInputDecoration(context, l10n.inn, widget.isEditable),
            style: getTextFieldStyle(context, widget.isEditable),
            validator: (val) => (val == null || val.isEmpty) ? l10n.enterInn : null,
          ),
          const SizedBox(height: 12),
          TextFormField(
            readOnly: !widget.isEditable,
            controller: _kppController,
            decoration: getInputDecoration(context, l10n.kpp, widget.isEditable),
            style: getTextFieldStyle(context, widget.isEditable),
            validator: (val) => (val == null || val.isEmpty) ? l10n.enterKpp : null,
          ),
          const SizedBox(height: 12),
          TextFormField(
            readOnly: !widget.isEditable,
            controller: _bankNameController,
            decoration: getInputDecoration(context, l10n.bankName, widget.isEditable),
            style: getTextFieldStyle(context, widget.isEditable),
            validator: (val) => (val == null || val.isEmpty) ? l10n.enterBankName : null,
          ),
          const SizedBox(height: 12),
          TextFormField(
            readOnly: !widget.isEditable,
            controller: _bikController,
            decoration: getInputDecoration(context, l10n.bik, widget.isEditable),
            style: getTextFieldStyle(context, widget.isEditable),
            validator: (val) => (val == null || val.isEmpty) ? l10n.enterBik : null,
          ),
          const SizedBox(height: 12),
          TextFormField(
            readOnly: !widget.isEditable,
            controller: _accountNumberController,
            decoration: getInputDecoration(context, l10n.accountNumber, widget.isEditable),
            style: getTextFieldStyle(context, widget.isEditable),
            validator: (val) => (val == null || val.isEmpty) ? l10n.enterAccountNumber : null,
          ),
          const SizedBox(height: 12),
          TextFormField(
            readOnly: !widget.isEditable,
            controller: _correspondentAccountController,
            decoration: getInputDecoration(context, l10n.correspondentAccount, widget.isEditable),
            style: getTextFieldStyle(context, widget.isEditable),
            validator: (val) => (val == null || val.isEmpty) ? l10n.enterCorrespondentAccount : null,
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    _recipientNameController.dispose();
    _innController.dispose();
    _kppController.dispose();
    _bankNameController.dispose();
    _bikController.dispose();
    _accountNumberController.dispose();
    _correspondentAccountController.dispose();
    super.dispose();
  }
}
