var express = require('express');
var router = express.Router();
var request = require('request');
var config = require('../config');
var moment = require('moment');
var fs = require('fs');

/* GET home page. */
router.get('/', function(req, res, next) {
    res.json({status: 'Up and running!'});
});

router.post('/', function(req, res, next) {
    fs.appendFile('testlog.txt', JSON.stringify(req.body), function (err) {
      if (err) return console.log(err);
    });

    res.status('200').end();
});

/* GET home page. */
router.get('/test', function(req, res, next) {
    // Just for testing shit
    var fakeData = {
        "Timestamp": "2015-02-12T14:00:00",
        "Resource": "Fixture",
        "Id": 149885,
        "URI": "http://api.football-data.org/v1/fixtures/149885",
        "Updates": "Score|1:0 -> 1:1;STATUS|OLD_VALUE -> NEW_VALUE"
    }

    // Request options
    var options = {
        method: 'GET',
        uri: fakeData.URI,
        headers: {
           'X-Auth-Token': config.token
        }
    };

    // Request the live data
    request(options, (error, response, body) => {
        if (response.statusCode === 200) {
            // Parse data to json
            var parsed = JSON.parse(body);

            // Parse response for update
            // TODO strip goals

            // Generate chat message
            var template = `Spielstand ${parsed.fixture.homeTeamName} : ${parsed.fixture.awayTeamName} 3:0`;
            req.app.locals.irc.say('#mulibu2k', template);

            res.json({status: 'ok'});
        } else {
            console.log('error');
            console.dir(body);
        }
    });
});

/* GET home page. */
router.post('/football-data.events', function(req, res, next) {
    // Just for testing
    fs.appendFile('testlog.txt', JSON.stringify(req.body), function (err) {
      if (err) return console.log(err);
    });

    res.status('200').end();

    // Do great stuff later
    // req.app.locals.irc.say('#mulibu2k', 'Hi, @Boerti1525! Nice to have u here!');
    // res.json('done');
});

/* GET home page. */
router.get('/fixtures', function(req, res, next) {
    var options = {
        method: 'GET',
        uri: 'http://api.football-data.org/v1/soccerseasons/424/fixtures',
        headers: {
           'X-Auth-Token': config.token
        }
    };

    request(options, (error, response, body) => {
        if (response.statusCode == 200) {
            res.json(JSON.parse(body).fixtures);
            // res.render('index', {games: JSON.parse(body)});
        } else {
            console.log('error');
            console.dir(body);
        }
    });
});

module.exports = router;

@@ -820,38 +820,8 @@
  1:1
-;STATUS%7COLD_VALUE -%3E NEW_VALUE
 %22%0A  
@@ -1239,79 +1239,135 @@
 date
-%0A            // TODO strip goals%0A%0A            // Generate chat message%0A
+ and generate chat message%0A            var message = '';%0A%0A            if(fakeData.Updates.match(/.+?(?=%5C%7C)/g) === 'Score') %7B%0A  
     
@@ -1378,19 +1378,82 @@
     
+  
 var 
-templat
+score = fakeData.Updates.match(/(%5Cd+:%5Cd+)/g);%0A                messag
 e = 
@@ -1532,13 +1532,179 @@
 me%7D 
-3:0%60;
+$%7Bscore%5B1%5D%7D%60;%0A            %7D else %7B%0A                message = %60Das Spiel  $%7Bparsed.fixture.homeTeamName%7D gegen $%7Bparsed.fixture.awayTeamName%7D hat begonnen.%60;%0A            %7D%0A
 %0A   
@@ -1752,15 +1752,14 @@
 k', 
-templat
+messag
 e);%0A

fix live ticker message