/*
 *  Copyright 2016 Anyware Services
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * Content context for profile assignments.
 */
Ext.define('Ametys.plugins.cms.profiles.ContentTypeRightAssignmentContext', {
    extend: 'Ametys.plugins.coreui.profiles.AbstractRightAssignmentContext',
    
    /**
     * @private
     * @property {Ametys.plugins.cms.contenttype.ContentTypeTree} _tree The content type tree
     */
    /**
     * @private
     * @property {Boolean} _initialized true if the root node of the tree is initialized
     */
    _initialized: false,
    
    getComponent: function()
    {
        this._tree = this._createTree();
        return this._tree;
    },
    
    initialize: function()
    {
    	this._tree.getStore().load({ callback: Ext.bind(this._loadCb, this) });
    },

    /**
     * @private
     * The callback function after the store of the tree was loaded
     */
    _loadCb: function ()
    {
        var selection = this._tree.getSelection();
        if (selection.length > 0 && selection[0].get('id') == 'root')
        {
            // At second load, if the root was already selected, the onSelectionChanged has to be manually throwed
            this._onSelectionChanged(this._tree.getSelectionModel(), selection);
        }
        else
        {
    		this._tree.getSelectionModel().select(this._tree.getRootNode());
        }
    },
    
    /**
     * @private
     * Create the content type tree
     * @return {Ametys.plugins.cms.contenttype.ContentTypeTree} The created tree
     */
    _createTree: function()
    {
        return Ext.create('Ametys.plugins.cms.contenttype.ContentTypeTree', {
            border: false,
            listeners: {
                'selectionchange': Ext.bind(this._onSelectionChanged, this)
            },
            rootVisible: true,
            excludeReferenceTable: true,
            excludeMixin: true,
            rootLabel: "{{i18n PLUGINS_CMS_RIGHT_ASSIGNMENT_CONTEXT_CONTENTTYPE_TREEROOT_LABEL}}"
        });
    },
    
    /**
     * @private
     * Listener when the selection in the tree has changed
     * @param {Ext.selection.Model} model The selection model
     * @param {Ext.data.Model[]} records The selected records
     */
    _onSelectionChanged: function(model, records)
    {
        if (records.length > 0)
        {
            var record = records[0],
                id = record.get('id'),
                label = record.get('label')
                object = record.get('contentTypeId');
            
            var hintTextContext = "{{i18n PLUGINS_CMS_RIGHT_ASSIGNMENT_CONTEXT_CONTENTTYPE_HINT_ALL_CONTENT_TYPE}}";
            if (label)
            {
                hintTextContext = "{{i18n PLUGINS_CMS_RIGHT_ASSIGNMENT_CONTEXT_CONTENTTYPE_HINT_CONTENT_TYPE}} <b>" + label + "</b>";
            }
            
            this._changeObjectContext('/content-types' + (object ? '/' + object : ''), hintTextContext, false, Ext.isEmpty(object));
        }
    }
});