// GENERATED CODE - DO NOT MODIFY BY HAND
// dart format width=80

// **************************************************************************
// StackedFormGenerator
// **************************************************************************

// ignore_for_file: public_member_api_docs, constant_identifier_names, non_constant_identifier_names,unnecessary_this

import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';
import 'package:yimaru_app/ui/common/validators/form_validator.dart';

const bool _autoTextFieldValidation = true;

const String EmailValueKey = 'email';
const String RegionValueKey = 'region';
const String PhoneNumberValueKey = 'phoneNumber';
const String LastNameValueKey = 'lastName';
const String FirstNameValueKey = 'firstName';

final Map<String, TextEditingController>
    _ProfileDetailViewTextEditingControllers = {};

final Map<String, FocusNode> _ProfileDetailViewFocusNodes = {};

final Map<String, String? Function(String?)?>
    _ProfileDetailViewTextValidations = {
  EmailValueKey: FormValidator.validateForm,
  RegionValueKey: FormValidator.validateForm,
  PhoneNumberValueKey: FormValidator.validatePhoneNumberForm,
  LastNameValueKey: FormValidator.validateForm,
  FirstNameValueKey: FormValidator.validateForm,
};

mixin $ProfileDetailView {
  TextEditingController get emailController =>
      _getFormTextEditingController(EmailValueKey);
  TextEditingController get regionController =>
      _getFormTextEditingController(RegionValueKey);
  TextEditingController get phoneNumberController =>
      _getFormTextEditingController(PhoneNumberValueKey);
  TextEditingController get lastNameController =>
      _getFormTextEditingController(LastNameValueKey);
  TextEditingController get firstNameController =>
      _getFormTextEditingController(FirstNameValueKey);

  FocusNode get emailFocusNode => _getFormFocusNode(EmailValueKey);
  FocusNode get regionFocusNode => _getFormFocusNode(RegionValueKey);
  FocusNode get phoneNumberFocusNode => _getFormFocusNode(PhoneNumberValueKey);
  FocusNode get lastNameFocusNode => _getFormFocusNode(LastNameValueKey);
  FocusNode get firstNameFocusNode => _getFormFocusNode(FirstNameValueKey);

  TextEditingController _getFormTextEditingController(
    String key, {
    String? initialValue,
  }) {
    if (_ProfileDetailViewTextEditingControllers.containsKey(key)) {
      return _ProfileDetailViewTextEditingControllers[key]!;
    }

    _ProfileDetailViewTextEditingControllers[key] =
        TextEditingController(text: initialValue);
    return _ProfileDetailViewTextEditingControllers[key]!;
  }

  FocusNode _getFormFocusNode(String key) {
    if (_ProfileDetailViewFocusNodes.containsKey(key)) {
      return _ProfileDetailViewFocusNodes[key]!;
    }
    _ProfileDetailViewFocusNodes[key] = FocusNode();
    return _ProfileDetailViewFocusNodes[key]!;
  }

  /// Registers a listener on every generated controller that calls [model.setData()]
  /// with the latest textController values
  void syncFormWithViewModel(FormStateHelper model) {
    emailController.addListener(() => _updateFormData(model));
    regionController.addListener(() => _updateFormData(model));
    phoneNumberController.addListener(() => _updateFormData(model));
    lastNameController.addListener(() => _updateFormData(model));
    firstNameController.addListener(() => _updateFormData(model));

    _updateFormData(model, forceValidate: _autoTextFieldValidation);
  }

  /// Registers a listener on every generated controller that calls [model.setData()]
  /// with the latest textController values
  @Deprecated(
    'Use syncFormWithViewModel instead.'
    'This feature was deprecated after 3.1.0.',
  )
  void listenToFormUpdated(FormViewModel model) {
    emailController.addListener(() => _updateFormData(model));
    regionController.addListener(() => _updateFormData(model));
    phoneNumberController.addListener(() => _updateFormData(model));
    lastNameController.addListener(() => _updateFormData(model));
    firstNameController.addListener(() => _updateFormData(model));

    _updateFormData(model, forceValidate: _autoTextFieldValidation);
  }

  /// Updates the formData on the FormViewModel
  void _updateFormData(FormStateHelper model, {bool forceValidate = false}) {
    model.setData(
      model.formValueMap
        ..addAll({
          EmailValueKey: emailController.text,
          RegionValueKey: regionController.text,
          PhoneNumberValueKey: phoneNumberController.text,
          LastNameValueKey: lastNameController.text,
          FirstNameValueKey: firstNameController.text,
        }),
    );

    if (_autoTextFieldValidation || forceValidate) {
      updateValidationData(model);
    }
  }

  bool validateFormFields(FormViewModel model) {
    _updateFormData(model, forceValidate: true);
    return model.isFormValid;
  }

  /// Calls dispose on all the generated controllers and focus nodes
  void disposeForm() {
    // The dispose function for a TextEditingController sets all listeners to null

    for (var controller in _ProfileDetailViewTextEditingControllers.values) {
      controller.dispose();
    }
    for (var focusNode in _ProfileDetailViewFocusNodes.values) {
      focusNode.dispose();
    }

    _ProfileDetailViewTextEditingControllers.clear();
    _ProfileDetailViewFocusNodes.clear();
  }
}

