/*
 *  Copyright 2025 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 class provides a widget to select a glyph.
 * 
 * This widget is registered for fields of type Ametys.form.WidgetManager#TYPE_STRING.<br>
 * 
 * This widget is configurable:<br>
 * - Use {@link #cfg-allowGlyphSources} to specify the authorized glyph sources (eg: 'application'). Separated by comma.<br>
 * See the subclasses of {@link Ametys.form.widget.Illustration.GlyphSource} for the available sources.<br>
 */
Ext.define('Ametys.form.widget.Glyph', {
    extend: 'Ametys.form.widget.Illustration',
    
	/**
     * @cfg {String} [buttonTooltip] The tooltip of button to select glyph. Default to "{{i18n PLUGINS_CORE_UI_WIDGET_ILLUSTRATION_SELECT_GLYPH_BUTTON}}".
     */
	/**
     * @cfg {String} [buttonIconCls="ametysicon-puzzle33"] The button's icon to select glyph 
     */
    /**
     * @cfg {String} [emptyText] The default text to place into an empty field. Default to "{{i18n PLUGINS_CORE_UI_WIDGET_GLYPH_NO_GLYPH}}".
     */
    /**
     * @cfg {String} [emptyCls="a-form-glyph-widget-empty"] The CSS class to apply to the empty field.
     */
    /**
     * @cfg {String} [deleteText] The text to display on delete button tooltip. Default to "{{i18n PLUGINS_CORE_UI_WIDGET_GLYPH_DELETE_GLYPH}}".
     */
    /**
     * @cfg {String} [deleteTextConfirm] The text to display when deleting a file. Default to "{{i18n PLUGINS_CORE_UI_WIDGET_GLYPH_DELETE_GLYPH_CONFIRM}}".
     */
    
    constructor: function (config)
    {
		config = Ext.applyIf(config, {
			deleteText: "{{i18n PLUGINS_CORE_UI_WIDGET_GLYPH_DELETE_GLYPH}}",
            deleteTextConfirm: "{{i18n PLUGINS_CORE_UI_WIDGET_GLYPH_DELETE_GLYPH_CONFIRM}}",
			emptyText: "{{i18n PLUGINS_CORE_UI_WIDGET_GLYPH_NO_GLYPH}}",
            emptyCls: 'a-form-glyph-widget-empty',
			buttonTooltip: "{{i18n PLUGINS_CORE_UI_WIDGET_ILLUSTRATION_SELECT_GLYPH_BUTTON}}",
			buttonIconCls: 'ametysicon-puzzle33'
		});
		
        config.allowSources = []; // No image source allowed for this widget
        
        this.callParent(arguments);
    },
    
   /**
    * @private
    * Callback function, called when a glyph is selected in the dialog.
    */
    _insertGlyphCb: function (glyph)
    {
   		if (glyph == null)
        {
			return;
        }
       
   		this.setValue(glyph);
    },
    
    setValue: function (value)
    {
		this.value = value;
		this.checkChange();

        Ext.suspendLayouts();
        try
        {
    	   this._updateUI();
        }
        finally
        {
            Ext.resumeLayouts(true);
        }
    },
    
	_updateUI: function()
    {
    	var value = this.value;
    	if (value)
    	{
    		if (!this.readOnly)
            {
                this.deleteButton.show();
                this.button.hide();
            }
            
            this._updateGlyphUI(value);
    	}
    	else
    	{
    		this.glyph.hide();
			
			if (!this.readOnly)
            {
                this.deleteButton.hide();
                this.button.show();
            }
            
            this.displayField.update(this.emptyText);
    	}
    }
});