const io = require('./socketio');
const db = require('../controllers/db');

let enabled = true;
let teamA = {
    name: "Team A",
    name2: "",
    score: 0,
    isSpielgemeinschaft: 0,
};
let teamB = {
    name: "Team B",
    name2: "",
    score: 0,
    isSpielgemeinschaft: 0,
};
let sideswitch = false;             // Attribute to switch sides after halftime
let referenceMirrored = false;      // If sitting behind the scoreboard, switch sides for admin board

// Function prototypes
function setEnabled(status) {};
function configTeam(team, name, name2, isSpielgemeinschaft) {};
function setScore(team, score) {};
function alterScore(team, dir) {};
function clearScore() {};
function toggleSideswitch() {};
function getEnabled() {};
function getValues() {};

// Enable or disable the scoreboard
function setEnabled(status) {
    enabled = status;
}

function configTeam(team, name, name2, isSpielgemeinschaft) {
    if(team == "teamA") {
        teamA.name = name;
        teamA.name2 = name2;
        teamA.isSpielgemeinschaft = isSpielgemeinschaft;
    } else if(team == "teamB") {
        teamB.name = name;
        teamB.name2 = name2;
        teamB.isSpielgemeinschaft = isSpielgemeinschaft;
    }
}

// Set score of team to passed value
function setScore(team, score) {
    if(team == "teamA") {
        teamA.score = score;
    } else if(team == "teamB") {
        teamB.score = score;
    }
}

// Increment score by one
function alterScore(team, dir) {
    if(team == "teamA") {
        if(dir == "inc") {
            teamA.score++;
            console.log("teamA inc");
        }
        else if(dir == "dec") {
            teamA.score--;
            console.log("teamA dec");
        }
    } else if(team == "teamB") {
        if(dir == "inc") {
            teamB.score++;
            console.log("teamB inc");
        }
        else if(dir == "dec") {
            teamB.score--;
            console.log("teamB dec");
        }
    }
    io.sockets.emit('score', print());
}

// Clears score of both teams
function clearScore() {
    teamA.score = 0;
    teamB.score = 0;
    io.sockets.emit('score', print());
}

// Toggle sideswitch
function toggleSideswitch() {
    console.log("Seiten gewechselt");
    sideswitch = !sideswitch;
    io.sockets.emit('score', print());
    io.sockets.emit('scoreSideswitch', '');
}

// Mirror team position on admin panel
function toggleReferenceMirrored() {
    referenceMirrored = !referenceMirrored;
}

// Return enabled value
function getEnabled() {
    return enabled
}

// Print current score depending on sideswitch
function print() {
    if(sideswitch) {
        return teamB.score + ":" + teamA.score
    } else {
        return teamA.score + ":" + teamB.score
    }
}

// Return all important values
function getValues() {
    return {
        enabled: enabled,
        teamA: teamA,
        teamB: teamB,
        sideswitch: sideswitch,
        referenceMirrored: referenceMirrored,
        print: print(),
        teams: db.getTeams(),
    };
}

module.exports = {
    setEnabled, configTeam, setScore, alterScore, clearScore, toggleSideswitch, toggleReferenceMirrored, getEnabled, getValues,
}