var express = require('express');
var router = express.Router();
var Records = require("../models/record-model");
var Bands = require("../models/band-model");
var Genres = require("../models/genre-model");
var auth = require('../middlewares/auth');

function saveImage(id, base64Data) {
	var base64Data = base64Data.replace(/^data:image\/png;base64,/, "")
								.replace(/^data:image\/jpeg;base64,/, "");
	var filename = "public/images/covers/" +id + ".jpg";
	require("fs").writeFile(filename, base64Data, 'base64', function(err) {
	});
	return filename.replace('public', '');
};

function getBandsNGenres(req, res) {
	var genresPromise = new Promise(function (resolve, reject) {
		Genres.find({
			user: req.session.user._id
		}, function (err, genres) {
			if (err) reject(err);
			resolve(genres);
		});
	});

	var bandsPromise = new Promise(function (resolve, reject) {
		Bands.find({
			user: req.session.user._id
		}, function (err, bands) {
			if (err) reject(err);
			resolve(bands);
		});
	});

	return Promise.all([bandsPromise, genresPromise]);
}

router.get('/', auth, function(req, res) {

	var recordsPromise = new Promise(function (resolve, reject) {
		Records.find({
			user: req.session.user._id
		}, function (err, bands) {
			if (err) reject(err);
			resolve(bands);
		}).populate('band');
	});

	Promise.all([getBandsNGenres(req, res), recordsPromise]).then(function (values) {
		res.locals.rbands = values[0][0];
		res.locals.rgenres = values[0][1];
		res.locals.records = values[1];
		res.render('records/index');
	}).catch(function (err) {
		res.locals.error = [err.message];
		res.render('records/index');
	});;
});

router.get('/add', auth, function(req, res) {
	getBandsNGenres(req, res).then(function (values) {
		res.locals.rbands = values[0];
		res.locals.rgenres = values[1];
		res.render('records/new');
	}).catch(function (err) {
		res.locals.error = [err.message];
		res.render('records/index');
	});
});

router.post('/add', auth, function (req, res) {
	var record = new Records({
		name: req.body['record.name'],
		band: req.body['record.band'],
		year: req.body['record.year'],
		label: req.body['record.label'],
		genre: req.body['record.genre'],
		number: req.body['record.number'],
		user: req.session.user._id
	});

	if (req.body['record.artwork']) {
		record.cover = saveImage(record._id, req.body['record.artwork']);
	}

	record.save(function (err, record) {
		if (err) {
			req.flash("error", err.message);
		} else {
			req.flash("success", req.__("record.added", {name: record.name })); 
		}
		res.redirect('/records');
	});

});

module.exports = router;

@@ -527,16 +527,49 @@
 (err) %7B%0A
+%09%09console.error(%22ERROR: %22, err);%0A
 %09%7D);%0A%09re

allow add image to an album