var express = require('express');
var router = express.Router();

var jsonsrv = require('../lib/jsonsrv');
var pcalutils = require('../lib/pcalutils');

function away_from_session(req)
{
 	console.log(req.params);
	console.log(req.session.cache.days);
	console.log(req.session.cache.days[req.params.day]);
	if(typeof req.session.cache.days[req.params.day] === 'undefined')
	{
		console.log("-> day undefined");
		return null;
	}
	if(typeof req.session.cache.days[req.params.day].away[req.params.away] === 'undefined')
	{
		console.log("-> away undefined");
		return null;
	}
	return req.session.cache.days[req.params.day].away[req.params.away];
}

function away_from_session_is_invalid(req, res, aw)
{
	if(aw === null)
	{
		console.log('AWAY UNDEFINED!');
		console.log(req.params);
		res.redirect('/day');
		return true;
	}
	return false;
}

router.get('/:day/:away/cancel', function(req, res, next) {
	delete req.session.cache.days[req.params.day].away[req.params.away];

	pcalutils.redirect_after_editing(req, res);
});

router.post('/:day/:away/commit', function(req, res, next) {

	console.log(req.body);

	var away = away_from_session(req);
	if(away_from_session_is_invalid(req, res, away))
		return;

	away.name = req.body.edit_name;
	away.involveddb_id = req.body.involveddb_id;
	away.reason = req.body.edit_reason;

	console.log(away);

	var data = {
		"request"     : "commit_away",
		"sessionuuid" : req.session.sessionuuid,
		"date"		  : req.params.day,
		"away"		  : req.params.away,
		"away_data"   : away
	};

	jsonsrv.do_jsonsrv2(data,
		function(result) {
			if(result.result === "OK") {
				delete req.session.cache.days[req.params.day].away[req.params.away];

				pcalutils.redirect_after_editing(req, res);
			}
			else {
				jsonsrv.handle_common_jsonsrverrors(req, res, result);
			}
		},
		function(error) {
			jsonsrv.handle_jsonconnectionerror(req, res, error);
		});
});

generateUID = function() {
	return ("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4)
}

function render_away_edit(req, res) {
	var the_away = req.session.cache.days[req.params.day].away[req.params.away];

	res.render("event_edit_container", { 	date: req.session.cache.days[req.params.day].date,
											pentrytype: "away",
											pentrytype_pretty: "Abwesenheit",
											pentry: the_away,
											pentry_href : "/away/" + req.params.day + "/" + req.params.away,
											pcalview : (typeof req.query.pcalview !== 'undefined' ? req.query.pcalview : 'day'),
											edit_type: (the_away.uuid.substr(0,4) === "new_" ? "erstellen" : "bearbeiten") });

	console.log('render_away_edit');
}

router.get('/:day/:away/edit', function(req, res, next) {
	if(typeof req.session.cache.days[req.params.day] !== 'undefined') {
		console.log(req.session.cache.days[req.params.day]);
		if(typeof req.session.cache.days[req.params.day].away[req.params.away] !== 'undefined') {
			render_away_edit(req, res);
			return;
		}
	}
	else {
		var day = {
				events: {},
				away  : {},
				keep_in_mind : {},
				todo         : {},
				vehicle_use  : {}
			};

		req.session.cache.days[req.params.day] = day;

		console.log(req.session.cache.days);

		console.log("created new day:");
		console.log(req.session.cache.days[req.params.day]);
	}

	if(req.params.away === "new") {
		var data = {
				"request"		: "get_parishday",
				"sessionuuid"   : req.session.sessionuuid,
				"date"			: req.params.day,
			};

		jsonsrv.do_jsonsrv2(data,
			function(result) {
				if(result.result === "OK")
				{
					req.session.cache.days[req.params.day].date = result.day.date;
					req.session.date = result.day.date;

					var uuid = "new_" + generateUID();
					var new_away = pcalutils.create_empty_away(uuid);
					req.session.cache.days[req.params.day].away[uuid] = new_away;

					var pcalview = "";
					if(req.query.pcalview)
						pcalview = '?pcalview='+req.query.pcalview;

					res.redirect('/away/'+req.params.day+'/'+uuid+'/edit'+pcalview);
				}
				else
					jsonsrv.handle_common_jsonsrverrors(req, res, result);
			},
			function(error) {
				jsonsrv.handle_jsonconnectionerror(req, res, error);
			});
	}
	else
	{
		var data =
		{
			"request"		: "away_details",
			"sessionuuid"   : req.session.sessionuuid,
			"date"			: req.params.day,
			"away"			: req.params.away,
		};

		jsonsrv.do_jsonsrv2(data,
			function(result) {
				if(result.result === "OK")
				{
					req.session.date = result.date;
					req.session.cache.days[req.params.day].date = result.date;

					req.session.cache.days[req.params.day].away[req.params.away] = result.away;

					render_away_edit(req, res);
				}
				else
					jsonsrv.handle_common_jsonsrverrors(req, res, result);
			},
			function(error) {
				jsonsrv.handle_jsonconnectionerror(req, res, error);
			});
	}
});

module.exports = router;
