var authScript = authScript || function () {

        var isInit = false;
        var init = function () {
            if (isInit) return;
            isInit = true;
            handler();
        };

        var handler = function () {
            // globalScript.initValidatorSimple();
            $("body").on('submit', '#login_form', function (e) {
                e.preventDefault();
                if($('#login_btn').hasClass('disabled')){
                    return false;
                }
                $('.login_errors').hide();
                $('.login_errors ul').html('');
                var email = $('#login_email').val();
                var password = $('#login_password').val();
                var obj = Object.create(globalScript.LOGIN);
                obj.data = {
                    'email': email,
                    'password': password
                };

                globalScript.ajaxCall(obj).done(function (response) {
                    if(response.errors && response.errors.length !==0 && response._metadata.outcomeCode === 2){
                        $('.login_errors').show();
                        $.each(response.errors, function (k, v) {
                            $('.login_errors ul').append('<li>' + v + '</li>');
                        });
                    }
                    if(response.errors && response.errors.length !==0 && response._metadata.outcomeCode === 3){
                        $('.login_errors').show();
                        $.each(response.errors, function (k, v) {
                            $('.login_errors ul').append('<li>' + v.message + '</li>');
                        });
                    }
                    if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                        window.location.href = 'dashboard';
                    }
                });
            });

            $('body').on('submit', '#signup_form', function (e) {
                e.preventDefault();
                if ($('#signup_btn').hasClass('disabled')) {
                    return false;
                }
                $('#signup_btn').addClass('disabled');
                $('.signup_errors').hide();
                $('.signup_errors ul').html('');
                var userName = $('#user_name').val();
                var userEmail = $('#user_email').val();
                var userPassword = $('#user_password').val();
                var obj = Object.create(globalScript.SIGN_UP);
                obj.data = {
                    'name': userName,
                    'email': userEmail,
                    'password': userPassword,
                    'type': 'back_assets'
                };

                globalScript.ajaxCall(obj).done(function (response) {
                    if (response.errors && response.errors.length !== 0) {
                        $('.signup_errors').show();
                        $('#signup_btn').removeClass('disabled');
                        $.each(response.errors, function (k, v) {
                            $('.signup_errors ul').append('<li>' + v + '</li>');
                        });
                    } else {
                        if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                            window.location.reload();
                            // $('.success_texts').html(response._metadata.message);
                            // $('.success_texts').show();
                            // $('#signup_form').trigger("reset");
                        }
                    }
                });
            });

            $('body').on('submit', '#forgot_password_form', function (e) {
                e.preventDefault();
                if ($('#forgot_btn').hasClass('disabled')) {
                    return false;
                }
                $('.forgot_errors').hide();
                $('.forgot_errors ul').html('');
                $('.success_texts').html('').hide();
                var resetEmail = $('#reset_email').val();
                var obj = Object.create(globalScript.RESET_EMAIL);
                obj.data = {
                    'email': resetEmail
                };

                globalScript.ajaxCall(obj).done(function (response) {
                    if (response.errors && response.errors.length !== 0) {
                        $('.forgot_errors').show();
                        $.each(response.errors, function (k, v) {
                            $('.forgot_errors ul').append('<li>' + v + '</li>');
                        });
                    } else {
                        if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                            $('.success_texts').html(response._metadata.message).show();
                            $('#forgot_password_form').trigger("reset");
                        } else {
                            $('.forgot_errors').show();
                            $('.forgot_errors ul').append('<li>' + response._metadata.message + '</li>');
                        }
                    }
                });
            });

            $('body').on('submit', '#reset_password_form', function (e) {
                e.preventDefault();
                if ($('#reset_btn').hasClass('disabled')) {
                    return false;
                }
                $('.reset_errors').hide();
                $('.reset_errors ul').html('');
                $('.success_texts').html('').hide();
                var resetToken = $('#reset_token').val();
                var password = $('#new_password').val();
                var passwordConfirmation = $('#new_password_confirmation').val();
                var obj = Object.create(globalScript.RESET_PASSWORD);
                obj.data = {
                    'token': resetToken,
                    'password': password,
                    'password_confirmation': passwordConfirmation
                };

                globalScript.ajaxCall(obj).done(function (response) {
                    if (response.errors && response.errors.length !== 0) {
                        $('.reset_errors').show();
                        $.each(response.errors, function (k, v) {
                            $('.reset_errors ul').append('<li>' + v + '</li>');
                        });
                    } else {
                        if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                            window.location.reload();
                        } else {
                            $('.reset_errors').show();
                            $('.reset_errors ul').append('<li>' + response._metadata.message + '</li>');
                        }
                    }
                });
            });
        };

        return {
            'init': init
        }

    }();

$(document).ready(function () {
    authScript.init();
});
