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


/**
 * Symbols editor singleton class
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.Symbols', {
	singleton: true,
	
	/**
	 * @property {Array[]} charmaps Multiple arrays of symbol-title pairs.
	 * Each pair represents a special characters.
	 */
	charmaps: [
		[ /* ponctuation */
			[' ',  'no-break space'],
			['&',   'ampersand'],
			['"',   'quotation mark'],
			['‹', 'single left-pointing angle quotation mark'],
			['›', 'single right-pointing angle quotation mark'],
			['«',  'left pointing guillemet'],
			['»',  'right pointing guillemet'],
			['‘', 'left single quotation mark'],
			['’', 'right single quotation mark'],
			['“', 'left double quotation mark'],
			['”', 'right double quotation mark'],
			['‚', 'single low-9 quotation mark'],
			['„', 'double low-9 quotation mark'],
			['–', 'en dash'],
			['—', 'em dash'],
			['¯',  'macron'],
			['‾', 'overline'],
			['¤',  'currency sign'],
			['¦',  'broken bar'],
			['¨',  'diaeresis'],
			['¡',  'inverted exclamation mark'],
			['¿',  'turned question mark'],
			['ˆ',  'circumflex accent'],
			['˜',  'small tilde'],
			['§',  'section sign'],
			['¶',  'paragraph sign']
		],
		[ /* finance */
			['¢',  'cent sign'],
			['€', 'euro sign'],
			['£',  'pound sign'],
			['¥',  'yen sign']
		],
		[ /* signs */
			['©',  'copyright sign'],
			['®',  'registered sign'],
			['™', 'trade mark sign'],
			['·',  'middle dot'],
			['•', 'bullet'],
			['…', 'three dot leader'],
			['′', 'minutes / feet'],
			['″', 'seconds / inches'],
			['ß',  'sharp s / ess-zed'],
			['♠', 'black spade suit'],
			['♣', 'black club suit'],
			['♥', 'black heart suit'],
			['♦', 'black diamond suit'],
			['´',  'acute accent'],
			['¸',  'cedilla'],
			['ª',  'feminine ordinal indicator'],
			['º',  'masculine ordinal indicator'],
			['†', 'dagger'],
			['‡', 'double dagger']
		],
		[ /* math / logical */
			['‰', 'per mille sign'],
			['µ',  'micro sign'],
			['<',   'less-than sign'],
			['>',   'greater-than sign'],
			['≤', 'less-than or equal to'],
			['≥', 'greater-than or equal to'],
			['°',  'degree sign'],
			['−', 'minus sign'],
			['±',  'plus-minus sign'],
			['÷',  'division sign'],
			['⁄', 'fraction slash'],
			['×',  'multiplication sign'],
			['¹',  'superscript one'],
			['²',  'superscript two'],
			['³',  'superscript three'],
			['¼',  'fraction one quarter'],
			['½',  'fraction one half'],
			['¾',  'fraction three quarters'],
			['ƒ',  'function / florin'],
			['∫', 'integral'],
			['∑', 'n-ary sumation'],
			['∞', 'infinity'],
			['√', 'square root'],
			['∼', 'similar to'],
			['≅', 'approximately equal to'],
			['≈', 'almost equal to'],
			['≠', 'not equal to'],
			['≡', 'identical to'],
			['∈', 'element of'],
			['∉', 'not an element of'],
			['∋', 'contains as member'],
			['∏', 'n-ary product'],
			['∧', 'logical and'],
			['∨', 'logical or'],
			['¬',  'not sign'],
			['∩', 'intersection'],
			['∪', 'union'],
			['∂', 'partial differential'],
			['∀', 'for all'],
			['∃', 'there exists'],
			['∅', 'diameter'],
			['∇', 'backward difference'],
			['∗', 'asterisk operator'],
			['∝', 'proportional to'],
			['∠', 'angle'],
			['ℵ', 'alef symbol'],
			['ϖ',  'pi symbol'],
			['ℜ', 'real part symbol'],
			['ϑ',  'theta symbol'],
			['ϒ',  'upsilon - hook symbol'],
			['℘', 'Weierstrass p'],
			['ℑ', 'imaginary part'],
			['⊂', 'subset of'],
			['⊃', 'superset of'],
			['⊄', 'not a subset of'],
			['⊆', 'subset of or equal to'],
			['⊇', 'superset of or equal to']
		],
		[ /* alphabetical special chars */
			['À',  'A - grave'],
			['Á',  'A - acute'],
			['Â',  'A - circumflex'],
			['Ã',  'A - tilde'],
			['Ä',  'A - diaeresis'],
			['Å',  'A - ring above'],
			['Æ',  'ligature AE'],
			['Ç',  'C - cedilla'],
			['È',  'E - grave'],
			['É',  'E - acute'],
			['Ê',  'E - circumflex'],
			['Ë',  'E - diaeresis'],
			['Ì',  'I - grave'],
			['Í',  'I - acute'],
			['Î',  'I - circumflex'],
			['Ï',  'I - diaeresis'],
			['Ð',  'ETH'],
			['Ñ',  'N - tilde'],
			['Ò',  'O - grave'],
			['Ó',  'O - acute'],
			['Ô',  'O - circumflex'],
			['Õ',  'O - tilde'],
			['Ö',  'O - diaeresis'],
			['Ø',  'O - slash'],
			['Œ',  'ligature OE'],
			['Š',  'S - caron'],
			['Ù',  'U - grave'],
			['Ú',  'U - acute'],
			['Û',  'U - circumflex'],
			['Ü',  'U - diaeresis'],
			['Ý',  'Y - acute'],
			['Ÿ',  'Y - diaeresis'],
			['Þ',  'THORN'],
			['à',  'a - grave'],
			['á',  'a - acute'],
			['â',  'a - circumflex'],
			['ã',  'a - tilde'],
			['ä',  'a - diaeresis'],
			['å',  'a - ring above'],
			['æ',  'ligature ae'],
			['ç',  'c - cedilla'],
			['è',  'e - grave'],
			['é',  'e - acute'],
			['ê',  'e - circumflex'],
			['ë',  'e - diaeresis'],
			['ì',  'i - grave'],
			['í',  'i - acute'],
			['î',  'i - circumflex'],
			['ï',  'i - diaeresis'],
			['ð',  'eth'],
			['ñ',  'n - tilde'],
			['ò',  'o - grave'],
			['ó',  'o - acute'],
			['ô',  'o - circumflex'],
			['õ',  'o - tilde'],
			['ö',  'o - diaeresis'],
			['ø',  'o slash'],
			['œ',  'ligature oe'],
			['š',  's - caron'],
			['ù',  'u - grave'],
			['ú',  'u - acute'],
			['û',  'u - circumflex'],
			['ü',  'u - diaeresis'],
			['ý',  'y - acute'],
			['þ',  'thorn'],
			['ÿ',  'y - diaeresis'],
		    ['Α',  'Alpha'],
			['Β',  'Beta'],
			['Γ',  'Gamma'],
			['Δ',  'Delta'],
			['Ε',  'Epsilon'],
			['Ζ',  'Zeta'],
			['Η',  'Eta'],
			['Θ',  'Theta'],
			['Ι',  'Iota'],
			['Κ',  'Kappa'],
			['Λ',  'Lambda'],
			['Μ',  'Mu'],
			['Ν',  'Nu'],
			['Ξ',  'Xi'],
			['Ο',  'Omicron'],
			['Π',  'Pi'],
			['Ρ',  'Rho'],
			['Σ',  'Sigma'],
			['Τ',  'Tau'],
			['Υ',  'Upsilon'],
			['Φ',  'Phi'],
			['Χ',  'Chi'],
			['Ψ',  'Psi'],
			['Ω',  'Omega'],
			['α',  'alpha'],
			['β',  'beta'],
			['γ',  'gamma'],
			['δ',  'delta'],
			['ε',  'epsilon'],
			['ζ',  'zeta'],
			['η',  'eta'],
			['θ',  'theta'],
			['ι',  'iota'],
			['κ',  'kappa'],
			['λ',  'lambda'],
			['μ',  'mu'],
			['ν',  'nu'],
			['ξ',  'xi'],
			['ο',  'omicron'],
			['π',  'pi'],
			['ρ',  'rho'],
			['ς',  'final sigma'],
			['σ',  'sigma'],
			['τ',  'tau'],
			['υ',  'upsilon'],
			['φ',  'phi'],
			['χ',  'chi'],
			['ψ',  'psi'],
			['ω',  'omega']
		],
		[ /* arrows */
			['←', 'leftwards arrow'],
			['↑', 'upwards arrow'],
			['→', 'rightwards arrow'],
			['↓', 'downwards arrow'],
			['↔', 'left right arrow'],
			['↵', 'carriage return'],
			['⇐', 'leftwards double arrow'],
			['⇑', 'upwards double arrow'],
			['⇒', 'rightwards double arrow'],
			['⇓', 'downwards double arrow'],
			['⇔', 'left right double arrow'],
			['∴', 'therefore'],
			['⊕', 'circled plus'],
			['⊗', 'circled times'],
			['⊥', 'perpendicular'],
			['⋅', 'dot operator'],
			['⌈', 'left ceiling'],
			['⌉', 'right ceiling'],
			['⌊', 'left floor'],
			['⌋', 'right floor'],
			['〈', 'left-pointing angle bracket'],
			['〉', 'right-pointing angle bracket'],
			['◊', 'lozenge']
		]
	]
});

