/**
 * plugin.js
 *
 * Copyright 2013 Web Power, www.webpower.nl
 * @author Arjan Haverkamp
 */

/* jshint unused:false */
/* global tinymce:true */

tinymce.PluginManager.requireLangPack('codemirror')

tinymce.PluginManager.add('codemirror', function(editor, url) {
    function showSourceEditor() {
        editor.focus()
        editor.selection.collapse(true)
        var options = editor.options.get('codemirror') ?? {};

        // Insert caret marker
        if (options.saveCursorPosition) {
            editor.selection.setContent('<span style="display: none;" class="CmCaReT">&#x0;</span>')
        }

        var codemirrorWidth = options.width ?? 800;
        var codemirrorHeight = options.height ?? 600;
        var fullscreen = options.fullscreen ?? false;

        var buttonsConfig = (tinymce.majorVersion < 5)
            ? [
                {
                    text: 'Ok',
                    subtype: 'primary',
                    onclick: function() {
                        var doc = document.querySelectorAll('.mce-container-body>iframe')[0]
                        doc.contentWindow.submit()
                        win.close()
                    }
                },
                {
                    text: 'Cancel',
                    onclick: 'close'
                }
            ]
            : [
                {
                    type: 'custom',
                    text: 'Ok',
                    name: 'codemirrorOk',
                    primary: true
                },
                {
                    type: 'cancel',
                    text: 'Cancel',
                    name: 'codemirrorCancel'
                }
            ]

        var config = {
            title: 'HTML source code',
            url: url + '/source.html',
            width: codemirrorWidth,
            height: codemirrorHeight,
            resizable: true,
            maximizable: true,
            fullScreen: fullscreen,
            saveCursorPosition: false,
            buttons: buttonsConfig
        }

        if (tinymce.majorVersion >= 5) {
            config.onAction = function (dialogApi, actionData) {
                if (actionData.name === 'codemirrorOk') {
                    var doc = document.querySelectorAll('.tox-dialog__body-iframe iframe')[0]
                    doc.contentWindow.submit()
                    win.close()
                }
            }
        }

        var win = (tinymce.majorVersion < 5)
            ? editor.windowManager.open(config)
            : editor.windowManager.openUrl(config)

        if (fullscreen) {
            win.fullscreen(true)
        }
    }

    if (tinymce.majorVersion < 5) {
        // Add a button to the button bar
        editor.addButton('code', {
            title: 'Source code',
            icon: 'code',
            onclick: showSourceEditor
        })

        // Add a menu item to the tools menu
        editor.addMenuItem('code', {
            icon: 'code',
            text: 'Source code',
            context: 'tools',
            onclick: showSourceEditor
        })
    } else {
        editor.ui.registry.addButton('code', {
            icon: 'sourcecode',
            title: 'Source code',
            tooltip: 'Source code',
            onAction: showSourceEditor
        })

        editor.ui.registry.addMenuItem('code', {
            icon: 'sourcecode',
            text: 'Source code',
            onAction: showSourceEditor,
            context: 'tools'
        })
    }
})
