var formidable = require('formidable');
var util = require('util');
var nconf = require('nconf');
nconf.file({ file: 'config.json' });

var api_key = nconf.get('mailgun:api_key');
var domain = nconf.get('mailgun:domain');
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});


exports.postSkjema = function (req, res){
	var text = "";
	for(var key in req.body){
		var storBokstavKey = key.charAt(0).toUpperCase() + key.slice(1);
		text += storBokstavKey + ': ' + req.body[key] + '\n';
	}

	var data = {
		from: 'Kontaktskjema ['+req.body.namn+']<' + nconf.get('mailgun:noreply') + '>',
		to: nconf.get('mailgun:contact'),
		subject: 'Melding fra: ' + req.body.namn,
		text: text
	};

	console.log(req.body);
	mailgun.messages().send(data, function (error, body) {
		if(error){
			console.log('mail error: ', error)
			res.end(error);
		}else{
			res.end('success');
		}
	});

	var mottatt = "Vi har mottatt din melding og vil komme med tilbakemelding så fort som mulig. \n\n ---- \n\n"
	mailgun.messages().send({
		from: 'Kontaktskjema ['+req.body.namn+']<' + nconf.get('mailgun:noreply') + '>',
		to: req.body.epost,
		subject: 'Melding mottatt',
		text: mottatt + text
	}, function (error, body) {
	});
}

exports.sendMail = function(data, callback){
	mailgun.messages().send(data, function (error, body) {
		
		callback(error, body);
	});
}

exports.testSendMail = function(req, res){

	var data = {
		from: 'Testepost <noreply@hjorundfjordmountainguide.no>',
		to: 'larskarbo@gmail.com',
		subject: 'Melding fra: yolo',
		text: 'Navn: tulleri'
	};

	mailgun.messages().send(data, function (error, body) {
		if(err) throw err

			res.send('success')
	});
}