/**
 * Symbols menu gallery item
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.Symbols.GalleryItem', {
	extend: 'Ametys.ribbon.element.RibbonUIController',
		
	/**
	 * Create an UI item to be placed inside a gallery.
	 * @return {Ext.Component} A ui that can takes place in a menu gallery
	 * @template
	 * @protected
	 */
	createGalleryItemUI: function()
	{
		var index = parseInt(this.getInitialConfig("symbol-category")) - 1;
		
		var charmap = Ametys.plugins.cms.editor.Symbols.charmaps[index];
		var len = charmap.length;
		
		var galleryItem = Ext.create('Ext.container.Container', {width: '100%'});
		for (i=0; i < len; i++) 
		{
			galleryItem.add(Ext.create('Ametys.plugins.cms.editor.Symbols.UI', {
				symbol: charmap[i][0],
				title: charmap[i][1]
			}));
		 }

		return galleryItem;
	}
});

/**
 * A single symbol UI for the symbols menu gallery
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.Symbols.UI', {
	extend: 'Ext.Component',
	
	constructor: function(config)
	{
		config.html = config.symbol;
		config.autoEl = {
			tag: 'a',
			cls: 'symbolMenu',
			title: config.title
		};
		
		delete config.symbol;
		delete config.title;
		
		this.callParent(arguments);
		
		this.on('click', this._insertChar, this, {element: 'el'});
	},
	
	/**
	 * Insert char in inline editor
	 * @param {Event} e The click event
	 */
	_insertChar: function(e)
	{
	    // FIXME "tinyMCE.activeEditor" a better method is to use the field.getEditor()
	    tinyMCE.activeEditor.focus();
		tinyMCE.activeEditor.execCommand('mceBeginUndoLevel');
		tinyMCE.activeEditor.execCommand('mceInsertContent', false, this.getInitialConfig('html'));
		tinyMCE.activeEditor.execCommand('mceEndUndoLevel');
	}
});