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

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

function keep_in_mind_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].keep_in_mind[req.params.keep_in_mind] === 'undefined')
	{
		console.log("-> keep_in_mind undefined");
		return null;
	}
	return req.session.cache.days[req.params.day].keep_in_mind[req.params.keep_in_mind];
}

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

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

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

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

	console.log(req.body);

	var keep_in_mind = keep_in_mind_from_session(req);
	if(keep_in_mind_from_session_is_invalid(req, res, keep_in_mind))
		return;

	keep_in_mind.name = req.body.edit_name;
	keep_in_mind.description = req.body.area_description;

	console.log(keep_in_mind);

	var data = {
		"request"     : "commit_keep_in_mind",
		"sessionuuid" : req.session.sessionuuid,
		"date"		  : req.params.day,
		"keep_in_mind"		  : req.params.keep_in_mind,
		"keep_in_mind_data"   : keep_in_mind
	};

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

				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_keep_in_mind_edit(req, res) {
	var the_keep_in_mind = req.session.cache.days[req.params.day].keep_in_mind[req.params.keep_in_mind];

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

	console.log('render_keep_in_mind_edit');
}

router.get('/:day/:keep_in_mind/edit', function(req, res, next) {
	if(typeof req.session.cache.days[req.params.day] !== 'undefined') {
		if(typeof req.session.cache.days[req.params.day].keep_in_mind[req.params.keep_in_mind] !== 'undefined') {
			render_keep_in_mind_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.keep_in_mind === "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_keep_in_mind = pcalutils.create_empty_keep_in_mind(uuid);
					req.session.cache.days[req.params.day].keep_in_mind[uuid] = new_keep_in_mind;

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

					res.redirect('/keep_in_mind/'+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"		: "keep_in_mind_details",
			"sessionuuid"   : req.session.sessionuuid,
			"date"			: req.params.day,
			"keep_in_mind"			: req.params.keep_in_mind,
		};

		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].keep_in_mind[req.params.keep_in_mind] = result.keep_in_mind;

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

module.exports = router;
