/*Scene manager
Author = DigS-Gov Projekt 
*/

var activeGame = false;
var reachedFinalPinboard = false;
var finalPopupAppeared = false;

// #region functions for the developer menu

// scenes
function skipToScene1() {
    if (activeGame == false) {
        activeGame = true;
        document.getElementById("gameSpace").scrollIntoView({ behavior: "smooth" });
        loadBackgroundImages();
        initPinboardButton();
        document.getElementById("pinboardButton").style.visibility="hidden";
        initFullscreen();
        initSound();
        setFontSize();
        fadeToBlack();
        document.getElementById("layer3").style.visibility = "hidden";
        window.setTimeout(initScene1, 1000);
        document.getElementById("playButton").remove();
    }
    else alert("Spiel läuft bereits!");
}

function skipToScene2() {
    if (activeGame == false) {
        activeGame = true;
        document.getElementById("gameSpace").scrollIntoView({ behavior: "smooth" });
        loadBackgroundImages();
        initPinboardButton();
        initFullscreen();
        initSound();
        setFontSize();
        fadeToBlack();
        document.getElementById("layer3").style.visibility = "hidden";
        window.setTimeout(initScene2, 1000);
        pinboardState = 3;
        pinboardProgress = 2;
        document.getElementById("playButton").remove();
    }
    else alert("Spiel läuft bereits!");
}

function skipToScene3() {
    if (activeGame == false) {
        activeGame = true;
        document.getElementById("gameSpace").scrollIntoView({ behavior: "smooth" });
        loadBackgroundImages();
        initPinboardButton();
        initFullscreen();
        initSound();
        setFontSize();
        fadeToBlack();
        document.getElementById("layer3").style.visibility = "hidden";
        window.setTimeout(initScene3, 1000);
        pinboardState = 5;
        pinboardProgress = 4;
        document.getElementById("playButton").remove();
    }
    else alert("Spiel läuft bereits!");
}

function skipToScene4() {
    if (activeGame == false) {
        activeGame = true;
        document.getElementById("gameSpace").scrollIntoView({ behavior: "smooth" });
        loadBackgroundImages();
        initPinboardButton();
        initFullscreen();
        initSound();
        setFontSize();
        fadeToBlack();
        document.getElementById("layer3").style.visibility = "hidden";
        window.setTimeout(initScene4, 1000);
        pinboardState = 7;
        pinboardProgress = 6;
        document.getElementById("playButton").remove();
    }
    else alert("Spiel läuft bereits!");
}

function skipToScene5() {
    if (activeGame == false) {
        activeGame = true;
        document.getElementById("gameSpace").scrollIntoView({ behavior: "smooth" });
        loadBackgroundImages();
        initPinboardButton();
        initFullscreen();
        initSound();
        setFontSize();
        fadeToWhite();
        document.getElementById("layer3").style.visibility = "hidden";
        window.setTimeout(initScene5, 1000);
        pinboardState = 9;
        document.getElementById("playButton").remove();
    }
    else alert("Spiel läuft bereits!");
}

//pinboards
function skipToPinboard1() {
    if (activeGame == false) {
        activeGame = true;
        document.getElementById("gameSpace").scrollIntoView({ behavior: "smooth" });
        loadBackgroundImages();
        initPinboardButton();
        document.getElementById("pinboardButton").style.visibility="hidden";
        initFullscreen();
        initSound();
        setFontSize();
        fadeToBlack();
        document.getElementById("layer3").style.visibility = "hidden";
        window.setTimeout(initPinboard, 1000);
        document.getElementById("playButton").remove();
    }
    else alert("Spiel läuft bereits!");
}

function skipToPinboard2() {
    if (activeGame == false) {
        activeGame = true;
        document.getElementById("gameSpace").scrollIntoView({ behavior: "smooth" });
        loadBackgroundImages();
        initPinboardButton();
        document.getElementById("pinboardButton").style.visibility="hidden";
        initFullscreen();
        initSound();
        setFontSize();
        fadeToBlack();
        document.getElementById("layer3").style.visibility = "hidden";
        window.setTimeout(initPinboard2, 1000);
        document.getElementById("playButton").remove();
        pinboardState = 3;
    }
    else alert("Spiel läuft bereits!");
}

function skipToPinboard3() {
    if (activeGame == false) {
        activeGame = true;
        document.getElementById("gameSpace").scrollIntoView({ behavior: "smooth" });
        loadBackgroundImages();
        initPinboardButton();
        document.getElementById("pinboardButton").style.visibility="hidden";
        initFullscreen();
        initSound();
        setFontSize();
        fadeToBlack();
        document.getElementById("layer3").style.visibility = "hidden";
        window.setTimeout(initPinboard3, 1000);
        document.getElementById("playButton").remove();
        pinboardState = 5;
        // pinboardProgress = 5;
    }
    else alert("Spiel läuft bereits!");
}

