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

/**
 * Dialog box used to create or edit a label
 * @private
 */
Ext.define('Ametys.plugins.thesaurus.helper.EditLabelDialogBox', {
	singleton: true,
	
	/**
	 * @property {Boolean} _initialized True if the dialog box was already initialized
	 * @private
	 */
	_initialized: false,
	
	/**
	 * @property {Ametys.window.DialogBox} _box The dialog box
	 * @private
	 */
	
	/**
	 * Open the dialog box
	 * @param {String} iconCls The CSS class for icon
	 * @param {String} title The title of the dialog box.
	 * @param {String} helpmessage The message displayed at the top of the dialog box. Can be null
	 * @param {Function} callback The callback function to called after validating dialog box
	 * @param {String} [value] The default value of title field
	 */
	open: function (iconCls, title, helpmessage, callback, value)
	{
		this._cbFn = callback || Ext.emptyFn;
		this._delayedInitialize(iconCls, title, helpmessage);
		
		
		var form = this._form.getForm();
		form.findField('title').setValue(value || '');
		form.findField('title').clearInvalid();
		
		this._box.show();
	},
	
	/**
	 * Creates the dialog box if it is not already created
	 * @param {String} iconCls The CSS class for icon
	 * @param {String} title The title of the dialog box.
	 * @param {String} helpmessage The message displayed at the top of the dialog box. Can be null
	 * @private
	 */
	_delayedInitialize: function (iconCls, title, helpmessage)
	{
		if (!this._initialized)
		{
			this._form = new Ext.FormPanel({
				border :false,
				defaults: {
					cls: 'ametys',
					labelWidth: 100,
					labelAlign: 'right',
					labelSeparator: '',
					msgTarget: 'side'
				},
				
				items:[{
				        	xtype: 'textfield',
				        	fieldLabel: "{{i18n PLUGINS_THESAURUS_NEW_THESAURUS_DIALOG_TITLE}}",
				        	name: 'title',
				        	itemId: 'title',
							width: 380,
							allowBlank: false		        
				        }
				 ]
			});
		    	
		    this._box = Ext.create('Ametys.window.DialogBox', {
		    	
		    	title: title,
		    	iconCls: iconCls,
		    	
				width: 430,
					
				items : [{
					xtype: 'component',
					cls: 'a-text',
					html: helpmessage || ''
				}, this._form ],
							
				defaultFocus: 'title',
				selectDefaultFocus: true,
				
				referenceHolder: true,
				defaultButton: 'validate',

				closeAction: 'hide',
				
				buttons : [{
						reference: 'validate',
						text :"{{i18n PLUGINS_THESAURUS_DIALOG_OK}}",
						handler : Ext.bind(this._ok, this)
					}, {
						text :"{{i18n PLUGINS_THESAURUS_DIALOG_CANCEL}}",
						handler: Ext.bind(function() {this._box.close();}, this)
					} 
				]
			});
		    
		    this._initialized = true;
		}
		else
		{
			this._box.setTitle (title);
			this._box.setIconCls(iconCls);
			this._box.items.getAt(0).update(helpmessage || '');
		}
	},
	
	/**
	 * Function called when clicking on 'Ok' button.
	 * Calls the callback function passed in {@link #method-open} and hide the dialog box.
	 * @private
	 */
	_ok: function()
	{
		var form = this._form.getForm();
		if (!form.isValid())
		{
			return;
		}
		
		var title = form.findField('title').getValue();
		
		if (Ext.isFunction (this._cbFn))
		{
			this._cbFn (title);
		}
		
		this._box.hide();
	}
	
});