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

/**
 * Singleton class defining the actions related to the view parameters
 * @private
 */
Ext.define('Ametys.plugins.web.parameters.view.ViewParametersActions', {
	singleton: true,
	
	/**
	 * Configure view parameters
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
	 */
	configure: function(controller)
	{
		var target = controller.getMatchingTargets()[0];
		var pageId = target.getParameters().id;
		var zoneName = target.getParameters()['zone-name'];
        var zoneItemId = target.getParameters()['zoneitem-id'];
		
        Ametys.plugins.web.parameters.view.ViewParametersDialogHelper.open(
        		pageId, 
        		zoneName, 
        		zoneItemId, 
        		this._saveParametersCb);
	},
	
	/**
	 * Configure template view parameters
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
	 */
	configureTemplate: function(controller)
	{
		var target = controller.getMatchingTargets()[0];
		var pageId = target.getParameters().id;
		
        Ametys.plugins.web.parameters.view.ViewParametersDialogHelper.open(
        		pageId, 
        		null, 
        		null, 
        		this._saveParametersCb);
	},
    
    /**
     * @private
     * Callback after saving view parameters
     * @param {String} pageId The page id
     * @param {String} zoneName The zone name (can be null)
     * @param {String} zoneItemId The zone item id (can be null)
     */
    _saveParametersCb: function (pageId, zoneName, zoneItemId)
    {
        Ext.create("Ametys.message.Message", {
            type: Ametys.message.Message.MODIFIED,
            targets: {
                id: pageId.startsWith("sitemap://") ? Ametys.message.MessageTarget.SITEMAP : Ametys.message.MessageTarget.PAGE,
                parameters: pageId.startsWith("sitemap://") ? { id: pageId } : { ids: [pageId] }
            }
        });
    }
});