/*
 *  Copyright 2018 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 to create and edit a metadataSet
 */
Ext.define('Ametys.plugins.contenttypeseditor.editor.EditMetadataSetHelper',  {
    singleton: true,
    
    /**
     * @property {Function} _cbFn The callback function invoked when a metadataset is created/edited
     * @private
     */
    
    /**
     * @property {Ametys.window.DialogBox} _box The dialog box for creating/editing a metadataset.
     * @private
     */
    
    /**
     * @property {String} _mode The current edition mode ('new' or 'edit')
     * @private
     */
    
    /**
     * @property {Ext.form.Panel} _form The form panel
     * @private
     */
    
    /**
     * Configure and open the dialog box
     * @param {Object} config The configuration options :
     * @param {String} config.contentTypeId The id of content type
     * @param {String} config.title The title of dialog box
     * @param {String} config.iconCls The CSS class for the icon of the dialog box
     * @param {String} config.metadataSetInfo All information about the metadataSet to edit
     * @param {String} config.mode The mode. Can only be 'add' or 'edit.
     * @param {Function} config.callback The callback function invoked when a new content type is created. The callback function will received the following parameters:
     * @param {Object} config.callback.contentTypeInfos All information about a content type 
     */
    open: function(config)
    {
        this._mode = config.mode || 'add';
        this._contentTypeId = config.contentTypeId;
        this._metadataSetInfo = config.metadataSetInfo;
        this._cbFn = config.callback;
       
        this._createDialogBox(config.title, config.iconCls);
         
        if (this._mode == 'edit')
        {
            this._fillFields(config.metadataSetInfo);
        }
        
        this._box.show();
    },

    /**
     * @private
     * When in edition, fill the forms
     * @param {Object} metadataSetInfo The metadata set informations
     */
    _fillFields: function(metadataSetInfo)
    {
        this._form.getForm().findField("metadataset-name").setValue(metadataSetInfo.name);
        this._form.getForm().findField("metadataset-label").setValue(metadataSetInfo.label);
        this._form.getForm().findField("metadataset-description").setValue(metadataSetInfo.description);
        this._form.getForm().findField("metadataset-edition").setValue(metadataSetInfo.isEdition);
        this._form.getForm().findField("metadataset-icon-glyph").setValue(metadataSetInfo.iconGlyph);
    },
    
    /**
     * @private
     * Creates the dialog box
     * @param {String} title The dialog box title
     * @param {String} iconCls The dialog box icon (as a CSS classname)
     */
    _createDialogBox: function(title, iconCls)
    {
        this._form = this._createFormPanel();
        
        this._box = Ext.create('Ametys.window.DialogBox', {
            title : title || "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATASET_DIALOG_EDIT_TITLE}}",
            iconCls: iconCls || 'ametysicon-add64',
            width: 600,
            layout: 'fit',
            scrollable: true,
            referenceHolder: true,
            defaultFocus: 'metadataset-name',

            items: [this._form],
            
            buttons: 
            [
                {
                    text: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATASET_DIALOG_ACTIONS_FINISH}}",
                    handler: this._validate,
                    scope: this
                }, 
                {
                    text: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATASET_DIALOG_ACTIONS_CANCEL}}",
                    handler: this._cancel,
                    scope: this
                }
            ],
            
            closeAction: 'destroy'
        })
    },
    
    /**
     * @private
     * Create the panel with the fields
     * @return {Ext.form.Panel} The form panel
     */
    _createFormPanel: function()
    {
        var formPanel = Ext.create('Ext.form.Panel', {
            defaults: 
            {
                cls: 'ametys',
                labelSeparator: '',
                labelStyle: 'font-weight: bold',
                labelWidth: 150,
                width: '100%',
                msgTarget: 'side'
            },
            items: 
            [
                {
                    xtype: "textfield",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATASET_DIALOG_METADATASET_NAME_INPUT_LABEL}} *",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATASET_DIALOG_METADATASET_NAME_INPUT_DESCRIPTION}}",
                    allowBlank: false,
                    name: 'metadataset-name',
                    id: 'metadataset-name'
                },
                {
                    xtype: "enhancedmultilingualstring",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATASET_DIALOG_METADATASET_LABEL_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATASET_DIALOG_METADATASET_LABEL_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    name: 'metadataset-label',
                    id: 'metadataset-label'
                },
                {
                    xtype: "enhancedmultilingualstring",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATASET_DIALOG_METADATASET_DESCRIPTION_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATASET_DIALOG_METADATASET_DESCRIPTION_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    name: 'metadataset-description',
                    id: 'metadataset-description'
                },
                {
                    xtype: "checkbox",
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATASET_DIALOG_METADATASET_EDITION_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATASET_DIALOG_METADATASET_EDITION_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    name: "metadataset-edition",
                    id: "metadataset-edition"
                },
                {
                    xtype: 'edition.illustration',
                    defaultSources: [],
                    glyphSources: [Ametys.form.widget.Illustration.ApplicationGlyph.SOURCE],
                    fieldLabel: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATASET_DIALOG_METADATASET_GLYPH_INPUT_LABEL}}",
                    ametysDescription: "{{i18n PLUGINS_CONTENTTYPESEDITOR_EDIT_METADATASET_DIALOG_METADATASET_GLYPH_INPUT_DESCRIPTION}}",
                    allowBlank: true,
                    name: 'metadataset-icon-glyph',
                    id: 'metadataset-icon-glyph'
                }
            ]
        });
        
        return formPanel;
    },
    
    /**
     * @private
     * When clicking 'ok' in the dialog box
     */
    _validate: function()
    {
        var isValidForm = this._form.getForm().isValid();
        if (!isValidForm)
        {
            return;
        }
        if (Ext.isFunction(this._cbFn))
        {
            var values = this._getFormValues();
            this._cbFn(values);
        }
        this._box.close();
    },
    
    /**
     * @private
     * Get the form values
     * @return {Object} The content of the form
     */
    _getFormValues: function()
    {
        var values = {};
        values.dataType = "view";
        values.name = this._form.getForm().findField("metadataset-name").getValue();
        values.label = this._form.getForm().findField("metadataset-label").getValue();
        values.description = this._form.getForm().findField("metadataset-description").getValue();
        values.isEdition = this._form.getForm().findField("metadataset-edition").getValue();
        values.iconGlyph = this._form.getForm().findField("metadataset-icon-glyph").getValue().id;
        values.leaf = false;
        return values;
    },
    
    /**
     * @private
     * When clicking cancel in the dialog box
     */
    _cancel: function()
    {
        this._box.close();
    }
    
});