
// Universal puzzle functions
// TODO: These probably can be moved to the index.html script

const newExpiration = (additionalSeconds) => {
    let date = new Date();
    let seconds = Math.round(date.getTime() / 1000);
    return seconds + additionalSeconds;
};

// This function sends the player's data to the server and then retrieves
// the data the other players synced as well.
const syncPuzzleData = (data_name, data_value) => {
    ws.send(JSON.stringify({
        "message": "Sync me, please!",
        "game": game,
        "puzzle": data_name,
        "data": data_value
    }));
    console.log("syncing");
    return combineDataSync(data_name, data_value);
};

// This loads the reward assets when a player wins a puzzle
const loadTheWin = () => {
    gameApp.innerHTML = puzzles[currentPuzzle["puzzle"]]["solution"];
    ws.send(JSON.stringify({
        "message": "Winner, winner, chicken dinner!",
        "game": game
    }));
    if (currentPuzzle["puzzle"] == "locked-door") {
        const smash = new Audio("./assets/smash.mp3");
        smash.play();
    }
};

const removeCrack = (area) => {
    document.getElementById(area).classList.remove("cracked");
};

const pauseHitting = () => {
    hittingSound.pause();  
};

const hittingSound = new Audio("./assets/hitting.mp3");

// This is run when a player "hits" (clicks) any area of the door.
// The area responds to one of the keys in data_locked_door["values"].
const run_locked_door = (area) => {
    console.log("run_locked_door: check of data_locked_door");
    console.log(data_locked_door);
    console.log("checking corrected_data_locked_door");
    console.log(corrected_data_locked_door);
    document.getElementById(area).classList.add("cracked");
    hittingSound.play();
    setTimeout(removeCrack, 500, area);

    // Handle the data

    data_locked_door.append({
        "expiration": newExpiration(30),
        "value": area
    });

    syncPuzzleData("data_locked_door", data_locked_door);
};

// safe
// TODO: Put the next puzzle's run event here


let data_locked_door = [];

// Later this will be a combination of all players' data synced.
// This is the object we want to check for the winning condition.
let corrected_data_locked_door = [];

// We sync data here for any puzzles that require multiple players
// to work together
const combineDataSync = (data_name) => {
    if (data_name == "data_locked_door") {
        let new_locked_door_data = {
            "area_1": 0,
            "area_2": 0,
            "area_3": 0,
            "area_4": 0,
            "area_5": 0,
            "area_6": 0
        };
        
        // Loop through the players' data and increment the dictionary we track
        for (const [expiration, value] of Object.entries(synced_data_value)) {
            for (let i=0; i < value.length; i++) {
                if (new_locked_door_data[value][i]) {
                    new_locked_door_data[value][i]++
                }
            }
        }
        
        // Check if the player won
        for (const [area, value] of Object.entries(new_locked_door_data)) {
            if (value < 9) {
                return;
            }
        }
        // If we got down here, all values were above 9
        loadTheWin();
        return;
    }
};