extension ValueProperties on FormStateHelper {
  bool get hasAnyValidationMessage => this
      .fieldsValidationMessages
      .values
      .any((validation) => validation != null);

  bool get isFormValid {
    if (!_autoTextFieldValidation) this.validateForm();

    return !hasAnyValidationMessage;
  }

  String? get emailValue => this.formValueMap[EmailValueKey] as String?;
  String? get regionValue => this.formValueMap[RegionValueKey] as String?;
  String? get phoneNumberValue =>
      this.formValueMap[PhoneNumberValueKey] as String?;
  String? get lastNameValue => this.formValueMap[LastNameValueKey] as String?;
  String? get firstNameValue => this.formValueMap[FirstNameValueKey] as String?;

  set emailValue(String? value) {
    this.setData(
      this.formValueMap..addAll({EmailValueKey: value}),
    );

    if (_ProfileDetailViewTextEditingControllers.containsKey(EmailValueKey)) {
      _ProfileDetailViewTextEditingControllers[EmailValueKey]?.text =
          value ?? '';
    }
  }

  set regionValue(String? value) {
    this.setData(
      this.formValueMap..addAll({RegionValueKey: value}),
    );

    if (_ProfileDetailViewTextEditingControllers.containsKey(RegionValueKey)) {
      _ProfileDetailViewTextEditingControllers[RegionValueKey]?.text =
          value ?? '';
    }
  }

  set phoneNumberValue(String? value) {
    this.setData(
      this.formValueMap..addAll({PhoneNumberValueKey: value}),
    );

    if (_ProfileDetailViewTextEditingControllers.containsKey(
        PhoneNumberValueKey)) {
      _ProfileDetailViewTextEditingControllers[PhoneNumberValueKey]?.text =
          value ?? '';
    }
  }

  set lastNameValue(String? value) {
    this.setData(
      this.formValueMap..addAll({LastNameValueKey: value}),
    );

    if (_ProfileDetailViewTextEditingControllers.containsKey(
        LastNameValueKey)) {
      _ProfileDetailViewTextEditingControllers[LastNameValueKey]?.text =
          value ?? '';
    }
  }

  set firstNameValue(String? value) {
    this.setData(
      this.formValueMap..addAll({FirstNameValueKey: value}),
    );

    if (_ProfileDetailViewTextEditingControllers.containsKey(
        FirstNameValueKey)) {
      _ProfileDetailViewTextEditingControllers[FirstNameValueKey]?.text =
          value ?? '';
    }
  }

