let actualTimer = 0;
let TimerTimeout = 3000;

function DigitalSignage4_showSuccessMessage(message, prepend = true) {
    DigitalSignage4_addElementInDigitalSignageInfosContainer(DigitalSignage4_createDigitalSignageInfoElement(message), prepend);
}

function DigitalSignage4_showErrorMessage(message, prepend = true) {
    DigitalSignage4_addElementInDigitalSignageInfosContainer(DigitalSignage4_createDigitalSignageInfoElement(message, true), prepend);
}

function DigitalSignage4_createDigitalSignageInfoElement(message, isError = false) {

    let infoElement = document.createElement('div');
    infoElement.classList.add('digitalSignageInfo');
    infoElement.classList.add((isError) ? 'digitalSignageInfoError' : 'digitalSignageInfoSuccess');
    infoElement.innerHTML = message;

    actualTimer = actualTimer + TimerTimeout;

    setTimeout(
        () => {
            infoElement.parentElement.removeChild(infoElement);
            actualTimer = actualTimer - TimerTimeout;
            if (actualTimer < 0) {
                actualTimer = 0;
            }
        },
        actualTimer
    );

    return infoElement;
}

function DigitalSignage4_addElementInDigitalSignageInfosContainer(element, prepend = true) {
    let infosContainer = DigitalSignage4_getDigitalSignageInfosContainerElement();
    if (prepend) {
        infosContainer.prepend(element);
    } else {
        infosContainer.append(element);
    }
}

function DigitalSignage4_getDigitalSignageInfosContainerElement() {

    let wpContent = document.getElementById('wpbody');

    if (!wpContent)
        wpContent = document.getElementById('content');

    let infosContainerId = 'digitalSignageInfos';
    if (wpContent.firstElementChild.id !== infosContainerId) {
        let infosContainer = document.createElement('div');
        infosContainer.id = infosContainerId;
        infosContainer.classList.add('digitalSignageInfos');
        wpContent.prepend(infosContainer);
    }

    return wpContent.firstElementChild;

}