/*
 *  Copyright 2013 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 helper provides a dialog box to select a view of a content
 */
Ext.define('Ametys.cms.uihelper.SelectContentView', {
	singleton: true,
	
	/**
	 * @property {Function} _cbFn The call back function to call after choosing contents
	 * @private
	 */
	/**
	 * @property {String} _contentId The id of content
	 * @private
	 */
	/**
	 * @property {Ametys.window.DialogBox} _box The dialog box
	 * @private
	 */
	
	/**
	 * Configure and open the dialog box to select view.
     * @param {Object} config The configuration options.
     * @param {Object} config.contentId The id of content
     * @param {String} config.icon The full path to icon (16x16 pixels) for the dialog box
     * @param {String} config.title The title of the dialog box.
     * @param {String} [config.helpmessage] The message displayed at the top of the dialog box
     * @param {Function} config.callback The callback function invoked when view is selected. The callback function will received the following parameters:
     * @param {String} config.callback.contentId The id of content
     * @param {String} config.callback.viewName The name of chosen view.
     * @param {String} config.callback.viewLabel The label of chosen view.
	 */
	open: function (config)
	{
		config = config || {};
	    
		this._contentId = config.contentId;
		this._cbFn = config.callback;
		
		this._createDialogBox(config.title, config.icon, config.helpmessage || '');
		this._box.show();
		
		var combo = this._box.down('#viewName');
		combo.getStore().removeAll();
		
		// Get the available views
		Ametys.data.ServerComm.callMethod({
			role: "org.ametys.cms.repository.ContentDAO",
			methodName: "getContentViews",
			parameters: [this._contentId, false],
			callback: {
				handler: this._getContentViewsCb,
			 	scope: this
			},
			errorMessage: {
				msg: "{{i18n PLUGINS_CMS_UIHELPER_SELECTVIEW_ERROR}}",
				category: Ext.getClassName(this)
			},
			waitMessage: {
				target: this._box
			}
		});
	},
	
	/**
	 * Callback function invoked after retrieving the content's views.
	 * @param {Object[]} views the views.
	 * @private
	 */
	_getContentViewsCb: function (views)
	{
		var combo = this._box.down('#viewName');
		combo.getStore().loadData(views);
		combo.focus();
	},
	
	/**
	 * Creates the dialog box
	 * @param {String} title The title of the dialog box.
	 * @param {String} icon The full path to icon (16x16 pixels) for the dialog box
	 * @param {String} helpMsg The message displayed at the top
	 * @private
	 */
	_createDialogBox: function (title, icon, helpMsg)
	{
		this._box = Ext.create('Ametys.window.DialogBox', {
			title: title,
			icon: icon,
			width: 400,
			
			items : [
       	          {
       	        	  xtype: 'component',
       	        	  cls: 'a-text',
       	        	  html: helpMsg
       	          },
       	          {
       	        	  xtype: 'combobox',
       	        	  itemId: 'viewName',
       	        	  name: 'viewName',
       	        	  fieldLabel: "{{i18n PLUGINS_CMS_UIHELPER_SELECTVIEW_VIEW_LABEL}}",
       	        	  allowBlank: false,
       	        	  store: {
       	        		  fields: [
       							'name',
       							{name: 'label', type: 'string'}
       					  ],
       					  autoLoad: false,
       					  sorters: {property: 'label'}
       	        	  },
       	        	  labelWidth: 100,
       	        	  labelAlign: 'right',
       	        	  labelSeparator: '',
       	        	  queryMode: 'local',
       	        	  displayField: 'label',
       	        	  valueField: 'name',
       	        	  typeAhead: true,
       	        	  editable: true,
       	        	  forceSelection: true,
       	        	  triggerAction: 'all'
       	          }
       	    ],
       	    
			closeAction: 'hide',
			
			referenceHolder: true,
			defaultButton: 'validate',
			
			buttons : [{
				reference: 'validate',
				text : "{{i18n PLUGINS_CMS_UIHELPER_SELECTVIEW_BUTTON_OK}}",
				handler: Ext.bind(this._validate, this)
		    }, 
		    {
		    	text : "{{i18n PLUGINS_CMS_UIHELPER_SELECTVIEW_BUTTON_CANCEL}}",
		    	handler: Ext.bind(function() {this._box.close();}, this) 
		    }]
		});
	},
	
	/**
	 * This function is called when validating the dialog box
	 * @private
	 */
	_validate: function ()
	{
		var combo = this._box.down('#viewName');
		if (!combo.isValid())
		{
			return;
		}
		
		if (Ext.isFunction (this._cbFn))
		{
			this._cbFn (this._contentId, combo.getValue());
		}
		
		this._box.hide();
	}
});