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

/**
 * Reference tables context for profile assignments.
 */
Ext.define('Ametys.plugins.cms.profiles.ReferenceTableRightAssignmentContext', {
    extend: 'Ametys.plugins.coreui.profiles.AbstractRightAssignmentContext',
    
    /**
     * @private
     * @property {Ametys.plugins.cms.contenttype.ContentTypeTree} _tree The content type tree
     */

    /**
     * @private
     * @property {Ext.container.Container} _container The component of this right assignment context
     */

    getComponent: function()
    {
        this._tree = this._createTree();

        var items = [{
                xtype: 'radiofield',
                boxLabel  : "{{i18n PLUGINS_CMS_RIGHT_ASSIGNMENT_CONTEXT_REFERENCETABLES_RADIO_OPTION_ALL}}",
                name      : 'radio-content',
                itemId: 'radio-content-all',
                checked: true,
                inputValue: 'all',
                style: {
                    marginLeft: '5px'
                }
            }, {
                xtype: 'radiofield',
                boxLabel  : "{{i18n PLUGINS_CMS_RIGHT_ASSIGNMENT_CONTEXT_REFERENCETABLES_RADIO_OPTION_CONTENTTYPE}}",
                name      : 'radio-content',
                inputValue: 'content-type',
                itemId: 'radio-content-contenttype',
                handler: this._onSelect,
                scope: this,
                style: {
                    marginLeft: '5px'
                }
            }, this._tree
        ];
        
        this._container = Ext.create('Ext.container.Container', {
            layout: {
                type: "vbox",
                align: "stretch"
            },
            items: items
        });
        
        return this._container;
    },

        /**
     * @private
     * Handler when selection an option
     * @param {Ext.form.Checkbox} contentTypeCheckbox The content type checkbox
     * @param {Boolean} contentTypeChecked Is content type checked?
     */
    _onSelect: function (contentTypeCheckbox, contentTypeChecked)
    {
        if (contentTypeChecked)
        {
            // Enable search
            this._switchToContentTypeMode();
        }
        else
        {
            // Disable search
            this._switchToAllContentsMode();
        }
    },

    initialize: function()
    {
        this._switchToAllContentsMode();
        this._tree.getStore().load();
    },
    
    /**
     * @private
     * Switch to the "all contents" mode
     */
    _switchToAllContentsMode: function()
    {
        this._container.down('#radio-content-all').setValue(true);
        this._changeObjectContext("/reference-tables", "<b>{{i18n PLUGINS_CMS_RIGHT_ASSIGNMENT_CONTEXT_REFERENCETABLES_HINT_ALL}}</b>", false, true);
        
        // Disable search result grid
        this._tree.getSelectionModel().deselectAll();
        this._tree.mask("{{i18n PLUGINS_CMS_RIGHT_ASSIGNMENT_CONTEXT_REFERENCETABLES_MASK_CONTENTTYPE}}", 'ametys-mask-unloading');
    },

    /**
     * @private
     * Swith to "content type" mode
     */
    _switchToContentTypeMode: function()
    {
        this._container.down('#radio-content-contenttype').setValue(true);
        this._tree.unmask();
        this._tree.getSelectionModel().select(this._tree.getRootNode().getChildAt(0));
    },
    
        /**
     * @private
     * Create the explorer tree
     * @return {Ametys.plugins.cms.contenttype.ContentTypeTree} The created explorer tree
     */
    _createTree: function()
    {
        return Ext.create('Ametys.plugins.cms.contenttype.ContentTypeTree', {
            border: false,
            listeners: {
                'selectionchange': Ext.bind(this._onSelectionChanged, this)
            },
            flex: 1,
            rootVisible: false,
            includeReferenceTableOnly: true,
            excludeMixin: 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');
            
            hintTextContext = "{{i18n PLUGINS_CMS_RIGHT_ASSIGNMENT_CONTEXT_REFERENCETABLES_HINT_CONTENT}} <b>" + label + "</b>";
            
            this._changeObjectContext('/content-types' + (object ? '/' + object : ''), hintTextContext, false, Ext.isEmpty(object));
        }
    }

});