
function openModal(modalId) {
    var modal = document.getElementById(modalId);
    modal.style.display = "block";


}
function closeModal(modal) {
    var modal = document.getElementById(modal);
    modal.style.display = "none";
}

function darkMode() {
    var checkbox = document.getElementById("darkcheck");
    if (checkbox.checked == true) {
        setColor("black");
    }
    else {
        setColor("white");
    }
}
function dysFont() {
    var checkbox = document.getElementById("dyscheckbox");
    var textToChange = document.querySelectorAll(".dys");
    var font;
    if (checkbox.checked == true) {
        font = "dys";
    }
    else {
        font = "main";
    }
    textToChange.forEach(function (item) {
        item.style.fontFamily = font;
    })
    document.getElementById("jeu-select").style.fontFamily = font;
}

function setColor(color) {
    if (color == "black") {
        var backgroundToChange = document.querySelectorAll(".background");
        backgroundToChange.forEach(function (element) {
            element.style.backgroundColor = blackColor;
        });
        document.body.style.color = whiteColor;
        document.getElementById("setting_int").style.color = whiteColor;
        var hr = document.querySelectorAll("#hr");
        hr.forEach(function (item) {
            item.setAttribute("style", "border : 3px solid " + whiteColor);
        });
        document.querySelectorAll(".border").forEach(function (element) {
            element.style.border = "5px solid" + whiteColor;
        });

    } else {
        document.body.style.color = blackColor;
        var backgroundToChange = document.querySelectorAll(".background");
        backgroundToChange.forEach(function (element) {
            element.style.backgroundColor = whiteColor;
        });
        document.getElementById("setting_int").style.color = blackColor;
        var hr = document.querySelectorAll("#hr");
        hr.forEach(function (item) {
            item.setAttribute("style", "border : 3px solid " + blackColor);
        });
        document.querySelectorAll(".border").forEach(function (element) {
            element.style.border = "5px solid" + blackColor;
        });
    }
    //fun
    document.querySelector(".secret").style.color = "#fe8a71";
}

function setLang(newLang) {
    var textToChange = document.querySelectorAll(".txt");
    if (newLang === "fr") {
        textToChange.forEach(function (item) {
            let currText = item.id;
            document.getElementById(currText).innerHTML = lang.fr[currText];
        });
        document.querySelector(".img_france").style.opacity = 1;
        document.querySelector(".img_usa").style.opacity = 0.2;
    }
    else {
        textToChange.forEach(function (item) {
            let currText = item.id;
            document.getElementById(currText).innerHTML = lang.en[currText];
        });
        document.querySelector(".img_france").style.opacity = 0.2;
        document.querySelector(".img_usa").style.opacity = 1;
    }
}
function calculNombreSwitch() {
    var entrelibre = 0;
    logiques.forEach(function (element) {

        if (element.id1 == null) {
            entrelibre++;
        }
        if (element.id2 == null && element.type != "inv") {
            entrelibre++;
        }
    });
    numberOfSwitch = entrelibre;
}
function whatIsElement(element) {
    if (findLogique(element.name) == null || findLogique(element.name) == undefined) {
        return "switch";
    } else {
        return "logique";
    }
}

function setLine(logiqueElement, lineId, lineName) {
    logiques.forEach(function (element, index) {
        if (element.name === logiqueElement.name) {
            Object.keys(element).map(function (objectKey, index) {
                if (objectKey == lineId) {
                    element[objectKey] = lineName;
                }
            });
        }
    });
}

function changeLineColor(idLine) {
    let line = stage.findOne("#" + idLine);
    var colorline = line.stroke() == colorLineActive ? colorLineInnactive : colorLineActive;
    line.stroke(colorline);
}
function changeLineColorInput(idLine, color) {
    let line = stage.findOne("#" + idLine);
    console.log("changer");
    line.stroke(color);
}
function changeLineColorBlack(idLine) {
    let line = stage.findOne("#" + idLine);
    line.stroke(colorLineInnactive);
}
function changeLineColorYellow(idLine) {
    let line = stage.findOne("#" + idLine);
    line.stroke(colorLineActive);
}

function checkAllSortieLogique() {
    logiques.forEach(function (element) {
        if (element.id3 != null)
            checkSortieLogique(element.name);
    });
}

function checkSortieLogique(logiqueId) {
    let logique = findLogique(logiqueId);
    logiques.forEach(function (element, index) {
        if (element.name === logique.name) {
            let line1State, line2State;
            let id1Color = stage.findOne("#" + element.id1).stroke();
            if (element.type !== "inv") {
                let id2Color = stage.findOne("#" + element.id2).stroke();
                if (id2Color == colorLineInnactive) {
                    line2State = false;
                } else {
                    line2State = true;
                }
            }

            if (id1Color == colorLineInnactive) {
                line1State = false;
            } else {
                line1State = true;
            }

            switch (logique.type) {
                case "et":
                    if (line1State == true && line2State == true) {
                        changeLineColorYellow(element.id3);
                    }
                    else {
                        changeLineColorBlack(element.id3);
                    }
                    break;
                case "inv":
                    if (line1State == false) {
                        changeLineColorYellow(element.id3);
                    }
                    else {
                        changeLineColorBlack(element.id3);
                    }
                    break;
                case "ou":
                    if (line1State == true || line2State == true) {
                        changeLineColorYellow(element.id3);
                    }
                    else {
                        changeLineColorBlack(element.id3);
                    }
                    break;
                case "nonou":
                    if (line1State == true || line2State == true) {
                        changeLineColorBlack(element.id3);
                    }
                    else {
                        changeLineColorYellow(element.id3);
                    }
                    break;
                case "etnon":
                    if (line1State == false && line2State == false) {
                        changeLineColorYellow(element.id3);
                    } else if (line1State == false && line2State == true) {
                        changeLineColorYellow(element.id3);
                    } else if (line1State == true && line2State == false) {
                        changeLineColorYellow(element.id3);
                    } else {
                        changeLineColorBlack(element.id3);
                    }
                    break;
            }
        }
    });
}

