/*
 *  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 UI helper is like the runtime one for choosing group directories for two contexts.
 */
Ext.define('Ametys.plugins.web.groupdirectories.ChooseGroupDirectoriesHelper', {
    singleton: true,
    
    /**
     * Opens a dialog box helper for choosing one or more group directory(ies).
     * @param {Object} config The configuration object
     * @param {String} config.title The title of the dialog box
     * @param {String} config.hintText The text of the hint.
     * @param {Function} config.okAction The action to perform when clicking on the 'ok' button
     * @param {String[]} [config.selectedIds1] The ids of the group directories which have to be selected at opening in first field
     * @param {String[]} [config.selectedIds2] The ids of the group directories which have to be selected at opening in second field
     * @param {Boolean} [config.allowCreation=false] Set tot true to allow creation
     */
    open: function(config)
    {
        var commonCfg = {
            multiple: true,
            allowCreation: config.allowCreation,
            
            cls: 'ametys',
            style: "margin-top: 10px;",
            labelSeparator: '',
            labelAlign: 'top',
            labelWidth: 200,
            width: '100%',
            msgTarget: 'side'
        };
        
        var selectGroupDirectoriesWidget1 = Ext.create('Ametys.form.field.SelectGroupDirectories', Ext.apply({
            itemId: 'select-group-directories-widget1',
            fieldLabel: "<b>{{i18n PLUGINS_WEB_GROUP_DIRECTORIES_CHOOSE_GROUP_DIRECTORIES_HELPER_BO_DIRECTORIES_LABEL}}</b>",
            ametysDescription: "{{i18n PLUGINS_WEB_GROUP_DIRECTORIES_CHOOSE_GROUP_DIRECTORIES_HELPER_BO_DIRECTORIES_DESCRIPTION}}",
            value: config.selectedIds1 || []
        }, commonCfg));
        var selectGroupDirectoriesWidget2 = Ext.create('Ametys.form.field.SelectGroupDirectories', Ext.apply({
            itemId: 'select-group-directories-widget2',
            fieldLabel: "<b>{{i18n PLUGINS_WEB_GROUP_DIRECTORIES_CHOOSE_GROUP_DIRECTORIES_HELPER_FO_DIRECTORIES_LABEL}}</b>",
            ametysDescription: "{{i18n PLUGINS_WEB_GROUP_DIRECTORIES_CHOOSE_GROUP_DIRECTORIES_HELPER_FO_DIRECTORIES_DESCRIPTION}}",
            value: config.selectedIds2 || []
        }, commonCfg));
        
        this._box = Ext.create('Ametys.window.DialogBox', {
            title: config.title || '',
            iconCls: 'ametysicon-multiple25',
            
            layout: {
                type: "vbox",
                align: "stretch"
            },
            width: 500,
//            height: 420,
            scrollable: true,
            
            closeAction: 'destroy',
            items: [
                {
                    xtype: "component",
                    html: config.hintText || ''
                }, 
                selectGroupDirectoriesWidget1,
                selectGroupDirectoriesWidget2
            ],
            
            referenceHolder: true,
            defaultFocus: 'select-group-directories-widget1',
            defaultButton: 'buttonOk',
            buttons: [{
                itemId: 'button-ok',
                reference: 'buttonOk',
                text: "{{i18n plugin.core-ui:PLUGINS_CORE_UI_GROUP_DIRECTORIES_CHOOSE_GROUP_DIRECTORIES_HELPER_OK_BUTTON}}",
                handler: Ext.bind(this._validate, this, [config.okAction]),
                scope: this
            }, {
                itemId: 'button-cancel',
                text: "{{i18n plugin.core-ui:PLUGINS_CORE_UI_GROUP_DIRECTORIES_CHOOSE_GROUP_DIRECTORIES_HELPER_CANCEL_BUTTON}}",
                handler: function() {this._box.close();},
                scope: this
            }]
        });
        
        this._box.show();
    },
    
    /**
     * @private
     * Action to perform when the 'ok' button is clicked
     * @param {Function} callback The function to call
     */
    _validate: function(callback)
    {
        var ids1 = this._box.items.get('select-group-directories-widget1').getValue();
        var ids2 = this._box.items.get('select-group-directories-widget2').getValue();
        if (Ext.isFunction(callback))
        {
            callback(ids1, ids2);
        }
        
        this._box.close();
    }
});