/*
 *  Copyright 2020 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 controller opens a tool to show the difference between two versions of the same content.
  */
Ext.define('Ametys.plugins.cms.content.compare.CompareContentController', {
    extend: 'Ametys.ribbon.element.ui.ButtonController',
    
    /**
     * @private
     * @property {String} _contentId The current content id
     */
    _contentId: null,
    /**
     * @private
     * @property {String} _contentTitle The current content title
     */
    _contentTitle: null,
    
    /**
     * @private
     * @property {String} _version The content version to compare
     */
    _version: null,
    /**
     * @private
     * @property {String} _baseVersion The content base version
     */
    _baseVersion: null,
    
    constructor: function(config)
    {
        config.action =  config.action || "Ametys.plugins.cms.content.compare.CompareContentController._act";
        this.callParent(arguments);
        this._toggleEnabled = false;
        
        Ametys.message.MessageBus.on(Ametys.message.Message.MODIFIED, this.refreshIfMatchingMessage, this);
        Ametys.message.MessageBus.on(Ametys.message.Message.WORKFLOW_CHANGED, this.refreshIfMatchingMessage, this);
    },
    
    updateState: function()
    {
        this.disable();
        
        var targets = this.getMatchingTargets();
        
        if (targets.length == 1)
        {
            var target = targets[0];
            this._contentId = target.getParameters().id;
            this._contentTitle = target.getParameters().title;
            this.serverCall("isComparable", [this._contentId, target.getParameters().version], this._isComparableCb, {refreshing: true});
        }
        else if (targets.length == 0)
        {
            this.setAdditionalDescription(this.getInitialConfig("selection-description-nomatch"));
        }
        else
        {
            this.setAdditionalDescription(this.getInitialConfig("selection-description-multiselectionforbidden"));
        }
    },
    
    areSameTargets: function (target1, target2)
    {
        if (target1.getParameters().id && target2.getParameters().id)
        {
            return target1.getParameters().id == target2.getParameters().id
                && target1.getParameters().version == target2.getParameters().version;
        }
        return false;
    },
    
    /**
     * @private
     * Callback function after calling server for comparison information
     * @param {Object} response The JSON response
     */
    _isComparableCb: function(response)
    {
        if (response.isComparable)
        {
            this.enable();
            this.setAdditionalDescription("");
            this._version = response.version;
            this._baseVersion = response.baseVersion;
        }
        else if (response.noBaseVersion)
        {
            this.disable();
            this.setAdditionalDescription(this.getInitialConfig("selection-description-nobaseversion"));
        }
        else if (response.isSameVersion)
        {
            this.disable();
            this.setAdditionalDescription(this.getInitialConfig("selection-description-issameversion"));
        }
        else
        {
            this.disable();
            this.setAdditionalDescription(this.getInitialConfig("selection-description-nomatch"));
        }
    },
    
    statics: {
        /**
         * @protected
         * This action do open the CompareContent tool
         * @param {Ametys.plugins.web.content.compare.CompareContentController} controller This controller.
         */
        _act: function(controller)
        {
            var contentId = controller._contentId;
            var version = controller._version;
            var baseVersion = controller._baseVersion;
            
            var parameters = {
                id: contentId,
                contentId: contentId,
                baseVersion: baseVersion,
                version: version
            };
            
            var toolId = controller.getInitialConfig("opentool-id");
            Ametys.tool.ToolsManager.openTool(toolId, parameters);
        }
    }
});