/// class PD.Manage.Module
// using PD.Web.UI.Controls.Control

PD.Manage.Module = PD.Web.UI.Controls.Control.extend({

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

    _tab: null,
    _data: null,
    _onErrorCallback: null,
    _onReadyCallback: null,
    _eventSubscriptions: null,

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

    init: function (options) {
        this._super();

        this._data = new PD.Manage.ModuleModel($.extend({
            uid: this.get_id(),
            id: (options && options.id) ? options.id : uuid.v4(),
            label: "",
            fixed: true,
            color: null,
            allowClose: true,
            hasChanges: false,
            hasToolbar: true,
            route: "/",
            fallback: "#/overview"
        }, options));

        this._onReadyCallback = options.ready;
        this._onErrorCallback = options.error;

        this.prepare(options);

    },

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

    data: function (name, value) {

        if (name == null) {
            return this._data;
        }

        if (name != null && value === undefined) {
            return this._data.get(name);
        }

        if (name != null && value !== undefined) {
            return this._data.set(name, value);
        }

    },

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

    onFocus: function () {
        // overridable
    },
    
    onSave: function () {
        // overridable
    },

    onReady: function () {
        // overridable
    },

    onPrepare: function () {
        this.ready();
    },

    _onCompleteExit: function () {
        var that = this;
        PD.context.get_global().get_events().publish("Module:Loaded", this.data());
        setTimeout(function () {
            that.focus();
        }, 100);
    },

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

    focus: function () {
        this.get_node().show();
        PD.context.get_global().get_events().publish("Module:Focused", this);
        this.createContext();
        this.onFocus();
    },

    save: function () {
        this.onSave();
    },

    unload: function () {
        this.remove();
        this.destroyContext();
        PD.context.get_global().get_events().publish("Module:Unloaded", this);
    },

    //===================================
    // PROTECTED METHODS
    //===================================

    ready: function () {
        this.createContext();
        if ($.isFunction(this._onReadyCallback)) {
            this._onReadyCallback(this);
        }
        this.onReady();
    },

    prepare: function (options) {
        this.onPrepare();
    },

    error: function (message) {
        if ($.isFunction(this._onErrorCallback)) {
            this._onErrorCallback(this, message);
        }
    },

    createContext: function () {

        let id = this.get_id();
        let label = this.data("label");

        PD.context.createContext({
            id: id,
            name: label,
            parent: PD.context.get_global(),
            tags: ["module"]
        });

        let contextExists =
            PDJS.Application
            .instance
            .contexts
            .getContextById(id) != null;

        if (!contextExists) {
            PDJS.Application
                .instance
                .contexts
                .createContext({
                    id: id,
                    name: label,
                    parent: PDJS.Application.instance.contexts.global
                });
        }
        else {
            PDJS.Application
                .instance
                .contexts
                .setCurrent(id);
        }
        
    },

    destroyContext: function () {

        let id = this.get_id();

        PD.context.destroyContext({
            id: id
        });

        PDJS.Application
            .instance
            .contexts
            .destroyContext({
                id: id
            });

    }

});