/// 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: "/"
        }, 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 () {

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

    },

    destroyContext: function () {

        PD.context.destroyContext({
            id: this.data("id")
        });

    }

});