{"size": 2592, "ext": "js", "max_stars_repo_path": "routes/chat.js", "max_stars_repo_name": "moelaknahour23/A.J.A.M.K.A", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "routes/chat.js", "max_issues_repo_name": "moelaknahour23/A.J.A.M.K.A", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "routes/chat.js", "max_forks_repo_name": "moelaknahour23/A.J.A.M.K.A", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.6097560976, "max_line_length": 111, "alphanum_fraction": 0.5119598765}
var express = require('express');
var router = express.Router();
var io = require('socket.io')();
var database = require('../custom_modules/database');
this.io = io;
var socket = require('../custom_modules/socket');
var port = 3000;

var REQUEST_TYPE = {
    GET_CHAT_MESSAGES: 'get-messages',
    INSERT_CHAT_MSG: 'post-chat'
}

router.get('/chat', function(req, res) {

    var authenticated = req.isAuthenticated();
    
    if(authenticated){
        //Open specific chatroom depending on id:
        var chatroomid = parseInt(req.query.id);
        if(chatroomid){
            database.getChatMessages(chatroomid, function(err, results){
                    if(err){
                        res.sendStatus(500);
                    } else {                            
                            res.render('chat', {
                            authenticated: req.isAuthenticated(),
                            userData: req.user,
                            data: results,
                            chatroomid: chatroomid
                        });
                    }
            });
        } else {
            console.log("No id specified for chatroom, showing generic chatroom instead");
            
            //Render generic chatroom, remove this when id is implemented
            res.render('chat', {authenticated: req.isAuthenticated()});
        }
    } else 
        res.render('authenticationError');
})

router.get('/chathome', function(req, res){
    var authenticated = req.isAuthenticated();
        if(authenticated){
        res.render('chathome', {authenticated: req.isAuthenticated(),
            userData: req.user
        });
    } else {
        res.render('authenticationError');
    }
});

router.post('/chatapi', function(req, res){
    var reqType = req.body.reqType;

    switch(reqType){
        case REQUEST_TYPE.GET_CHAT_MESSAGES:
            database.getChatMessages(req.body.chatroomid, function(err, results){
                if(err){
                    res.sendStatus(500);
                } else {
                    res.json(results);
                }
            });
            break;
        
        case REQUEST_TYPE.INSERT_CHAT_MSG:
            database.insertChatMessage(req.body.chatroomid, req.body.msg, parseInt(req.user.id), function(err){
                if(err){
                    console.log("Error inserting chat message");
                    res.sendStatus(500);
                } else {
                    res.sendStatus(200);
                }
            });
            break;
    }
});

module.exports = router;