/// class PD.Context.ContextManager

PD.Context.ContextManager =
Class.extend({

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

    _globalContext: null,
    _defaultContext: null,
    _contexts: null,
    
    //===================================
    // CONSTRUCTOR
    //===================================

    init: function () {

        this._contexts = new PD.Context.ContextCollection({
            remove: function (e) {
            }
        });

        this._globalContext = this.createContext({
            id: "GLOBAL"
        });

    },

    //===================================
    // PUBLIC PROPERTIES
    //===================================

    get_contexts: function () {
        return this._contexts;
    },

    get_default: function () {
        return this._defaultContext;
    },

    get_defaultId: function () {
        return this._defaultContext;
    },

    get_global: function () {
        return this._globalContext;
    },

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

    setDefault: function (contextId) {
    
        var context = this.get_contexts().getById(contextId);

        if (context != null) {
            this._defaultContext = context;
        }

    },

    createContext: function (createOptions) {
        
        var options = _.extend({
            id: uuid.v4(),
            name: "",
            parent: this.get_default(),
            makeDefault: true
        }, createOptions);

        var context = new PD.Context.Context(options);

        this.get_contexts().add(context);

        if (options.makeDefault) {
            this.setDefault(options.id);
        }

        return context;

    },

    destroyContext: function (options) {
        
        var context = this.getContextById(options.id)
            isDefault = this._isDefault(context);

        if (context != null) {
            this.get_contexts().remove(context);
            if (isDefault) {
                this._defaultContext = context.get_parent();
            }
        }
        
    },

    revolveContext: function (options) {

        var originalContext = this.get_default(),
            hasContext = options.context != null,
            isContext = _.isFunction(options.context.findClosestContext),
            hasFunction = options.fn,
            isFunction = _.isFunction(options.fn);

        if (hasContext && isContext) {
            this._defaultContext = options.context
        }

        if (hasFunction && isFunction) {
            options.fn(this._defaultContext);
        }

        this._defaultContext = originalContext;

    },

    getContextById: function (contextId) {
        return this.get_contexts().getById(contextId);
    },

    findClosestContext: function (tagsArr) {
        return this.get_default().findClosestContext(tagsArr);
    },

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

    _isDefault: function (context) {
        return context != null && this._defaultContext.get_id() === context.get_id();
    }

});