/**
 * Base.Box 1.0
 *
 * Requiere
 *     Base 1.0
 */
$(function() {
    $.extend(Base, {
        Loading: {

            _buildMessage: function(options){

                message = options.message;

                if(options.spinner){
                    message = '<span class="spinner-border ' + options.spinnerClass + '"></span>' + message;
                }

                if(options.blockMessage){
                    message = '<div class="blockui-message">' + message + '</div>';
                }

                return message;
            },

            show: function (options) {

                var _i18n = {};

                _i18n = $.extend(true, _i18n, Base.i18n.loading);

                if(undefined == options)
                    options = {};

                if (options._i18n !== undefined && options._i18n != null) {
                    _i18n = $.extend(true, _i18n, options._i18n);
                }

                var defaultOptions = {
                    zIndex: false,
                    message: '<div class="blockui-message"><span class="spinner-border text-primary"></span> Cargando...</div>',
                    overlayClass: null,
                    overflow: 'hidden',
                    spinner: true,
                    spinnerClass: 'text-primary',
                    blockMessage: true
                };

                var finallyOptions = $.extend(true, defaultOptions, options);
                finallyOptions.message = this._buildMessage(finallyOptions);
                var targetContent = document.querySelector("#kt_content");
                var targetPopup = document.querySelector(".bootbox-dialog .modal-content");

                var target = targetPopup ? targetPopup : targetContent;

                var blockUI = KTBlockUI.getInstance(target);

                if(!blockUI)
                    blockUI = new KTBlockUI(target, finallyOptions);

                if (!blockUI.isBlocked())
                    blockUI.block();

                return blockUI;
            },

            showInBlock: function (target, options) {

                var _i18n = {};

                if(undefined == options)
                    options = {};

                _i18n = $.extend(true, _i18n, Base.i18n.loading);

                if (options._i18n !== undefined && options._i18n != null) {
                    _i18n = $.extend(true, _i18n, options._i18n);
                }

                var defaultOptions = {
                    zIndex: false,
                    message: '<div class="blockui-message"><span class="spinner-border text-primary"></span> Cargando...</div>',
                    overlayClass: null,
                    overflow: 'hidden'
                };

                var finallyOptions = $.extend(true, defaultOptions, options);
                finallyOptions.message = this._buildMessage(finallyOptions);
                var targetElement = document.querySelector(target);
                var blockUI = KTBlockUI.getInstance(targetElement);

                if(!blockUI)
                    blockUI = new KTBlockUI(targetElement, finallyOptions);

                if (!blockUI.isBlocked())
                    blockUI.block();

                return blockUI;
            },

            hide: function(){

                var targetContent = document.querySelector("#kt_content");
                var targetPopup = document.querySelector(".bootbox-dialog .modal-content");
                var target = targetPopup ? targetPopup : targetContent;

                var blockUI = KTBlockUI.getInstance(target);

                if(blockUI){
                    blockUI.release();
                    blockUI.destroy();
                }

            },

            hideInBlock: function(target){

                var targetElement = document.querySelector(target);
                var blockUI = KTBlockUI.getInstance(targetElement);

                if(blockUI){
                    blockUI.release();
                    blockUI.destroy();
                }

            },

            progress: function(target, options){
                var _i18n = {};

                _i18n = $.extend(true, _i18n, Base.i18n.loading);

                if (options._i18n !== undefined && options._i18n != null) {
                    _i18n = $.extend(true, _i18n, options._i18n);
                }

                var defaultOptions = {
                    opacity: 0.05,
                    state: 'dark',
                    centerX: true,
                    centerY: true,
                    shadow: true,
                    width: 'auto',
                    overlayColor: '#000000',
                    type: 'v2',
                    state: 'success',
                    message: '',
                    size: 'lg',
                    closeButton: false
                };

                var finallyOptions = $.extend(true, defaultOptions, options);

                KTApp.progress(target, finallyOptions);
            },

            unprogress: function(target){

                KTApp.unprogress(target);
            },

            _qxLoading: function (box) {

                return {

                    ver: function () {

                        console.log("Base.Loading 1.0");
                        console.log("Requiere: Base 1.0");
                    }
                }
            }
        }
    });

    $.fn.QxLoading = function () {

        var loading = this;

        return Base.Box._qxLoading(loading);
    }
    
}); 