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

/**
 * This class provides a widget to select one ore more program items.<br>
 * This widget is registered for fields of type Ametys.form.WidgetManager#TYPE_CONTENT.
 */
Ext.define('Ametys.odf.widget.SelectProgramItemOfSameContext', {
    extend : 'Ametys.cms.form.widget.SelectContent',
    
    xtype: 'edition.select-program-item-of-same-context',
    
    /**
     * @cfg {String} [targetCatalogCriteria="reference-catalog-eq"] The name of the catalog criteria in #cfg-modelId search model 
     */
    targetCatalogCriteria: "reference-catalog-eq",
    
    /**
     * @protected
     * Get additional values used to load the combobox with values for search
     * @return {Object} Non empty
     */
    _getAdditionalLoadValues: function()
    {
        // SelectContent already provide the lang if necessary
        let values = this.callParent(arguments);
        
        if (this.contentInfo && this.contentInfo.catalog)
        {
            values[this.targetCatalogCriteria] = this.contentInfo.catalog;
        }
        
        return values;
    },
    
    updateAdditionalWidgetsConf: function(config)
    {
        this.callParent(arguments);
        
        // If a context is provided with a content id refresh the context catalog
        if (this.contentInfo && this.contentInfo.contentId)
        {
            Ametys.data.ServerComm.callMethod({
                role: "org.ametys.odf.catalog.CatalogsManager",
                methodName: "getContentCatalog",
                parameters: [this.contentInfo.contentId],
                callback: {
                    handler: this._getContentCatalogCb,
                    scope: this,
                },
                waitMessage: false // The result is only necessary when creating
                // a skill from this widget.
            });
        }
    },
    
    _getContentCatalogCb: function(catalogName, args)
    {
        this.contentInfo.catalog = catalogName;
    },

});