/* Hilfsfunktionen */
jQuery(document).ready(function($) {  // gleich wie jQuery(function($)

//------------------------------------------------------------------
// button functions ------------------------------------------------
//------------------------------------------------------------------
    // BOLD
    $('#bold-button').click(function() {
        var textarea = $('#event-details');
        wrapSelectedText(textarea, '<strong>', '</strong>');
    });

    // ITALIC
    $('#italic-button').click(function() {
        var textarea = $('#event-details');
        wrapSelectedText(textarea, '<em>', '</em>');
    });

    // UNDERLINE
    $('#underline-button').click(function() {
        var textarea = $('#event-details');
        wrapSelectedText(textarea, '<u>', '</u>');
    });

    // STRIKETHROUGH
    $('#strikethrough-button').click(function() {
        var textarea = $('#event-details');
        wrapSelectedText(textarea, '<s>', '</s>');
    });

    // HEADING (using h4 as an example)
    $('#heading-button').click(function() {
        var textarea = $('#event-details');
        wrapSelectedText(textarea, '<h2>', '</h2>');
    });

    // QUOTE
    $('#quote-button').click(function() {
        var textarea = $('#event-details');
        wrapSelectedText(textarea, '<blockquote>', '</blockquote>');
    });

    // CODE
    $('#code-button').click(function() {
        var textarea = $('#event-details');
        wrapSelectedText(textarea, '<code>', '</code>');
    });

    // LIST: Convert selected lines into a list
    $('#list-button').click(function() {
        var textarea = $('#event-details');
        var startPos = textarea.prop('selectionStart');
        var endPos = textarea.prop('selectionEnd');
        var textValue = textarea.val();
        var selectedText = textValue.substring(startPos, endPos);
        var lines = selectedText.split('\n');
        var newText = '<ul>';
        for (var i = 0; i < lines.length; i++) {
            newText += '<li>' + lines[i] + '</li>';
        }
        newText += '</ul>';
        textarea.val(textValue.substring(0, startPos) + newText + textValue.substring(endPos));
        // Place cursor after the list
        textarea.prop('selectionStart', startPos + newText.length);
        textarea.prop('selectionEnd', startPos + newText.length);
    });

    // LINK: Wrap selected text into a link if text is selected.
    // If nothing is selected, you might consider inserting a placeholder link.
    $('#link-button').click(function() {
        var textarea = $('#event-details');
        var selectedText = textarea.val().substring(textarea.prop('selectionStart'), textarea.prop('selectionEnd')) || 'Link Text';
        var url = prompt('Enter the URL:', 'https://');
        if (url) {
            var newText = '<a href="' + url + '" target="_blank">' + selectedText + '</a>';
            // Replace selected text or insert at cursor if nothing is selected.
            var startPos = textarea.prop('selectionStart');
            var endPos = textarea.prop('selectionEnd');
            var textValue = textarea.val();
            textarea.val(textValue.substring(0, startPos) + newText + textValue.substring(endPos));
            textarea.prop('selectionStart', startPos + newText.length);
            textarea.prop('selectionEnd', startPos + newText.length);
            textarea.trigger('input');
        }
    });

    // IMAGE: Prompt for image URL and insert an image tag.
    $('#image-button').click(function() {
        var textarea = $('#event-details');
        var imageUrl = prompt('Enter the image URL:', 'https://');
        if (imageUrl) {
            // Optional: if some text is selected, you could choose to append the IMG after it.
            var selectedText = textarea.val().substring(textarea.prop('selectionStart'), textarea.prop('selectionEnd'));
            var imageTag = '<img src="' + imageUrl + '" class="wp-block-image">';
            if (selectedText) {
                // Append imageTag after the selected text.
                wrapSelectedText(textarea, '', imageTag);
            } else {
                // Insert imageTag at the cursor.
                insertAtCursor(textarea, imageTag);
            }
            textarea.trigger('input');
        }
    });

    // HR: Insert horizontal rule at the cursor position.
    $('#hr-button').click(function() {
        var textarea = $('#event-details');
        // We don't expect any selected text for HR so we simply insert it.
        insertAtCursor(textarea, '<hr>');
    });

//------------------------------------------------------------------
//------------------------------------------------------------------
});   // END JQUERY READY
//------------------------------------------------------------------
// Helper: Wraps selected text with openTag and closeTag.
// If no text is selected, the tags will be inserted with the cursor placed between them.
function wrapSelectedText(textarea, openTag, closeTag) {
    var startPos = textarea.prop('selectionStart');
    var endPos = textarea.prop('selectionEnd');
    var textValue = textarea.val();
    var selectedText = textValue.substring(startPos, endPos);

    // Build new content and update textarea.
    var newText = openTag + selectedText + closeTag;
    textarea.val(textValue.substring(0, startPos) + newText + textValue.substring(endPos));

    // If nothing was selected, place cursor between the tags.
    if (startPos === endPos) {
        textarea.prop('selectionStart', startPos + openTag.length);
        textarea.prop('selectionEnd', startPos + openTag.length);
    } else {
        // Otherwise, keep the originally selected text highlighted.
        textarea.prop('selectionStart', startPos + openTag.length);
        textarea.prop('selectionEnd', startPos + openTag.length + selectedText.length);
    }
   textarea.trigger('input');
}

// Helper: Inserts text at the cursor location, replacing any selected text.
function insertAtCursor(textarea, textToInsert) {
    var startPos = textarea.prop('selectionStart');
    var endPos = textarea.prop('selectionEnd');
    var textValue = textarea.val();

    textarea.val(textValue.substring(0, startPos) + textToInsert + textValue.substring(endPos));

    // Set the cursor right after the inserted text.
    textarea.prop('selectionStart', startPos + textToInsert.length);
    textarea.prop('selectionEnd', startPos + textToInsert.length);
    textarea.trigger('input');
}
