let content = document.getElementById('content');

let heading = document.getElementById('heading');
let description = document.getElementById('description');

let input = document.getElementById('input');
let close = document.getElementById('close');

function setInputFieldType(value) {
    if (input.type == value) return;

    var field = document.createElement('input');
    field.type = value;
    field.id = input.id;
    field.name = input.name;
    input.parentNode.replaceChild(field, input);

    input = field;
    input.focus();

    return field;
}


function setText(heading, description) {
    setHeading(heading);
    setDescription(description);
}

function setHeading(value) {
    heading.innerText = value;
}

function setDescription(value) {
    if (value.length <= 5) {
        description.remove();
        return false;
    }

    description.innerText = value;
}

function removeDescription() {
    document.removeChild(description);
}

function getInput() {
    return input.value.length != 0 ? input.value : null;
}

close.onclick = function closeWindow() {
    mp.trigger('closeinputhelper');
    console.log('Fenster geschlossen!');
}

setInputFieldType('text');
setText("Überschrift", "Lorem ipsum dolor sit amet consectetur adipisicing elit. Maiores laboriosam atque ut voluptates molestiae ab autem? Est consectetur voluptatem quia, commodi sint a omnis consequuntur. Tempora dolores ullam natus in?");

/* Enter wird im Input-Feld gedrückt */
input.onkeyup = e => {
    let code = e.keyCode ? e.keyCode : e.which;
    if (code === 13) {
        // TODO: Wert entgegennehmen
        closeWindow();
    }
}

/* ESC wird gedrückt */
document.onkeyup = e => {
    let code = e.keyCode ? e.keyCode : e.which;
    if (code === 27) { closeWindow(); }
}

close.onclick = e => { closeWindow(); }
