function initAll() {
    darkMode();
    setLang("fr");
    dysFont();

}

var mobile = false;
var switchs = [],
    lineCount = [],
    logiques = [],
    lines = [],
    endLines = [],
    end, switchsInfo = [],
    switchsInfoCopy = [],
    lineRemove = [];
var width = window.innerWidth - window.innerWidth / 100 * 30;
var height = window.innerHeight / 2.5;
var stage = new Konva.Stage({
    container: 'setting_container',
    width: width,
    height: height,
});
var layer = new Konva.Layer();
tutoCreateLayer();

function tutoCreateLayer() {
    isTuto = true;
    width = window.innerWidth - window.innerWidth / 100 * 30;
    height = window.innerHeight / 2.5;
    stage = new Konva.Stage({
        container: 'setting_container',
        width: width,
        height: height,
    });
    stage.add(layer);

    colonneTot = 1;
    numberPerColonne = [1];
    var logiqueCount = 0;

    for (let i = 0; i < colonneTot; i++) {
        liveColonneNumber.push([]);
    }
    insertLogiqueColonne("logique1", "et", 0);
    calculNombreSwitch();
    switchCreator(numberOfSwitch);
    createAllLinkSwitch();

    initAllSwitch();
    createEnd();
    initEnd();
}

width = (window.innerWidth - window.innerWidth / 100 * 10);
height = window.innerHeight / 1.5;

function resetAllTabs(save) {
    if (!save) {
        logiques = [];

        numberPerColonne = [];
        liveColonneNumber = [];
        timeEnd = 5;


        numberOfSwitch = 0;

        colonneTot = 0;
    }

    switchs = [], lineCount = [], lines = [], endLines = [], end, switchsInfo = [], lineRemove = [], switchsInfoCopy = [];

    layer.destroyChildren();
    stage.draw();
}

function gameOne() {
    document.querySelector("#play_container").style.maxHeight = window.innerHeight / 1.5 + "px";
    createGameOne(niveauActuel);
    isLineCollapsing();
    click = 0;
    var i = 0;
    while (checkIfEnd() || isLineCollapsing()) {
        if (i === NOMBRETEST) {
            break;
        }
        resetAllTabs(false);
        createGameOne(niveauActuel);
        isLineCollapsing();
        i++;
    }
    //console.log(stage);
    //gameData = new GameData(height,width,2,10,"mode1",false,"play_container");
    //gameData.createGame();

    //checkAllSortieLogique();
}


function initEndGame(mode) {

    calculNombreSwitch();
    switchCreator(numberOfSwitch);

    createAllLinkSwitch();

    initAllSwitch(mode);

    logiques.forEach(function (element) {
        checkEntreCroisement(element.name);
    });

    createEnd();
    initEnd();

    initTimer();

    checkAllSortieLogique();
}

function resetCache() {
    openModal("reset_cache");
    setTimeout(() => {
        location.reload();
    }, 3000);
}

window.addEventListener('resize', saveStage);

function getSwitchState() {
    var stateSwitch = new Array();
    switchsInfoCopy.forEach(function (element) {
        if (stage.findOne("#" + element.id).fill() == colorSwitchActiveBackground) stateSwitch.push(1);
        else stateSwitch.push(0);
    });
    return stateSwitch;
}

function saveStage() {
    var oldSwitch = getSwitchState();
    resetAllTabs(true);
    if (isTuto) {
        createStage("setting_container");
    } else {
        createStage("play_container");
    }

    stage.add(layer);
    logique2 = logiques;
    logiques = [];
    var logiqueCount = 0;
    for (let i = 0; i < colonneTot; i++) {
        liveColonneNumber[i] = [];
    }
    for (let i = 0; i < colonneTot; i++) {
        for (let j = 0; j < numberPerColonne[i]; j++) {
            insertLogiqueColonne("logique" + logiqueCount, logique2[logiqueCount].type, i);
            logiqueCount++;
        }
    }
    logiques.forEach(function (element) {
        createLinkAuto(element.name);
    });

    initEndGame();

    for (let i = 0; i < oldSwitch.length; i++) {
        let id = i + 1;
        if (oldSwitch[i] == 1) {
            if (stage.findOne("#s" + id).fill() == colorSwitchInnactiveBackground)
                activeSwitch("s" + id);
        } else {
            if (stage.findOne("#s" + id).fill() == colorSwitchActiveBackground)
                activeSwitch("s" + id);
        }
    }
    checkAllSortieLogique();

}

function showInfo() {
    if (document.querySelector("#info_check_input").checked) allowedInfoPorte = false;
    else allowedInfoPorte = true;

}