/*
 *  Copyright 2024 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.
 */

/**
 * Web content type context for profile assignments.
 * This overrides split the context in two category, global and site context
 */
Ext.define('Ametys.plugins.web.profiles.WebContentTypeRightAssignmentContext', {
    extend: 'Ametys.plugins.cms.profiles.ContentTypeRightAssignmentContext',
        
    /**
     * @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: false,
            excludeReferenceTable: true,
            excludeMixin: true,
            separateGlobal: true,
        });
    },
    
    /**
     * @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')
                global = record.get('global');
            
            var hintTextContext = global
                ? "{{i18n plugin.web:PLUGINS_WEB_RIGHT_ASSIGNMENT_CONTEXT_GLOBAL_CONTENTTYPE_HINT_ALL}}"
                : "{{i18n plugin.web:PLUGINS_WEB_RIGHT_ASSIGNMENT_CONTEXT_LOCAL_CONTENTTYPE_HINT_ALL}}";
            if (label)
            {
                hintTextContext = "{{i18n plugin.cms:PLUGINS_CMS_RIGHT_ASSIGNMENT_CONTEXT_CONTENTTYPE_HINT_CONTENT_TYPE}} <b>" + label + "</b>";
            }
            var prefix = global ? '/content-types' : '/site-content-types';
            this._changeObjectContext(prefix + (object ? '/' + object : ''), hintTextContext, false, Ext.isEmpty(object));
        }
    },
    
    _loadCb: function()
    {
        this._tree.setSelection(this._tree.getRootNode().firstChild)
    }
});