/*
 *  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.
 */

/**
 * Convertor for HTMLExpert
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.htmlexpert.HTMLExpertConvertor', {
    statics: {
        /**
         * Create the html for the htmlexpert marker in the editor
         * @param {String} id The identifier
         * @param {String} code The html code
         * @param {String} [pellet] The pellet to display if specified
         * @param {Boolean} [ribbonselect=false] Should enforce the ribbon selection
         */
        createHTML: function(id, code, pellet, ribbonselect)
        {
            var classes = ['htmlexpert', 'mceItemNoResize'];
            if (pellet)
            {
                classes.push(pellet);
            }
            
            var idAttr = id ? 'id="' + id + '" ' : '';
            var ribbonAttr = ribbonselect ? '_mce_ribbon_select="1" ' : '';
            var pelletAttr = pellet ? 'pellet="' + pellet + '" ' : '';
            var classAttr = 'class="' + classes.join(' ') + '" ';
            var srcAttr = 'src="' + Ametys.getPluginResourcesPrefix('cms') + '/img/content/edition/htmlexpert/trans.gif"';
            
            return '<img ' + idAttr + 'htmlexpert="-' + code + '" marker="marker" ' + ribbonAttr + pelletAttr + classAttr + srcAttr + '/>';
        }
    },

    
    /**
     * @property {RegExp} _regexpGetContent The RegExp to use in getcontent method
     * @private
     */
    _regexpGetContent: /<img[^>]* htmlexpert="-([^"]*)"[^>]*>/g,
    /**
     * @property {RegExp} _regexpSetContent The RegExp to use in setcontent method
     * @private
     */
    _regexpSetContent: /<!-- htmlexpert(:([^ ]+))? ([^-]*) -->/g,

    /**
     * Registered to intercept getContent on richtext
     * @param {Ametys.form.field.RichText} field The richtext to convert
     * @param {tinymce.Editor} editor The current richtext editor
     * @param {Object} object The object of value to be modified
     */
    onGetContent: function(field, editor, object)
    {
        var me = this;
        
        if (object.content)
        {
            object.content = object.content.replace(
                me._regexpGetContent, 
                function(s, m1) {
                    var htmlexpert = m1;
                    var pellet = / pellet="([^"]+)"/.test(s) ? RegExp.$1 : null;
                    return "<!-- htmlexpert" + (pellet != null ? ':' + pellet : '') + ' ' + htmlexpert.replace(/-/g, "%2D") + " -->";
                }
            );
        }        
    },
    
    /**
     * Registered to intercept setContent on richtext
     * @param {Ametys.form.field.RichText} field The richtext to convert
     * @param {tinymce.Editor} editor The current richtext editor
     * @param {Object} object The object of value to be modified
     */
    onSetContent: function(field, editor, object)
    {
        var me = this;

        if (object.content)
        {
            object.content = object.content.replace(
                me._regexpSetContent, 
                function(s, m1, m2, m3) {
                    me._regexpSetContent.test(s);
                    var pellet = m2;
                    var htmlexpert = m3;
                    return Ametys.plugins.cms.editor.htmlexpert.HTMLExpertConvertor.createHTML(null, htmlexpert.replace(/%2D/g, "-"), pellet, false);
                }
            );
        }        
    }
});