function skipToPinboard4() {
    if (activeGame == false) {
        activeGame = true;
        document.getElementById("gameSpace").scrollIntoView({ behavior: "smooth" });
        loadBackgroundImages();
        initPinboardButton();
        document.getElementById("pinboardButton").style.visibility="hidden";
        initFullscreen();
        initSound();
        setFontSize();
        fadeToBlack();
        document.getElementById("layer3").style.visibility = "hidden";
        window.setTimeout(initPinboard4, 1000);
        document.getElementById("playButton").remove();
        pinboardState = 7;
        // pinboardProgress = 7;
    }
    else alert("Spiel läuft bereits!");
}

function skipToPinboard5() {
    if (activeGame == false) {
        activeGame = true;
        document.getElementById("gameSpace").scrollIntoView({ behavior: "smooth" });
        loadBackgroundImages();
        initPinboardButton();
        document.getElementById("pinboardButton").style.visibility="hidden";
        initFullscreen();
        initSound();
        setFontSize();
        fadeToWhite();
        document.getElementById("layer3").style.visibility = "hidden";
        window.setTimeout(initPinboard5, 1000);
        document.getElementById("playButton").remove();
        pinboardState = 10;
    }
    else alert("Spiel läuft bereits!");
}

// #endregion functions for the developer menu

//---------------------------------------------------------------

//scene management

// #region functions to init Scenes

//initializing the tutorial
function initTutorial() {
    document.getElementById(activeLayer).style.backgroundImage = "url('images/scene0/Wohnzimmer.png')";
    document.getElementById(activeLayer).style.opacity = "100%";
    // window.setTimeout(drawHitboxesTutorial,1000);

    fadeToGrey();
    window.setTimeout(function() {playSound(dialogue_sounds.welcomePart1_2_audio)}, 1000);
    window.setTimeout('initScreenMessage(1)', 1000);
    // window.setTimeout(tutorialDialogue1, 1000);
    // activeGame = true;
}

function initScreenMessage(number) {
    const messageContainer = document.createElement("div");
    const message = document.createElement("div");
    message.id = "screenMessage"
    messageContainer.id = "screenMessageContainer";
    if (number == 1) {
        message.innerHTML += "Willkommen im Spiel \"Eine spielend leichte Geburt\"";
        message.innerHTML += "<br>";
        message.innerHTML += "<br>";
        message.innerHTML += "Hier können Sie spielerisch erfahren, wie das Bundeszentralamt für Steuern von der Geburt eines Kindes erfährt.";
    }
    if (number == 2) {
        message.innerHTML += "Wenn dieses Symbol erscheint, können Sie durch einen Klick darauf zu der Übersichtskarte zurückkehren.";
        // message.innerHTML += "<br>";
        // message.innerHTML += "<br>";
        // message.innerHTML += "Hier können Sie spielerisch erfahren, wie das Bundeszentralamt für Steuern von der Geburt eines Kindes erfährt.";
    }
    messageContainer.appendChild(message);
    document.getElementById("layer3").appendChild(messageContainer);

    createScreenMessageButton();
}

//initializing scene 1
function initScene1() {
    // LOG SCENE 1
    logData("Szene 1");
    document.getElementById(activeLayer).style.backgroundImage = "url('images/scene1/szene1_zimmer.jpg')";
    window.setTimeout(scene1_Dialogue_1.bind(null, drawHitboxesScene1));
}


//initializing scene 1_2
function initScene1_2() {
    Dialogue.remove();
    crossFade();
    showImage("letter");
    document.getElementById(activeLayer).style.backgroundImage = "url('images/scene1/szene1_hintergrund2.jpg')";
    drawHitboxesScene1_2();
    // playBirdSound();
}

//initializing scene 2
function initScene2() {
    // LOG SCENE 2
    logData("Szene 2");
    hideImage("pinHospital_red");
    hideImage("pinOffice_red");
    hideImage("pinAuthority_red");
    hideImage("pinBzfst_red");
    hideImage("pinFamily");
    hideImage("pinFamily_grey");
    document.getElementById("pinboardButton").style.visibility="visible";
    document.getElementById(activeLayer).style.backgroundImage = "url('images/scene2/Krankenhaus.png')";
    //playBacksound2();

    window.setTimeout(scene2_Dialogue_1, 1000);
}

function initScene2_1() {
    document.getElementById("pinboardButton").style.visibility="visible";
    removeAllHitboxes();
    Dialogue.remove();
    crossFade();
    drawHitboxesScene2_1();
    document.getElementById(activeLayer).style.backgroundImage = "url('images/scene2/klemmbrett.jpg')";

}

