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

/**
 * Singleton class to insert a table
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.Tables.InsertTableAction', {
	singleton: true,
	
	/**
	 * @property {Ametys.window.DialogBox} _box The insert table popup window.
	 * @private
	 */
	
	/**
	 * Insert a table by choosing the number of columns and rows
	 * @param {Ametys.cms.editor.EditorButtonController} controller The controller calling the function
	 */
	act: function(controller)
	{
		this._delayedInitialize();
		this._box.show();
	},
	
	/**
	 * Creates the dialog box if not already defined
	 * @returns {Ametys.window.DialogBox} The dialog box
	 */
	_delayedInitialize: function()
	{
		if (!this._box)
		{
			this._box = Ext.create('Ametys.window.DialogBox', {
				title : "{{i18n CONTENT_EDITION_TABLE_INSERT_TABLE_TITLE}}...",
				icon : Ametys.getPluginResourcesPrefix('cms') + '/img/content/edition/table/insert_16.png',
				
				layout: 'form',
				width: 310,
				height: 180,
				
				items: [{
					xtype: 'fieldset',
					title: "{{i18n CONTENT_EDITION_TABLE_INSERT_TABLE_DIALOG_SIZE}}",
					defaultType: 'numberfield',
					defaults: {
						cls: 'ametys',
						labelAlign: 'right',
						labelSeparator: '',
						labelWidth: 150,
						width: 220
					},
					items: [{
						fieldLabel : "{{i18n CONTENT_EDITION_TABLE_INSERT_TABLE_DIALOG_SIZE_COLS}}",
						name: 'cols',
						id: 'inlineedittabcols',
						allowDecimals: false,
						minValue: 1,
						value: 5,
						allowBlank: false
					}, {
						fieldLabel : "{{i18n CONTENT_EDITION_TABLE_INSERT_TABLE_DIALOG_SIZE_ROWS}}",
						name: 'rows',
						id: 'inlineedittabrows',
						allowDecimals: false,
						minValue: 1,
						value: 2,
						allowBlank: false
					}]
				}],
				
				closeAction: 'hide',
				defaultFocus: 'inlineedittabcols',
				
				referenceHolder: true,
				defaultButton: 'validate',
				
				buttons: [{
					reference: 'validate',
					text: "{{i18n CONTENT_EDITION_TABLE_INSERT_TABLE_DIALOG_OK}}",
					handler: Ext.bind(function() { Ext.defer(this._ok, 1, this); }, this)
				},{
					text: "{{i18n CONTENT_EDITION_TABLE_INSERT_TABLE_DIALOG_CANCEL}}",
					handler: Ext.bind(this._cancel, this)
				}]
			});
		}
		
	},
	
	/**
	 * Handler called when pressing 'Ok' button of the dialog box #_box
	 * @private
	 */
	_ok: function()
	{
		var cols = this._box.down('#inlineedittabcols');
		var rows = this._box.down('#inlineedittabrows');

		if (!cols.isValid() || !rows.isValid())
		{
			return;
		}
		
		this._box.hide();
		
		Ametys.plugins.cms.editor.Tables.createTable(cols.getValue(), rows.getValue());
	},
	
	/**
	 * Handler called when pressing 'Cancel' button of the dialog box #_box
	 * @private
	 */
	_cancel: function()
	{
		this._box.hide();
		tinyMCE.activeEditor.focus();
	}
});