var express = require('express');
var router = express.Router();
const io = require('../controllers/socketio');

const timer = require('../controllers/timer');
const score = require('../controllers/score');
const cli = require('../controllers/cli');
const db = require('../controllers/db');
const etc = require('../controllers/etc');

// ######################################################################################################################################################
// General endpoints
// ######################################################################################################################################################

// Express router endpoint to trigger a frontend refresh on all connected sockets
router.get('/refreshMonitor', function(req, res, next) {
    console.log("Monitor neu geladen");
    io.sockets.emit('refresh', "");         // Emit refresh to all connected sockets over websockets
    res.status(200);                        // Set http status code to 200 (success)
    res.send();                             // send empty response
});

// Express router endpoint to open browser
router.get('/openBrowser', function(req, res, next) {
    cli.openBrowser();
    res.status(200);                        // Set http status code to 200 (success)
    res.send();                             // send empty response
});

// Express router endpoint to kill browser
router.get('/killBrowser', function(req, res, next) {
    cli.killBrowser();
    res.status(200);                        // Set http status code to 200 (success)
    res.send();                             // send empty response
});

// ######################################################################################################################################################
// Etc endpoints
// ######################################################################################################################################################

// Express router endpoint to toggle splashscreen
router.get('/etcToggleSplashscreen', function(req, res, next) {
    etc.toggleSplashScreen();               // Toggles splashscreen
    res.status(200);                        // Set http status code to 200 (success)
    res.json(etc.getValues());              // Answer with important values
});

// Express router endpoint to get important etc values
router.get('/etcGetValues', function(req, res, next) {
    res.status(200);                        // Set http status code to 200 (success)
    res.json(etc.getValues());              // Answer with important values
});

// ######################################################################################################################################################
// Timerendpoints
// ######################################################################################################################################################

// Express router entpoint to start the timer
router.get('/timerStart', function(req, res, next) {
    if(timer.start()) {                     // If successfully started the timer
        res.status(200);                    // Set http status code to 200 (success)
        res.json(timer.getValues());        // Respond with all important values to update the frontend
    } else {                                // If not successfull, for example because timer is already running or not time is left
        res.status(406);                    // Set http status code to 406 (not acceptable)
        res.send();                         // Respond with empty payload
    }
});

// Express router endpoint to pause the timer
router.get('/timerPause', function(req, res, next) {
    if(timer.pause()) {                     // If successfully paused the timer
        res.status(200);                    // Set http status code to 200 (success)
        res.json(timer.getValues());        // Respond with all important values to update the frontend
    } else {                                // If not successfull, for example if timer already is paused
        res.status(406);                    // Set http status code to 406 (not acceptable)
        res.send();                         // Respond with empty payload
    }
});

// Express router endpoint to reset the timer
router.post('/timerReset', function(req, res, next) {
    timer.reset(req.body.duration);         // Reset the timer with the duration in seconds received from the request
    res.json(timer.getValues());            // Respond with all important values to update the frontend
});

router.post('/timerIncDec', function(req, res, next) {
    timer.incDec(req.body.value);
    res.json(timer.getValues());
});

// Express router endpoint to get important timer values
router.get('/timerGetValues', function(req, res, next) {
    res.json(timer.getValues());            // Respond with important values for frontend
});

// ######################################################################################################################################################
// Scoreboard endpoints
// ######################################################################################################################################################

// Express router endpoint to toggle the scoreboard
router.get('/scoreToggle', function(req, res, next) {
    if(score.getEnabled()) {                // If scoreboard enabled
        console.log("Scoreboard disabled");
        score.setEnabled(false)             // Disable the scoreboard
        res.status(200);                    // Set http status code to 200 (success)
        res.json(score.getValues())         // Respond with all important values to update the frontend
    } else {
        console.log("Scoreboard enabled");
        score.setEnabled(true)              // Enable the scoreboard
        res.status(200);                    // Set http status code to 200 (success)
        res.json(score.getValues());        // Respond with all important values to update the frontend
    }
});

// Express router endpoint to set score of a team
router.post('/scoreSetScore', function(req, res, next) {
    score.setScore(req.body.team, req.body.score);
    res.status(200);
    res.json(score.getValues());
});

// Express router endpoint to alter passed teams score by one in the passed direction
router.post('/scoreAlterScore', function(req, res, next) {
    score.alterScore(req.body.team, req.body.dir);
    res.status(200);
    res.json(score.getValues());
});

// Express router endpoint to switch the score after halftime
router.get('/scoreToggleSideswitch', function(req, res, next) {
    score.toggleSideswitch();
    res.status(200);
    res.json(score.getValues());
});

// Express router endpoint to toggle reference mirrored attribute
router.get('/scoreToggleReferenceMirrored', function(req, res, next) {
    score.toggleReferenceMirrored();
    res.status(200);
    res.json(score.getValues());
});

// Express router endpoint to get important score values
router.get('/scoreGetValues', function(req, res, next) {
    res.json(score.getValues());            // Respond with important values for frontend
});

// Express router endpoint to clear score
router.get('/scoreClearScore', function(req, res, next) {
    score.clearScore();                     // Clear score
    res.json(score.getValues());            // Respond with important values for frontend
});

router.post('/scoreConfigTeams', function(req, res, next) {
    score.configTeam("teamA", req.body.teamA.name, req.body.teamA.name2, req.body.teamA.isSpielgemeinschaft);
    score.configTeam("teamB", req.body.teamB.name, req.body.teamB.name2, req.body.teamB.isSpielgemeinschaft);
    console.log(score.getValues());
    res.json(score.getValues());
});

// ######################################################################################################################################################
// DB endpoints
// ######################################################################################################################################################

// Express router endpoint to get important values for db contents
router.get('/dbGetValues', function(req, res, next) {
    res.json(db.getValues());               // Respond with important values for frontend
});

// Express router endpoint to add a team to the team database
router.post('/dbAddTeam', function(req, res, next) {
    db.addTeam(req.body.teamName);          // Add teamname to DB
    res.json(db.getValues());               // Respond with all important values to update the frontend
});

// Express router endpoint to delete a team from the team database
router.post('/dbDeleteTeam', function(req, res, next) {
    db.deleteTeam(req.body.teamName);          // Add teamname to DB
    res.json(db.getValues());               // Respond with all important values to update the frontend
});

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('admin');
});

module.exports = router;
