/*
 *  Fontfamily Picker
 *
 *  base source: https://gist.github.com/andreilupu/223ea5f58de554793d0cf41ba6e06811
 */
(function () {
    var fontFamilyPicker = (function () {
        'use strict';

        var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
        var pluginname = 'fontFamilyPicker';
        var regex = /has-font-([1-6]|none)/g;
        var debug = false;

        global.add(pluginname, function ( editor, url ) {

            /**
             * register format
             */
            editor.once('init', function () {
                editor.formatter.register(pluginname, {
                    inline: 'span',
                    classes: '%value',
                    remove: 'empty'
                });
            });

            // init fonts
            var tinymce_fonts = [];
            $.each(theme_wordings.tinymce.fonts, function(i, atts) {
                tinymce_fonts.push( {
                    text: atts.name,
                    value: i !== 0 ? 'has-font-'+i : ''
                } );
            });

            /**
             * add the button
             */
            editor.addButton(pluginname, {
                type: 'listbox',
                tooltip: theme_wordings.tinymce.fontfamily,
                text: theme_wordings.tinymce.fontfamily,
                icon: false,
                values: tinymce_fonts,
                onPostRender: function () {
                    // Select the first item by default
                    //this.value('');
                },
                onselect: function ( event ) {
                    removeCurrentFormatting(editor);

                    if ( this.value().length > 0 ) {
                        editor.execCommand('mceApply'+pluginname, this.value());
                    }
                    else {
                        editor.execCommand('mceRemove'+pluginname);
                    }
                }
            });

            /**
             * Create the toolbar with the controls for row
             * Also in this example the toolbar will apear only when the focused element is a div and the parent has the class greyd_fontsize-mceItem
             */
            // save toolbar for larger scope
            var toolbar;
            editor.on('wptoolbar', function ( args ) {
                if ( args.element.parentElement.nodeName === 'DIV' && args.element.parentElement.className.indexOf(pluginname+'-mceItem') !== -1 ) {
                    args.toolbar = toolbar;
                    args.selection = parent[0];
                }
            });

            /**
             * Assign buttons for our toolbar
             */
            editor.once('preinit', function () {
                if ( editor.wp && editor.wp._createToolbar ) {
                    toolbar = editor.wp._createToolbar([
                        pluginname
                    ]);
                }
            });

            /**
             * Register commands
             */
            editor.addCommand('mceApply'+pluginname, function (value) {
                applyFormat(editor, value);
            });
            editor.addCommand('mceRemove'+pluginname, function () {
                removeFormat(editor);
            });
        });
        
        /**
         *  Get & remove current formatting
         */
        var removeCurrentFormatting = function (editor) {
            if (debug) console.log('removeCurrentFormatting');
            var elm = editor.selection.getStart();
            var sizes = elm.className.matchAll(regex);
            if (sizes !== null) {
                for (const size of sizes) {
                    editor.dom.removeClass(elm, size[0]);
                }
            }
        };
        
        /**
         *  Apply format
         */
        var applyFormat = function (editor, value) {
            if (debug) console.log('applyFormat(format: '+pluginname+', value: '+value+')');
            editor.undoManager.transact(function () {
                editor.focus();
                editor.formatter.apply(pluginname, {
                    value: value
                });
                editor.nodeChanged();
            });
        };
        
        /**
         *  Remove format
         */
        var removeFormat = function (editor) {
            if (debug) console.log('removeFormat(format: '+pluginname+')');
            editor.undoManager.transact(function () {
                editor.focus();
                editor.formatter.remove(pluginname, {
                    value: null
                }, null, true);
                editor.nodeChanged();
            });
        };

        // return
        function Plugin () {
        }

        return Plugin;

    }());
})();