/// class PD.Web.UI.Window

PD.Web.UI.Window = Class.extend({

    //================================
    // FIELDS
    //================================

    _id: null,
    _kendo: null,
    _$content: null,
    _$container: null,
    _options: null,

    //================================
    // CONSTRUCTOR
    //================================

    init: function (options) {

        var that = this;

        this._id = uuid.v4();
        this._setOptions(options);
        this._frame(this._options.config.markup);
        this._activate();

    },

    //================================
    // PROPERTIES
    //================================

    get_id: function () {
        return this._id;
    },

    get_content: function () {
        return this._$content;
    },

    get_container: function () {
        return this._$container;
    },

    get_buttonRestore: function () {
        return this.get_container().find(".k-i-restore");
    },

    get_buttonMaximize: function () {
        return this.get_container().find(".k-i-maximize");
    },

    get_buttonClose: function () {
        return this.get_container().find(".k-i-close");
    },

    //================================
    // EVENT HANDLERS
    //================================

    _onDeactivate: function () {
        this._kendo.destroy();
    },

    _onRefresh: function () {

        var fn = this._options.events["refresh"];

        if ($.isFunction(fn)) {
            fn();
        }

        this._kendo.center();
        this._kendo.trigger("resize");
        this._onResize();

    },

    _onResize: function () {

        var fn = this._options.events["resize"];

        if ($.isFunction(fn)) {
            $.proxy(fn, this)();
        }

    },

    _onBrowserResize: function () {

        if (!this._kendo.options.isMaximized) {
            this._kendo.center();
        }

    },

    //================================
    // PUBLIC METHODS
    //================================

    close: function (options) {

        if (options === undefined) {
            this._invokeCallback("cancel", {});
        }
        else {
            this._invokeCallback(options.command, options.data);
        }

        this._kendo.destroy();

    },

    addContent: function (content) {
        this.get_content().empty();
        this.get_content().append(content);
    },

    //================================
    // PRIVATE METHODS
    //================================

    _setOptions: function (options) {

        var that = this;

        this._options = $.extend(true, {
            config: {
                modal: true,
                content: null,
                markup: null,
                minHeight: 50
            },
            events: {},
            callbacks: {}
        }, options);

    },

    _frame: function (content) {

        var frame = [
                "<div>",
                    content ? content : "<table class=\"-pd-window-loading\" style='height:100%;width:100%;'><tr><td style='text-align:center;'>Loading...</td></tr></table>",
                "</div>"
        ];

        this._$content = $(frame.join("")).appendTo("body DIV[data-role=windows]");

    },

    _activate: function () {

        var that = this,
            closeFn = this._options.events["close"],
            config = $.extend(this._options.config, {
                animation: false,
                resize: $.proxy(PD.BCL.Util.debounce(this._onResize, 200), this),
                refresh: $.proxy(this._onRefresh, this),
                deactivate: $.proxy(this._onDeactivate, this),
                visible: false,
                close: function () {
                    if (_.isFunction(closeFn)) {
                        _.bind(closeFn, that)({});
                    }
                    else {
                        that.close();
                    }
                }
            });

        this._kendo = this.get_content().kendoWindow(config).data("kendoWindow");
        this._$container = this._$content.parent();
        this._$container.attr("ID", this.get_id());
        this._$container.addClass("-jadejs-window -pd-scope-capture");
        
        setTimeout(() => {
            this._kendo.center();
            this._kendo.open();
            this._kendo.refresh();
            this._setWindowButtonIcons();
        }, 50);

        PD.context.get_global().get_events().publish("Window:Resized", this._onBrowserResize, this);

    },

    _invokeCallback: function (command, args) {

        var callbacks = this._options["callbacks"],
            callback = null;

        if (command === null && $.isPlainObject(callbacks)) {
            return;
        }

        callback = callbacks[command];

        if ($.isFunction(callback)) {
            callback(this, args);
        }

    },

    _invokeSimpleCallback: function (command, args) {

        var callback = this._options[command];

        if ($.isFunction(callback)) {
            callback(this, args);
        }

    },

     _setWindowButtonIcons: function () {
        this.get_buttonClose().addClass("icofont-close");
        this.get_buttonClose().empty();
        this.get_buttonMaximize().addClass("icofont-maximize");
        this.get_buttonMaximize().empty();
        this.get_buttonRestore().addClass("icofont-restore");
        this.get_buttonRestore().empty();
    }

});