function initScene2_2() {
    document.getElementById("pinboardButton").style.visibility="visible";
    removeAllHitboxes();
    Dialogue.remove();
    playWritingSound();
    window.setTimeout(showImage.bind(null, "paper_text"), 1200);
    document.getElementById(activeLayer).style.backgroundImage = "url('images/scene2/klemmbrett.jpg')";
    window.setTimeout(scene2_Dialogue_2, 1500);

}

//initializing scene 3
function initScene3() {
    // LOG SCENE 3
    logData("Szene 3");
    printer = 0;
    //computer = 0;
    letter = 0;
    //clickedPostit = 0;
    //certificate = 0;
    hideImage("pinOffice_red");
    hideImage("pinAuthority_red");
    hideImage("pinBzfst_red");
    hideImage("pinFamily");
    hideImage("pinFamily_grey");
    document.getElementById("pinboardButton").style.visibility="visible";
    document.getElementById(activeLayer).style.backgroundImage = "url('images/scene3/Standesamt_buro.jpg')";
    window.setTimeout(scene3_Dialogue, 700);
    //playBacksound3();
}
//change the background to the desk and draw new hitboxes
function initScene3_1() {
    document.getElementById("pinboardButton").style.visibility="visible";
    try {
        hideImage("narrator");
    } catch (e) { }
    crossFade();
    removeAllHitboxes();
    Dialogue.remove();
    document.getElementById(activeLayer).style.backgroundImage = "url('images/scene3/Standesamt_Schreibtisch.jpg')";
    window.setTimeout(scene3_Dialogue2, 700);
}
//change the background to the desktop and draw new hitboxes
function initScene3_2() {
    document.getElementById("pinboardButton").style.visibility="visible";
    crossFade();
    removeAllHitboxes();
    Dialogue.remove();
    document.getElementById(activeLayer).style.backgroundImage = "url('images/scene3/Standesamt_pc.jpg')";
    showImage('window2');
    drawHitboxesScene3_3();
}
//change the background to the paper and draw new hitboxes
function initScene3_last() {
    document.getElementById("pinboardButton").style.visibility="visible";
    crossFade();
    removeAllHitboxes();
    Dialogue.remove();
    document.getElementById(activeLayer).style.backgroundImage = "url('images/scene3/Standesamt_geburtsurkunde.jpg')";
    window.setTimeout(scene3_Dialogue_last, 500);
}

//initializing scene 4
function initScene4() {
    // LOG SCENE 4
    logData("Szene 4");
    email = 0;
    database = 1;
    persondataOpen = 0;
    s = 0;
    n = 0;
    c = 0;
    hideImage("pinAuthority_red");
    hideImage("pinBzfst_red");
    hideImage("pinFamily");
    hideImage("pinFamily_grey");
    document.getElementById("pinboardButton").style.visibility="visible";
    Dialogue.remove();
    document.getElementById(activeLayer).style.backgroundImage = "url('images/scene4/Meldeamt_buro.jpg')";
    scene4_dialogue();
}

function initScene4_1() {
    document.getElementById("pinboardButton").style.visibility="visible";
    try {
        hideImage("narrator");
    } catch (e) { }
    crossFade();
    Dialogue.remove();
    document.getElementById(activeLayer).style.backgroundImage = "url('images/scene4/Meldeamt_schreibtisch.jpg')";
    scene4_dialogue1();
}

function initScene4_2() {
    document.getElementById("pinboardButton").style.visibility="visible";
    removeAllHitboxes();
    crossFade();
    Dialogue.remove();
    document.getElementById(activeLayer).style.backgroundImage = "url('images/scene4/Meldeamt_desktop.jpg')";
    window.setTimeout(playEmailSound, 1500);
    window.setTimeout(showImage.bind(null, "message"), 1500);
    window.setTimeout(drawHitboxesScene4_apps, 1500);
}

//initializing scene 5
function initScene5() {
    // LOG SCENE 5
    logData("Szene 5");
    hideImage("arrow_grey");
    hideImage("arrow_red");
    hideImage("pinAuthority_red");
    hideImage("pinBzfst_red");
    hideImage("pinFamily");
    hideImage("pinFamily_grey");
    document.getElementById("pinboardButton").style.visibility="visible";
    document.getElementById(activeLayer).style.backgroundImage = "url('images/szene5.jpg')";
    // window.setTimeout(scene1_Dialogue_1.bind(null, drawHitboxesScene5),1000);
    window.setTimeout(scene5_Dialogue, 1000);
}

//initializing scene 5_2
// function initScene5_2(){
//     Dialogue.remove();
//     removeAllHitboxes();
//     crossFade();
//     showImage("letter");
//     document.getElementById(activeLayer).style.backgroundImage = "url('images/scene1/szene1_hintergrund2.jpg')";
//     window.setTimeout(scene5_Dialogue,600);
// }