function getRandomArbitrary(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
}

function isElementExisting(elementId) {
    if (stage.findOne("#" + elementId) != null) {
        return true;
    }
    return false;
}

function checkLineSwitch(switchId) {
    if (stage.findOne("#" + switchId) != null) {
        return true;
    }
    return false;
}

function checkEnd() {
    let ter = true;
    endLines.forEach(function (element) {
        if (stage.findOne("#line" + element).stroke() == "black") {
            ter = false;

        }
    });
    if (ter && !isTuto) {
        setTimeout(() => { nextLevel(); }, 100);
    }
}

function nextLevel() {
    niveauActuel++;
    timeTot = timeTot + timer.getTimeValues().seconds;
    launchGameOne();
}

function calculNombreSwitch() {
    var entrelibre = 0;
    logiques.forEach(function (element) {

        if (element.id1 == null) {
            entrelibre++;
        }
        if (element.id2 == null && element.type != "inv") {
            entrelibre++;
        }
    });
    numberOfSwitch = entrelibre;
}



function resize() {
    console.log(window.innerWidth);
    stage.scale({ x: 1, y: 1 });
    stage.draw();
    console.log(stage);
}

function activeSwitchInput(event) {
    changeAllSwitchColorActive(event.target.value);
}
function inactiveSwitchInput(event) {
    changeAllSwitchColorInActive(event.target.value);
}
function activeLineInput(event) {
    changeAllLineColorActive(event.target.value);
}
function inactiveLineInput(event) {
    changeAllLineColorInActive(event.target.value);
}
function inactiveEndInput(event) {
    changeEndColorInActive(event.target.value);
}

document.querySelector("#active_switch_picker").addEventListener("input", activeSwitchInput, false);
document.querySelector("#inactive_switch_picker").addEventListener("input", inactiveSwitchInput, false);
document.querySelector("#active_line_picker").addEventListener("input", activeLineInput, false);
document.querySelector("#inactive_line_picker").addEventListener("input", inactiveLineInput, false);
document.querySelector("#inactive_end_picker").addEventListener("input", inactiveEndInput, false);

function changeAllSwitchColorActive(color) {
    switchsInfoCopy.forEach(function (element) {
        var switche = stage.findOne("#" + element.id);
        if (switche.fill() == colorSwitchActiveBackground) {
            switche.fill(color);
        }
    });
    colorSwitchActiveBackground = color;
    layer.draw();
}
function changeEndColorInActive(color) {
    stage.findOne("#end").fill(color);
    layer.draw();
}
function changeAllSwitchColorInActive(color) {
    switchsInfoCopy.forEach(function (element) {
        var switche = stage.findOne("#" + element.id);
        if (switche.fill() == colorSwitchInnactiveBackground) {
            switche.fill(color);
        }
    });
    colorSwitchInnactiveBackground = color;
    layer.draw();
}

function changeAllLineColorActive(color) {
    var line1 = stage.findOne("#line2");
    var line2 = stage.findOne("#line3");
    var line3 = stage.findOne("#line4");
    if (line1.stroke() === colorLineActive) {
        line1.stroke(color);
    }
    if (line2.stroke() === colorLineActive) {
        line2.stroke(color);
    }
    if (line3.stroke() === colorLineActive) {
        line3.stroke(color);
    }
    colorLineActive = color;
    layer.draw();
}
function changeAllLineColorInActive(color) {
    var line1 = stage.findOne("#line2");
    var line2 = stage.findOne("#line3");
    var line3 = stage.findOne("#line4");
    if (line1.stroke() === colorLineInnactive) {
        line1.stroke(color);
    }
    if (line2.stroke() === colorLineInnactive) {
        line2.stroke(color);
    }
    if (line3.stroke() === colorLineInnactive) {
        line3.stroke(color);
    }
    colorLineInnactive = color;
    layer.draw();
}

function play() {
    niveauActuel = 1;
    document.querySelector("#mainPage").style.display = 'none';
    document.querySelector("#playPage").style.display = 'flex';
    timeTot = 0;
    launchGameOne();
}

function launchGameOne() {
    document.querySelector(".niveau").innerHTML = niveauActuel;
    timer.stop();
    timer.reset();
    resetAllTabs();
    gameOne();
}

function clearArray(arr) {
    while (arr.length > 0) {
        arr.pop();
    }
    return arr.splice(0, arr.length);
}

function closeGame() {
    timer.stop();
    document.querySelector("#mainPage").style.display = 'block';
    document.querySelector("#playPage").style.display = 'none';
    resetAllTabs();
    tutoCreateLayer();
}
var rotated = false;
function flip() {
    document.querySelectorAll(".fa").forEach(function (element) {
        deg = rotated ? 0 : 360;
        element.style.transform = 'rotate(' + deg + 'deg)';
    });
    rotated = !rotated;
}