/*
 *  Copyright 2016 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 is a singleton to handle actions on group directories in a web context.
 */
Ext.define('Ametys.plugins.web.groupdirectories.GroupDirectoryActions', {
    singleton: true, 
    
    /**
     * @property {String} _contextBO The context for BO populations
     * @private
     */
    /**
     * @property {String} _contextFO The context for FO populations
     * @private
     */
    /**
     * @property {String} _hintText The hint text in the dialog box
     * @private
     */
    /**
     * @property {String[]} _directoriesBO The ids of the directories
     * @private
     */

    /**
     * @private
     * @property {String} _currentSiteName The current site name
     */
    /**
     * @private
     * @property {String} _currentSiteId The current site id
     */

    /**
     * Opens a dialog box to select the group directories to link to the selected site context
     * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
     */
    linkToSite: function(controller)
    {
        var targets = controller.getMatchingTargets();
        
        if (targets.length > 0)
        {
            var siteId = targets[0].getParameters().id;
            var siteName = targets[0].getParameters().name;
            this._contextBO = controller.getInitialConfig('contextBO') + '/' + siteName;
            this._contextFO = controller.getInitialConfig('contextFO') + '/' + siteName;
            this._currentSiteId = siteId;
            this._currentSiteName = siteName;
            
            var siteTitle = targets[0].getParameters().title || siteName;
            
            this._callback = Ext.bind(this._sendModifiedSiteMsg, this, [siteId, siteName], 1);
            this._hintText = Ext.String.format("{{i18n PLUGINS_WEB_USER_POPULATIONS_CHOOSE_GROUP_DIRECTORIES_HINT}}", siteTitle);
            
            Ametys.plugins.core.groupdirectories.GroupDirectoryDAO.getGroupDirectoriesOnContext([this._contextBO], this._linkToContexts, {scope: this});
        }
    },
    
        /**
     * @private
     * Sends a modified message bus on the site if needed.
     * @param {Object} response The server response
     * @param {String} siteId The id of the site
     * @param {String} siteName The name of the site
     */
    _sendModifiedSiteMsg: function(response, siteId, siteName)
    {
        if (response != null && response.length > 0)
        {
            Ext.create('Ametys.message.Message', {
                type: Ametys.message.Message.MODIFIED,
                targets: {
                    id: Ametys.message.MessageTarget.SITE,
                    parameters: {
                        id: siteId,
                        name: siteName
                    }
                }
            });
        }
    },
    
    /**
     * @private
     * Retrieve the already linked group directories on the second context(FO) after retrieving the ones on the first context (BO)
     * @param {String[]} directoriesBO The group directories linked to the first context (BO)
     */
    _linkToContexts: function(directoriesBO)
    {
        this._directoriesBO = directoriesBO;
        Ametys.plugins.core.groupdirectories.GroupDirectoryDAO.getGroupDirectoriesOnContext([this._contextFO], this._openChooseDialog, {scope: this});
    },
    
    /**
     * @private
     * Function to open the choose dialog box
     * @param {String[]} directoriesFO The ids of the already linked group directories of FO
     */
    _openChooseDialog: function(directoriesFO)
    {
                
        Ametys.plugins.web.groupdirectories.ChooseGroupDirectoriesHelper.open({
            title: "{{i18n plugin.core-ui:PLUGINS_CORE_UI_GROUP_DIRECTORIES_CHOOSE_GROUP_DIRECTORIES_TITLE}}",
            hintText: this._hintText,
            selectedIds1: this._directoriesBO,
            selectedIds2: directoriesFO,
            allowCreation: true,
            okAction: Ext.bind(this._doLink, this)
        });
    },
    
    /**
     * @private
     * Links some group directories to 2 context
     * @param {String[]} idsBO The ids of the selected group directories for BO
     * @param {String[]} idsFO The ids of the selected group directories for FO
     */
    _doLink: function(idsBO, idsFO)
    {
        Ametys.plugins.core.groupdirectories.GroupDirectoryDAO.link([this._contextBO, idsBO], this._callback || Ext.emptyFn, {scope: this});
        Ametys.plugins.core.groupdirectories.GroupDirectoryDAO.link([this._contextFO, idsFO], this._callback || Ext.emptyFn, {scope: this});
        
        Ametys.notify({
            type: 'info',
            iconGlyph: 'ametysicon-multiple25',
            title: "{{i18n PLUGINS_WEB_GROUP_DIRECTORIES_CHOOSE_DIRECTORY_HELPER_DONE_TITLE}}",
            description: Ext.String.format("{{i18n PLUGINS_WEB_GROUP_DIRECTORIES_CHOOSE_DIRECTORY_HELPER_DONE_DESC}}", this._currentSiteName),
            action: Ext.bind(Ametys.tool.ToolsManager.openTool, Ametys.tool.ToolsManager, ['uitool-admin-sites', {id: this._currentSiteId}], false)
        });

    }
});