import { showValidationErrors, clearValidationErrors, isValidEmail } from '../utils/validation';

var Wick = window.Wick || {};

Wick.NotifyMeSubscription = {
    NOTIFY_ME_EVENT: 'NOTIFY_ME',
    HIDDEN_CLASS_NAME: 'd-none',
    el: {
        notifyMePopup: '#modal-notify-me',
        notifyMeBtn: '.btn--notify-open',

        form: '#modal-notify-me .notify-me-form',
        emailInput: '#modal-notify-me #notify-me-email',
        stateForm: '#modal-notify-me [data-notify-state="form"]',
        stateSuccess: '#modal-notify-me [data-notify-state="success"]',
        headerForm: '#modal-notify-me [data-notify-header="form"]',
        headerSuccess: '#modal-notify-me [data-notify-header="success"]',
        productId: '#product-code-val',
    },
    defaultErrorMessage: 'Something went wrong',
    invalidEmailMessage: 'Please enter a valid email address',
    showCustomModal() {
        $(Wick.NotifyMeSubscription.el.notifyMePopup).modal('show');
    },
    getEmail() {
        return ($(Wick.NotifyMeSubscription.el.emailInput).val() || '').toString().trim();
    },
    isEmailValid() {
        const email = Wick.NotifyMeSubscription.getEmail();

        return isValidEmail(email);
    },
    showFormState() {
        $(Wick.NotifyMeSubscription.el.stateSuccess).addClass(
            Wick.NotifyMeSubscription.HIDDEN_CLASS_NAME
        );
        $(Wick.NotifyMeSubscription.el.stateForm).removeClass(
            Wick.NotifyMeSubscription.HIDDEN_CLASS_NAME
        );

        $(Wick.NotifyMeSubscription.el.headerSuccess).addClass(
            Wick.NotifyMeSubscription.HIDDEN_CLASS_NAME
        );
        $(Wick.NotifyMeSubscription.el.headerForm).removeClass(
            Wick.NotifyMeSubscription.HIDDEN_CLASS_NAME
        );
    },
    showSuccessState() {
        $(Wick.NotifyMeSubscription.el.stateForm).addClass(
            Wick.NotifyMeSubscription.HIDDEN_CLASS_NAME
        );
        $(Wick.NotifyMeSubscription.el.stateSuccess).removeClass(
            Wick.NotifyMeSubscription.HIDDEN_CLASS_NAME
        );

        $(Wick.NotifyMeSubscription.el.headerForm).addClass(
            Wick.NotifyMeSubscription.HIDDEN_CLASS_NAME
        );
        $(Wick.NotifyMeSubscription.el.headerSuccess).removeClass(
            Wick.NotifyMeSubscription.HIDDEN_CLASS_NAME
        );
    },
    resetValidation() {
        clearValidationErrors(Wick.NotifyMeSubscription.el.form);
    },
    validateForm() {
        Wick.NotifyMeSubscription.resetValidation();

        if (!Wick.NotifyMeSubscription.isEmailValid()) {
            showValidationErrors(
                Wick.NotifyMeSubscription.el.form,
                Wick.NotifyMeSubscription.invalidEmailMessage
            );
            return false;
        }

        return true;
    },
    showError(err = {}) {
        const msg = err.errMsg || err.errorMessage || Wick.NotifyMeSubscription.defaultErrorMessage;

        showValidationErrors(Wick.NotifyMeSubscription.el.form, msg);
    },
    init() {
        $(document).on('click', Wick.NotifyMeSubscription.el.notifyMeBtn, function (e) {
            e.preventDefault();
            Wick.NotifyMeSubscription.showCustomModal();
        });

        $(Wick.NotifyMeSubscription.el.notifyMePopup).on('hidden.bs.modal', function () {
            Wick.NotifyMeSubscription.showFormState();
            clearValidationErrors(Wick.NotifyMeSubscription.el.form);
        });
    },
};

$(document).ready(function () {
    Wick.NotifyMeSubscription.init();
});