  bool get hasEmail =>
      this.formValueMap.containsKey(EmailValueKey) &&
      (emailValue?.isNotEmpty ?? false);
  bool get hasRegion =>
      this.formValueMap.containsKey(RegionValueKey) &&
      (regionValue?.isNotEmpty ?? false);
  bool get hasPhoneNumber =>
      this.formValueMap.containsKey(PhoneNumberValueKey) &&
      (phoneNumberValue?.isNotEmpty ?? false);
  bool get hasLastName =>
      this.formValueMap.containsKey(LastNameValueKey) &&
      (lastNameValue?.isNotEmpty ?? false);
  bool get hasFirstName =>
      this.formValueMap.containsKey(FirstNameValueKey) &&
      (firstNameValue?.isNotEmpty ?? false);

  bool get hasEmailValidationMessage =>
      this.fieldsValidationMessages[EmailValueKey]?.isNotEmpty ?? false;
  bool get hasRegionValidationMessage =>
      this.fieldsValidationMessages[RegionValueKey]?.isNotEmpty ?? false;
  bool get hasPhoneNumberValidationMessage =>
      this.fieldsValidationMessages[PhoneNumberValueKey]?.isNotEmpty ?? false;
  bool get hasLastNameValidationMessage =>
      this.fieldsValidationMessages[LastNameValueKey]?.isNotEmpty ?? false;
  bool get hasFirstNameValidationMessage =>
      this.fieldsValidationMessages[FirstNameValueKey]?.isNotEmpty ?? false;

  String? get emailValidationMessage =>
      this.fieldsValidationMessages[EmailValueKey];
  String? get regionValidationMessage =>
      this.fieldsValidationMessages[RegionValueKey];
  String? get phoneNumberValidationMessage =>
      this.fieldsValidationMessages[PhoneNumberValueKey];
  String? get lastNameValidationMessage =>
      this.fieldsValidationMessages[LastNameValueKey];
  String? get firstNameValidationMessage =>
      this.fieldsValidationMessages[FirstNameValueKey];
}

extension Methods on FormStateHelper {
  void setEmailValidationMessage(String? validationMessage) =>
      this.fieldsValidationMessages[EmailValueKey] = validationMessage;
  void setRegionValidationMessage(String? validationMessage) =>
      this.fieldsValidationMessages[RegionValueKey] = validationMessage;
  void setPhoneNumberValidationMessage(String? validationMessage) =>
      this.fieldsValidationMessages[PhoneNumberValueKey] = validationMessage;
  void setLastNameValidationMessage(String? validationMessage) =>
      this.fieldsValidationMessages[LastNameValueKey] = validationMessage;
  void setFirstNameValidationMessage(String? validationMessage) =>
      this.fieldsValidationMessages[FirstNameValueKey] = validationMessage;

  /// Clears text input fields on the Form
  void clearForm() {
    emailValue = '';
    regionValue = '';
    phoneNumberValue = '';
    lastNameValue = '';
    firstNameValue = '';
  }

  /// Validates text input fields on the Form
  void validateForm() {
    this.setValidationMessages({
      EmailValueKey: getValidationMessage(EmailValueKey),
      RegionValueKey: getValidationMessage(RegionValueKey),
      PhoneNumberValueKey: getValidationMessage(PhoneNumberValueKey),
      LastNameValueKey: getValidationMessage(LastNameValueKey),
      FirstNameValueKey: getValidationMessage(FirstNameValueKey),
    });
  }
}

/// Returns the validation message for the given key
String? getValidationMessage(String key) {
  final validatorForKey = _ProfileDetailViewTextValidations[key];
  if (validatorForKey == null) return null;

  String? validationMessageForKey = validatorForKey(
    _ProfileDetailViewTextEditingControllers[key]?.text,
  );

  return validationMessageForKey;
}

/// Updates the fieldsValidationMessages on the FormViewModel
void updateValidationData(FormStateHelper model) =>
    model.setValidationMessages({
      EmailValueKey: getValidationMessage(EmailValueKey),
      RegionValueKey: getValidationMessage(RegionValueKey),
      PhoneNumberValueKey: getValidationMessage(PhoneNumberValueKey),
      LastNameValueKey: getValidationMessage(LastNameValueKey),
      FirstNameValueKey: getValidationMessage(FirstNameValueKey),
    });
