import {isValidFieldSize, isValidPasswordSize, updateStylingError} from "./utils/validation";

var Wick = window.Wick || {};

Wick.ChangePassword = {
  el: {
    changePasswordForm: '.change-password__form',
    fieldRow: '.form-row',
    $currentPassword: $('#currentPassword'),
    $newPassword: $('#newPassword'),
    $confirmPassword: $('#confirmPassword'),
    formInvalidClass: 'form_invalid',
    fieldError: '.form-row.form-row_validation-error',
    btnCancel: '[data-action="cancel-form"]',
    btnReset: '[data-action="reset-details"]',
    saveForm: '[data-action="save-form"]'
  },
  errorMessages: {
    errorCurrentPassword: 'Please enter your current password',
    errorNewPassword: 'Please enter a strong password (at least 6 characters)',
    errorPasswordMismatch: 'Password and password confirmation do not match'
  },
  defaultNotificationConfig: {
    container: '.change-password .globalMessages .container',
    closeBtn: true,
    noScrolling: true,
  },
  getFieldsConfigurations: function () {
    return [
      {
        input: Wick.ChangePassword.el.$currentPassword,
        validator: isValidFieldSize,
        error: Wick.ChangePassword.errorMessages.errorCurrentPassword,
      },
      {
        input: Wick.ChangePassword.el.$newPassword,
        validator: isValidPasswordSize,
        error: Wick.ChangePassword.errorMessages.errorNewPassword,
      },
      {
        input: Wick.ChangePassword.el.$confirmPassword,
        validator: isValidPasswordSize,
        error: Wick.ChangePassword.errorMessages.errorNewPassword,
      },
    ];
  },
  validateInput: function (input, validatorFunction, errorMsg) {
    const value = $(input).val();

    const isValid = validatorFunction(value);
    updateStylingError(input, isValid, errorMsg);

    return isValid;
  },
  validateForm: function () {
    let isValid = true;

    const fields = Wick.ChangePassword.getFieldsConfigurations();

    fields.forEach((field) => {
      isValid =
        Wick.ChangePassword.validateInput(field.input, field.validator, field.error) && isValid;
    });

    if (isValid) {
      const newPassword = Wick.ChangePassword.el.$newPassword.val();
      const confirmPassword = Wick.ChangePassword.el.$confirmPassword.val();

      if (newPassword !== confirmPassword) {
        isValid = false;
        updateStylingError(
          Wick.ChangePassword.el.$confirmPassword,
          false,
          Wick.ChangePassword.errorMessages.errorPasswordMismatch
        );
      }
    }

    return isValid;
  },
  handleChangePasswordFormSubmit: function () {
    $(Wick.ChangePassword.el.changePasswordForm).on('submit', function (event) {
      event.preventDefault();

      if (Wick.ChangePassword.validateForm()) {
        Wick.LeaveEvent.removeLeaveEvent();
        event.target.submit();
      }
    });
  },
  hasUnsavedChanges() {
    const currentState = Wick.LeaveEvent.getState(Wick.ChangePassword.el.changePasswordForm);

    return !_.isEqual(currentState, Wick.LeaveEvent.initialFormState);
  },
  reloadPage() {
    location.reload();
  },
  init() {
    Wick.LeaveEvent.bindEvent(
      Wick.ChangePassword.el.changePasswordForm,
      Wick.ChangePassword.el.btnCancel,
      Wick.ChangePassword.el.btnReset,
      Wick.ChangePassword.reloadPage
    );

    Wick.Pwdstrength.bindPStrength();
    Wick.ChangePassword.handleChangePasswordFormSubmit();
  },
};

$(document).ready(function () {
  if (!$(Wick.ChangePassword.el.changePasswordForm).length) {
    return;
  }

  Wick.ChangePassword.init();
});
