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

/**
 * Items for the Theme colors menu
 */
Ext.define('Ametys.plugins.skinfactory.ribbon.menu.ThemeColorsMenuItem', {
	extend: 'Ametys.ribbon.element.ui.ButtonController',
	
	/**
	 * @property {RegExp} hexColorRe The regexp for 6-digit color hex code without the # symbol
	 */
	hexColorRe: /^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/,
	
	/**
	 * @property {Ext.XTemplate} renderItemTpl The template for menu item
	 */
	renderItemTpl: new Ext.XTemplate(
                '<div style="float: left; margin-right: 2px; width: 75px;">',
	                '<tpl for="colors">',
		                '<div style="float: left; border: 1px solid #888; margin: 2px">',
		                    '<span style="display: block; height: 12px; width: 12px; line-height: 10px; background-color:{.}"> </span>',
		                '</div>',
		            '</tpl>',
                '</div>',
                '<span class="x-menu-item-text">{label}</span>'
    ),
            
	createMenuItemUI: function ()
	{
		if (this.getLogger().isDebugEnabled())
		{
			this.getLogger().debug("Creating new UI menu item for controller " + this.getId());
		}
		
		var colors = [];
		for (var i=0; i < this.getInitialConfig("color-size") && i < 4; i++)
		{
			var color = this.getInitialConfig("color-" + i)
			if (this.hexColorRe.test(color))
			{
				color = '#' + color;
			}
			colors.push(color);
		}
		
		var text = this.renderItemTpl.apply({
			label: this.getInitialConfig("label"),
			colors: colors
		})
		
		var element = Ext.create("Ext.menu.CheckItem", Ext.apply({
			text: text,
			
			showCheckbox: false,
			checkHandler: Ext.bind(this.onPress, this),
			checked: false,
			
			controlId: this.getId(),
		}, this.getInitialConfig('ui-config') || {}));
		
		return element;
	}
});