/*
 *  Copyright 2017 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 provides a dialog box for choosing one or more content types in content types tree.
*/
Ext.define('Ametys.cms.form.widget.ChooseContentTypes', {
	singleton: true,
    
    /**
     * @property {Ametys.window.DialogBox} _box The dialog box
     * @private
     */
    
    /**
     * @private
     * @property {Function} _cbFn The callback function invoked when content types are selected
     */
    
    /**
     * @private 
     * @property {Boolean} _excludePrivate True to exclude private content types.
     */
    
    /**
     * @private 
     * @property {Boolean} _excludeReferenceTable True to exclude reference table content types.
     */
    
    /**
     * @private 
     * @property {Boolean} _excludeAbstract True to exclude abstract content types.
     */
    
    /**
     * @private 
     * @property {Boolean} _excludeMixin True to exclude mixin content types.
     */
    
    /**
     * @private 
     * @property {Boolean} _includeMixinOnly True to only include mixin content types.
     */
    
    /**
     * @private 
     * @property {Boolean} _includeSupertype True to recursively include supertypes.
     */
    
    /**
     * @private 
     * @property {Ametys.plugins.cms.contenttype.ContentTypeTree} _tree The content type tree
     */
	
	/**
     * Configure and open the dialog box
     * @param {Object} config The configuration options :
     * @param {String} [config.title] The title of the dialog box.
     * @param {String} [config.iconCls] The CSS class for icon of the dialog box
     * @param {String} [config.helpMessage] The help message to display on top of dialog box.
     * @param {Boolean} [config.excludePrivate=false] True to exclude private content types. By default excludePrivate is false.
     * @param {Boolean} [config.excludeReferenceTable=true] True to exclude reference table content types. By default excludeReferenceTable is true.
     * @param {Boolean} [config.excludeAbstract=false] True to exclude abstract content types. By default excludeAbstract is false.
     * @param {Boolean} [config.excludeMixin=false] True to exclude mixin content types. By default excludeMixin is false.
     * @param {Boolean} [config.includeMixinOnly=false] True to only include mixin content types. By default includeMixinOnly content type is false.
     * @param {Boolean} [config.includeSupertype=false] True to recursively include supertypes. By default includeSupertype is false.
     * @param {Boolean} [config.multiple=false] True to allow multiple selection in tree. By default multiple selection is false.
     * @param {Function} config.callback The callback function invoked when content types are selected. The callback function will received the following parameters:
     * @param {String[]/String} config.callback.ids The identifiers of the selected content types. In simple mode, the value is a string, in multiple mode it is an array
     * @param {String[]/String} config.callback.title The label of the selected content types. In simple mode, the label of content types is a string, in multiple mode it is an array
     */
	open: function (config)
	{
        this._cbFn = config.callback;
        this._excludePrivate = config.excludePrivate;
        this._excludeReferenceTable = config.excludeReferenceTable;
        this._excludeAbstract = config.excludeAbstract;
        this._excludeMixin = config.excludeMixin;
        this._includeMixinOnly = config.includeMixinOnly;
        this._includeSupertype = config.includeSupertypeOnly;
        this._contentTypes = config.contentTypes;
        this._strictContentTypes = config.strictContentTypes;
        this._multiple = config.multiple;
        this._values = config.values;
        this._createDialogBox(config.title, config.iconCls, config.helpMessage);
        this._tree.getStore().load();
        this._box.show();
	},
	
	/**
     * @private
     * Creates the dialog box
     * @param {String} [title] The title of the dialog box.
     * @param {String} [iconCls] The CSS class for the icon of the dialog box.
     * @param {String} [helpMessage] The help message to display on top of dialog box.
     */
	_createDialogBox: function (title, iconCls, helpMessage)
	{
        this._tree = this._createContentTypeTree();
        
        this._box = Ext.create('Ametys.window.DialogBox', {
            title: title || "",
            width: 450,
            height: 500,
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            scrollable: false,
            iconCls: iconCls || 'ametysicon-squares36',
            
            items: 
            [
                {
                    xtype: "component",
                    cls: "a-text",
                    html: helpMessage || "{{i18n PLUGIN_CMS_WIDGET_SELECTCONTENTTYPES_DIALOG_HINT}}"
                },
                this._tree
            ],
            
            closeAction: 'destroy',
            buttons: 
            [
                {
	                text: "{{i18n PLUGIN_CMS_WIDGET_SELECTCONTENTTYPES_DIALOG_OK_BUTTON}}",
	                handler: this._validate,
	                scope: this
                },
                {
                    text: "{{i18n PLUGIN_CMS_WIDGET_SELECTCONTENTTYPES_DIALOG_CANCEL_BUTTON}}",
                    handler: Ext.bind(function() {this._box.close();}, this)
                }
            ]
        });
	},
    
    /**
     * @private
     * Create a content type tree
     * @return {Ametys.plugins.cms.contenttype.ContentTypeTree} the content type tree
     */
    _createContentTypeTree: function()
    {
        var tree = Ext.create('Ametys.plugins.cms.contenttype.ContentTypeTree', {
            flex: 1,
            hierarchicalView: true,
            excludeMode: 'disabled',
            excludeAbstract: this._excludeAbstract,
            excludeMixin: this._excludeMixin,
            excludeReferenceTable: this._excludeReferenceTable,
            excludePrivate: this._excludePrivate,
            includeMixinOnly: this._includeMixinOnly,
            strictContentTypes: this._strictContentTypes,
            contentTypes: this._contentTypes,
            checkMode: this._multiple,
            values: this._values
        });
        return tree;
    },
    
    /**
     * @private
     * Function called when OK button is pressed
     */
    _validate: function()
    {
        if (Ext.isFunction(this._cbFn))
        {
            var selectedNodes = [];
            if (this._multiple)
            {
                selectedNodes = this._tree.values;
            }
            else
            {
                var selectedNode = this._tree.getSelectionModel().getSelection()[0].get('contentTypeId');
                selectedNodes.push(selectedNode);
            }
            this._cbFn(selectedNodes);
        }
        this._box.close();  
    }
	
});