var vendorsScript = vendorsScript || function () {
    var isInit = false;
    var APP_URL = window.APP_URL + '/api/vendor/';
    var init = function () {
        if (isInit) return false;
        isInit = true;
        handler();
    };
    // Global AJAX Hook all AJAX Calls
    //////////////////////////////////////////////////////////////
    var _globalAjaxHook = function () {
        $._ajax_ = $.ajax;
        $.ajax = undefined;
        $.ajax = function (url, options) { // Request Handler for AJAX Calls
            loadingStart();
            var error, success;
            if (typeof url === 'string') {
                _success = options.complete;
                _error = options.error;
            } else {
                _success = url.success;
                _error = url.error;
            }
            url.success = function (response) {
                loadingEnd();
                if (url && url.data && url.data.draw) {
                } else {
                    if (response && response._metadata && response._metadata.outcomeCode === 4) {
                        window.location.reload();
                    }
                }
                _success(response);
            };
            url.error = function (jqXHR, textStatus, errorThrown) {
                setTimeout(function () {
                    loadingEnd();
                }, 3000);
            };
            return $._ajax_(url, options);
        }
    };

    var loadingStart = function () {
        $('#f-admin-overlay').show();
    };

    var loadingEnd = function () {
        $("#f-admin-overlay").hide();
    };

    var handler = function () {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        _globalAjaxHook();

        $('body').on('click', '#logout_btn', function () {
            var obj = Object.create(vendorsScript.LOGOUT);
            vendorsScript.ajaxCall(obj).done(function (response) {
                if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 0) {
                    window.location.href = 'login';
                }
            });
        });
    };

    var initValidator = function () {
        $('.modal').on('shown.bs.modal', function (e) {
            $(this).find('form[data-toggle=validator]').validator('destroy');
            $(this).find('form[data-toggle=validator]').validator();
        });
    };

    var initValidatorSimple = function () {
        $('form[data-toggle=validator]').validator('destroy');
        $('form[data-toggle=validator]').validator();
    };

    var showConfirmationAlert = function (defaultMessage, successCallback, cancelCallback) {
        swal({
            title: "Are you sure?",
            text: defaultMessage,
            type: "warning",
            closeOnClickOutside: false,
            showCancelButton: true,
            closeOnCancel: true,
            buttons: true,
            dangerMode: true
        }, function (isConfirm) {
            if (isConfirm) {
                if (typeof successCallback == 'function') successCallback();
            } else {
                if (typeof cancelCallback == 'function') cancelCallback();
            }
        });
    };

    var ajax = function (data) {
        var preparedAjaxObject = {
            method: data.method,
            url: data.url,
            data: data.data,
            beforeSend: function (xhr, opts) {
                xhr.setRequestHeader("Accept", 'application/json');
            },
            success: function (response) {
                if (response.hasOwnProperty('_metadata') && response._metadata.hasOwnProperty('outcomeCode') && response._metadata.outcomeCode === 9) {

                }
            }
        };

        if ('headers' in data) {
            preparedAjaxObject.headers = data.headers;
        }

        if ('dataType' in data) {
            preparedAjaxObject.dataType = data.dataType;
        }

        if ('cache' in data) {
            preparedAjaxObject.cache = data.cache;
        }

        if ('processData' in data) {
            preparedAjaxObject.processData = data.processData;
        }

        if ('contentType' in data) {
            preparedAjaxObject.contentType = data.contentType;
        }
        return $.ajax(preparedAjaxObject).fail(function (jqXHR, textStatus, errorThrown) {
        });
    };

    var LOGIN = {
        method: 'POST',
        url: APP_URL + 'auth/login'
    };

    var SIGN_UP = {
        method: 'POST',
        url: APP_URL + 'auth/signup'
    };

    var RESET_EMAIL = {
        method: 'POST',
        url: APP_URL + 'auth/resetemail'
    };

    var RESET_PASSWORD = {
        method: 'POST',
        url: APP_URL + 'auth/resetpassword'
    };

    var LOGOUT = {
        method: 'GET',
        url: APP_URL + 'auth/logout'
    };

    var DELETE_UPLOADED_VENDOR_LOGO = {
        method: 'POST',
        url: APP_URL + 'deleteuploadedvendorlogo'
    };

    var SAVE_VENDOR_STARTUP = {
        method: 'POST',
        url: APP_URL + 'savevendorstartup'
    };

    var GET_ENVIROMENTS_LIST = {
        method: 'GET',
        url: APP_URL + 'enviroments'
    };

    var GET_ENVIROMENT_SECTIONS = {
        method: 'GET',
        url: function (id) {
            return APP_URL + 'enviromentsections/' + id;
        }
    };

    var GET_SECTION_MODULES = {
        method: 'GET',
        url: function (id) {
            return APP_URL + 'sectionmodules/' + id;
        }
    };

    var DELETE_UPLOADED_PRODUCT_LOGO = {
        method: 'POST',
        url: APP_URL + 'deleteuploadedproductlogo'
    };

    var GET_PRODUCT_DETAILS = {
        method: 'GET',
        url: function (id) {
            return APP_URL + 'getproductdetails/' + id;
        }
    };

    var SAVE_PRODUCT = {
        method: 'POST',
        url: APP_URL + 'saveproduct'
    };

    var UPDATE_PRODUCT = {
        method: 'POST',
        url: function (id) {
            return APP_URL + 'updateproduct/' + id;
        }
    };

    var UPDATE_PRODUCT_STATUS = {
        method: 'POST',
        url: APP_URL + 'updateproductstatus'
    };

    var UPDATE_PRODUCT_FEATURED = {
        method: 'POST',
        url: APP_URL + 'updateproductfeatured'
    };

    var DELETE_PRODUCT = {
        method: 'POST',
        url: APP_URL + 'deleteproduct'
    };

    var CREATE_DEAL = {
        method: 'POST',
        url: APP_URL + 'createdeal'
    };

    var UPDATE_DEAL = {
        method: 'POST',
        url: function (id) {
            return APP_URL + 'updatedeal/' + id;
        }
    };

    var GET_DEAL_DETAILS = {
        method: 'GET',
        url: function (id) {
            return APP_URL + 'getdealdetail/' + id;
        }
    };

    var UPDATE_DEAL_STATUS = {
        method: 'POST',
        url: APP_URL + 'updatedealstatus'
    };

    var DELETE_DEAL = {
        method: 'POST',
        url: APP_URL + 'deletedeal'
    };

    var DELETE_UPLOADED_DEAL_LOGO = {
        method: 'POST',
        url: APP_URL + 'deleteuploadeddeallogo'
    };

    var GET_SETTINGS = {
        method: 'GET',
        url: APP_URL + 'settings'
    };

    var SAVE_SETTINGS = {
        method: 'POST',
        url: APP_URL + 'savesettings'
    };

    var SAVE_COMPANY_DETAILS = {
        method: 'POST',
        url: function (id) {
            return APP_URL + 'savecompanydetails/' + id;
        }
    };

    var CHANGE_PASSWORD = {
        method: 'POST',
        url: APP_URL + 'changepassword'
    };

    return {
        'init': init,
        'APP_URL': APP_URL,
        'ajaxCall': ajax,
        'initValidator': initValidator,
        'initValidatorSimple': initValidatorSimple,
        'showConfirmationAlert': showConfirmationAlert,
        'LOGIN': LOGIN,
        'SIGN_UP': SIGN_UP,
        'RESET_EMAIL': RESET_EMAIL,
        'RESET_PASSWORD': RESET_PASSWORD,
        'LOGOUT': LOGOUT,
        'DELETE_UPLOADED_VENDOR_LOGO': DELETE_UPLOADED_VENDOR_LOGO,
        'SAVE_VENDOR_STARTUP': SAVE_VENDOR_STARTUP,
        'GET_ENVIROMENTS_LIST': GET_ENVIROMENTS_LIST,
        'GET_ENVIROMENT_SECTIONS': GET_ENVIROMENT_SECTIONS,
        'GET_SECTION_MODULES': GET_SECTION_MODULES,
        'DELETE_UPLOADED_PRODUCT_LOGO': DELETE_UPLOADED_PRODUCT_LOGO,
        'GET_PRODUCT_DETAILS': GET_PRODUCT_DETAILS,
        'SAVE_PRODUCT': SAVE_PRODUCT,
        'UPDATE_PRODUCT': UPDATE_PRODUCT,
        'UPDATE_PRODUCT_STATUS': UPDATE_PRODUCT_STATUS,
        'UPDATE_PRODUCT_FEATURED': UPDATE_PRODUCT_FEATURED,
        'DELETE_PRODUCT': DELETE_PRODUCT,
        'CREATE_DEAL': CREATE_DEAL,
        'UPDATE_DEAL': UPDATE_DEAL,
        'GET_DEAL_DETAILS': GET_DEAL_DETAILS,
        'UPDATE_DEAL_STATUS': UPDATE_DEAL_STATUS,
        'DELETE_DEAL': DELETE_DEAL,
        'DELETE_UPLOADED_DEAL_LOGO': DELETE_UPLOADED_DEAL_LOGO,
        'GET_SETTINGS': GET_SETTINGS,
        'SAVE_SETTINGS': SAVE_SETTINGS,
        'SAVE_COMPANY_DETAILS': SAVE_COMPANY_DETAILS,
        'CHANGE_PASSWORD': CHANGE_PASSWORD
    };
}();
vendorsScript.init();