// #endregion functions to init Scenes

// #region functions to init Pinboards

//initializing pinboard after scene 1
function initPinboard() {
    // LOG PINBOARD 1
    logData("Pinnwand 1");

    document.getElementById("pinboardButton").style.visibility="hidden";
    document.getElementById(activeLayer).style.backgroundImage = "url('images/pinboard/szene1_pinnwand.png')";
    window.setTimeout(pinboard1_Dialogue1, 1100);
}
// initializing pinboard before scene 2 (hospital)
function initPinboard_1() {
    // LOG PINBOARD 1
    logData("Pinnwand 1");

    document.getElementById("pinboardButton").style.visibility="hidden";
    document.getElementById(activeLayer).style.backgroundImage = "url('images/pinboard/szene1_pinnwand.png')";
    // window.setTimeout(pinboard1_Dialogue1, 1100);

    pinboardState = 2;
    updatePinboard();
    pinboardProgress=2;
}

//initializing pinboard after scene 2
function initPinboard2() {
    // LOG PINBOARD 2
    logData("Pinnwand 2");

    if(pinboardProgress<3)pinboardProgress=3;
    document.getElementById("pinboardButton").style.visibility="hidden";
    drawHitboxesPinboard_2();
    document.getElementById(activeLayer).style.backgroundImage = "url('images/pinboard/szene2_pinnwand.jpg')";
}

// initializing pinboard before scene 3 (office)
function initPinboard_2() {
    // LOG PINBOARD 2
    logData("Pinnwand 2");

    document.getElementById("pinboardButton").style.visibility="hidden";
    document.getElementById(activeLayer).style.backgroundImage = "url('images/pinboard/szene2_pinnwand.jpg')";
    // window.setTimeout(pinboard1_Dialogue1, 1100);

    pinboardState = pinboardState - 2;
    updatePinboard();
}

//initializing pinboard after scene 3
function initPinboard3() {
    // LOG PINBOARD 3
    logData("Pinnwand 3");

    if(pinboardProgress<5)pinboardProgress=5;
    document.getElementById("pinboardButton").style.visibility="hidden";
    drawHitboxesPinboard_3();
    document.getElementById(activeLayer).style.backgroundImage = "url('images/pinboard/szene3_pinnwand.png')";

    // if(destatis == 1) showImage("pinDestatis");
    // if(standes == 1) showImage("pinStandes");
    // if(call == 1) hideImage("pinHideNews");

}

// initializing pinboard before scene 4 (authority)
function initPinboard_3() {
    // LOG PINBOARD 3
    logData("Pinnwand 3");

    document.getElementById("pinboardButton").style.visibility="hidden";
    document.getElementById(activeLayer).style.backgroundImage = "url('images/pinboard/szene3_pinnwand.png')";
    // window.setTimeout(pinboard1_Dialogue1, 1100);

    if(pinboardState==5){
        pinboardState--;
    }else{
        pinboardState = pinboardState - 2;
    }
    updatePinboard();
}

//initializing pinboard after scene 4
function initPinboard4() {
    // LOG PINBOARD 4
    logData("Pinnwand 4");

    if(pinboardProgress<7)pinboardProgress=7;
    document.getElementById("pinboardButton").style.visibility="hidden";
    drawHitboxesPinboard_4();
    document.getElementById(activeLayer).style.backgroundImage = "url('images/pinboard/szene4_pinnwand.jpg')";
}

// initializing pinboard before scene 5 (bzfst)
function initPinboard_4() {
    // LOG PINBOARD 4
    logData("Pinnwand 4");

    document.getElementById("pinboardButton").style.visibility="hidden";
    document.getElementById(activeLayer).style.backgroundImage = "url('images/pinboard/szene4_pinnwand.jpg')";
    // window.setTimeout(pinboard1_Dialogue1, 1100);

    pinboardState--;
    updatePinboard();
}

//initializing pinboard after scene 5
function initPinboard5() {
    // LOG PINBOARD 5
    logData("Pinnwand 5");

    document.getElementById("pinboardButton").style.visibility="hidden";
    reachedFinalPinboard = true;
    if (destatis == 1) showImage("pinDestatis");
    if (standes == 1) showImage("pinStandes");
    if (news == 1) showImage("pinNews");
    if (call == 0) showImage("pinHideNews");
    if (renten == 1) showImage("pinRenten");
    if (lasd == 1) showImage("pinLASD");
    family == 1 ? showImage("pinFamily") : showImage("pinFamily_grey");
    showImage("pinBzfst_red");
    showImage("arrow_red");
    document.getElementById(activeLayer).style.backgroundImage = "url('images/pinboard/szene4_pinnwand.jpg')";
    window.setTimeout(gameEnd, 1500);
    pinboardProgress=10;
}

// #endregion functions to init Pinboards
