/*
 *  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 controls a ribbon button representing the lock state of a page
 * @private
 */
Ext.define('Ametys.plugins.web.page.controller.LockController', {
	extend: 'Ametys.web.controller.WebButtonController',
	
	/**
	 * @cfg {String} locked-page-icon-glyph The CSS class for glyph when the selected page is locked.
	 */
	/**
	 * @property {String} _lockedIconGlyph See #cfg-locked-content-icon-glyph
	 * @private
	 */
	
	constructor: function(config)
	{
        config['toggle-enabled'] = true;

        this.callParent(arguments);
		
		this._unlockedIconGlyph = this.getInitialConfig("icon-glyph");
	
		this._lockedIconGlyph = this.getInitialConfig("locked-page-icon-glyph");
		
		Ametys.message.MessageBus.on(Ametys.message.Message.LOCK_CHANGED, this.refreshIfMatchingMessage, this);
	},
	
	updateState: function()
	{
		this.callParent(arguments);
		this._getLockState(this.getMatchingTargets()[0]);
	},
	
	/**
	 * Get the lock state of given targets
	 * @param pageTarget The page target
	 * @private
	 */
	_getLockState: function (pageTarget)
	{
		var parameters = pageTarget.getParameters();
        var page = parameters.page;
        var pageParams = {};
        
        if (page != null)
        {
            var i18nStr = page.getLocked() ? this.getConfig("locked-page-description") : this.getConfig("unlocked-page-description");
            var description = Ext.String.format(i18nStr, page.getTitle());
            
            pageParams["id"] = page.getId();
            pageParams["title"] = page.getTitle();
            pageParams["description"] = description;
            pageParams["lock-status"] = page.getLocked() ? "locked" : "unlocked";
        }
        
        // Update tooltip description and icons
		this._updateTooltipDescription("", pageParams);
		this._updateIcon (pageParams);
		
		// Toggle element if at least one content is locked
		this.toggle (this._currentLockStatus && this._currentLockStatus == "locked");
		
		this.enable();
	},


	/**
	 * @private
	 * Update the tooltip description according to lock state of the current selection
	 * @param {String} description The initial description
	 * @param {Object} params The JSON result object
	 */
	_updateTooltipDescription: function (description, params)
	{
		this._currentLockStatus = params["lock-status"];
		
		if (description != "")
		{
			description += "<br/><br/>";
		}
		description += params.description;
		
		this.setDescription (description);
	},
	
	/**
	 * @private
	 * Update the controller icons according to lock state of the current selection
	 * @param {Object} params The JSON result object
	 */
	_updateIcon: function (params)
	{
		if (this._currentLockStatus == "locked")
		{
			this.setGlyphIcon (this._lockedIconGlyph);
		}
		else
		{
			this.setGlyphIcon (this._unlockedIconGlyph);
		}
			this.setIconDecorator (null);
	}
});