var feeds = require('./feeds')
var express = require('express')
var path = require('path')
var redis = require("./redis")

var exWrap = function(res, promise) {
	promise.catch(function(err) {
		console.log(err)
		return res.send(500)
	})
}

var server = function(db) {
	var app = express()
	var servDir = path.join(__dirname, '../../../web/build')

	app.use(function(req, res, next) {
		console.log("%s - %s", req.method, req.url)

		return next()
	})

	app.use(express.json())
	app.use(express.compress())

	app.post('/api/feed/add', function(req, res) {
		var url = req.body['url']
		if (!url) {
			return res.send(400, 'Error')
		}

		exWrap(feeds.add(db, url).then(function() {
			res.send(200)
		}))
	})

	app.get('/api/channels', function(req, res) {
		exWrap(res, db.getChannels().then(function(result) {
			var chans = result.map(function(chan) {
				chan.id = chan.channel_id
				delete chan.channel_id

				return chan
			})

			return res.json(200, chans)
		}))
	})

  // TODO: Replace this with DELETE
 //  app.post('/api/feed/:id', function(req, res) {
	// var id = req.params['id']

	// res.send(id)
 //  })

	// TODO: Move this to Redis!
	var feedCache = {}
	app.get('/api/feed/:id', function(req, res) {
		var id = req.params.id

		var modSince = req.get("If-Modified-Since")

		redis.getFeedLatest(id).then(function(cached) {

			if(cached) {
				if(modSince >= cached) {
					return res.send(304)
				}
			}
		})

		exWrap(res, db.getFeed(id).then(function(feeds) {
			// todo: Only send feeds which are pubed >= If-Modified-Since
			var last

			feeds.map(function(feed) {
				feed.id = feed.item_id

				if(!last || feed.published > last) {
					last = feed.published
				}

				delete feed.item_id
				delete feed.hash

				return feed
			})

			if(!last) {
				last = modSince
			}

			res.set({
			 	"Last-Modified": last,
			 	"Cache-Control": "public, max-age=300"
			})

			redis.setFeedLatest(id, last)
			res.send(200, feeds)
		}))
	})

	app.get('/api/feed/unread/:id', function(req, res) {
		var id = req.params.id
	})

	app.use(function(req, res, next) {
		return res.send(404)
	})

	var port = 3000
	app.listen(port)

	console.log("listening on port " + port)

	return app
}

module.exports = {
	server: server
}

@@ -1237,16 +1237,56 @@
 arams.id
+%0A%09%09if(!id) %7B%0A%09%09%09return res.send(400)%0A%09%09%7D
 %0A%0A%09%09var 

Check that id exists in the request