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

/**
 * This class controls a ribbon button changing based on the visibility of the current selection
 * @private
 */
Ext.define('Ametys.plugins.web.content.controller.ContentPrivacyController', {
    extend: 'Ametys.plugins.cms.content.controller.SmartContentController',
    
    /**
     * Constructor for the controller
     * @param {Object} config The initial configuration
     */
    constructor: function(config)
    {
        this.callParent(arguments);
        
        Ametys.message.MessageBus.on(Ametys.message.Message.MODIFIED, this.refreshIfMatchingMessage, this);
    },

    _calculateStatus: function(targets)
    {
        var result = {};
        result["allright-contents"] = [];
        result["noright-contents"] = [];
        result["public-contents"] = [];
        result["private-contents"] = [];
        result["protected-contents"] = [];
        result["unmodifiable-contents"] = [];

        Ext.Array.each(targets, function(matchingTarget)
            {
                var parameters = matchingTarget.getParameters();
                if (parameters && parameters.content)
                {
                    var content = parameters.content;

                    if (content.getIsModifiable() && content.getIsWebContent())
                    {
                        var canChangePrivacy = content.getCanChangePrivacy();
                        if (!canChangePrivacy)
                        {
                            var i18nStr = this.getConfig("noright-content-description");
                            var description = Ext.String.format(i18nStr, content.getTitle());
                            var contentParam = this._getContentDefaultParameters(content);
                            contentParam["description"] = description;
                            result["noright-contents"].push(contentParam);
                        }
                        else
                        {
                            var privacy = content.getPrivacy();
                            var contentParam = null;
                            if (privacy == "public")
                            {
                                var i18nStr = this.getConfig("public-content-description");
                                var description = Ext.String.format(i18nStr, content.getTitle());
                                contentParam = this._getContentDefaultParameters(content);
                                contentParam["description"] = description;
                                result["public-contents"].push(contentParam);
                            }
                            else if (privacy == "private")
                            {
                                var i18nStr = this.getConfig("private-content-description");
                                var description = Ext.String.format(i18nStr, content.getTitle());
                                contentParam = this._getContentDefaultParameters(content);
                                contentParam["description"] = description;
                                result["private-contents"].push(contentParam);
                            }
                            else if (privacy == "protected")
                            {
                                var i18nStr = this.getConfig("protected-content-description");
                                var description = Ext.String.format(i18nStr, content.getTitle());
                                contentParam = this._getContentDefaultParameters(content);
                                contentParam["description"] = description;
                                result["protected-contents"].push(contentParam);
                            }

                            result["allright-contents"].push(contentParam);
                        }
                    }
                    else
                    {
                        var i18nStr = this.getConfig("nomodifiable-content-description");
                        var description = Ext.String.format(i18nStr, content.getTitle());
                        var contentParam = this._getContentDefaultParameters(content);
                        contentParam["description"] = description;
                        result["unmodifiable-contents"].push(contentParam);
                    }
                }

            }, this);
        return result;
    },

    /**
     * Callback function called after retrieving the status of content targets
     * @param {Object} params The JSON result 
     * @private
     */
    _getStatusCb: function (params, targets)
    {
    	this.callParent(arguments);
    	
        var nbContents = params["allright-contents"].length;

        var privacyLevel = "";

        if (nbContents != 0)
        {
            if (nbContents === params["public-contents"].length)
            {
                privacyLevel = "public";                
            }
            else if (nbContents === params["private-contents"].length)
            {
                privacyLevel = "private";
            }
            else if (nbContents === params["protected-contents"].length)
            {
                privacyLevel = "protected";
            }
            else
            {
                privacyLevel = "mixed";
            }
        }

        this._getGalleries().each(function (gallery) {
            gallery.items.each(function(item) {
                var elmt = Ametys.ribbon.RibbonManager.getUI(item.controlId);
                item.toggle(elmt.privacyLevel == privacyLevel);
            })
        });

        var prefix = privacyLevel != "" ? privacyLevel + "-" : "";

        var iconGlyph = this.getInitialConfig(prefix + "icon-glyph");
        var iconDecorator = this.getInitialConfig(prefix + "icon-decorator");
        this.setGlyphIcon(iconGlyph);
        this.setIconDecorator(iconDecorator);
    },
    
    /**
	 * @protected
	 * Update the tooltip description according state of the current selection
	 * @param description The initial description. Can be empty.
	 * @param params The JSON result received
	 */
	_updateTooltipDescription: function (description, params)
	{
        description = this._handlingMultiple(description, 'noread', params['noread-contents']);
		description = this._handlingMultiple(description, "public", params['public-contents']);
        description = this._handlingMultiple(description, "private", params['private-contents']);
        description = this._handlingMultiple(description, "protected", params['protected-contents']);
		description = this._handlingMultiple(description, "noright", params['noright-contents']);
		description = this._handlingMultiple(description, "nomodifiable", params['unmodifiable-contents']);

		this.setDescription (description);
	}
});