/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var logunit = require('../src/log/log.js');
var listunut = require('../src/list.js');
var Log = logunit.Log;
var Map = listunut.List;
var log, map;

var app = express.createServer();

updatePage = function(req, res, key, value, key2, value2) {
  var msgs = '<ul>', flash = req.flash();
  Object.keys(flash).forEach(function(type){
    flash[type].forEach(function(msg){
      msgs += '<li class="' + type + '">' + msg + '</li>';
    });
  });
  msgs += '</ul>';
  res.send(msgs
          + '<form method="post">'
          + '<fieldset>'
          + '<legend>Adding values:</legend>'
          + 'Key: <input type="text" name="key" value="'
          + (key != undefined ? key : '') + '" /> <br/>'
          + 'Value: <input type="text" name="value" value="'
          + (value != undefined ? value : '') + '" /> <br/>'
          + '<input type="submit" value="Push!" />'
          + '</fieldset>'
          + '</form>'
          + '<fieldset>'
          + '<legend>Getting values:</legend>'
          + '<form method="post">'
          + '<input type="hidden" value="put" name="_method" />'
          + 'Key: <input type="text" name="key2" value="'
          + (key2 != undefined ? key2 : '') + '" /> <br/>'
          + 'Value: <input type="text" name="value2" value="'
          + (value2 != undefined ? value2 : '') + '" /> <br/>'
          + '<input type="submit" value="Get!" />'
          + '</fieldset>'
          + '</form>');
};

// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({ secret: 'keyboard cat' }));
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
  log = new Log('/tmp/server-test/', 8);
  map = new Map(log);
  map.on('push', function(node) {
    map.toLog();
  });
});

app.get('/', function(req, res){
  var key = req.param('key');
  var value = req.param('value');
  var key2 = req.param('key2');
  var value2 = req.param('value2');
  updatePage(req, res, key, value, key2, value2);
});

app.post('/', function(req, res){
  if (req.body.key) {
    map.once('write', function() {
      req.flash('info', 'Successfully saved log on disk');
      res.redirect('/?key=' + req.body.key + '&value=' + req.body.value);
    });
    req.flash('info', 'Saving ' + req.body.key + '. Please wait...');
    map.push(req.body.key, req.body.value);
  } else {
    req.flash('error', 'Error: key required');
    res.redirect('/');
  }
});

app.put('/', function(req, res){
  map.once('get', function(node){
    if (node != undefined) {
      req.flash('info', 'Found value for ' + req.body.key2);
      res.redirect('/?key2=' + node.key + '&value2=' + node.value);
    } else {
      req.flash('info', 'Not found value for ' + req.body.key2);
      res.redirect('/?key2=' + req.body.key2 + '&value2=NULL');
    }
  });
  req.flash('info', 'Searching for ' + req.body.key2 + '. Please wait...');
  map.get(req.body.key2);
});

app.listen(3000);
console.log("Express server listening on port %d in %s mode",
            app.address().port, app.settings.env);
