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

// sampleData
router.get('/sampleData', function (req, res) {
    var db = req.db;
    db.collection('sampleData').find().toArray(function (err, items) {
        res.json(items);
    });
});
// geofences
router.get('/geofences', function (req, res) {
    var db = req.db;
    db.collection('geofences').find().toArray(function (err, items) {
        res.json(items);
    });
});
router.get('/removegeofences', function (req, res) {
    var db = req.db;
    db.collection('geofences').remove({}, function (err, items) {
        res.json(items);
    });
});
router.post('/addgeofences', function (req, res) {
    var db = req.db;
    db.collection('geofences').insert(req.body, function (err, result) {
        res.send(
            (err === null) ? { msg: '' } : { msg: err }
        );
    });
});
// centroids
router.get('/centroids', function (req, res) {
    var db = req.db;
    db.collection('centroids').find().toArray(function (err, items) {
        res.json(items);
    });
});
router.get('/removecentroids', function (req, res) {
    var db = req.db;
    db.collection('centroids').remove({}, function (err, items) {
        res.json(items);
    });
});
router.post('/addcentroids', function (req, res) {
    var db = req.db;
    db.collection('centroids').insert(req.body, function (err, result) {
        res.send(
            (err === null) ? { msg: '' } : { msg: err }
        );
    });
});
// clusters
router.get('/clusters', function (req, res) {
    var db = req.db;
    db.collection('clusters').find().toArray(function (err, items) {
        res.json(items);
    });
});
router.get('/removeclusters', function (req, res) {
    var db = req.db;
    db.collection('clusters').remove({}, function (err, items) {
        res.json(items);
    });
});
router.post('/addclusters', function (req, res) {
    var db = req.db;
    db.collection('clusters').insert(req.body, function (err, result) {
        res.send(
            (err === null) ? { msg: '' } : { msg: err }
        );
    });
});
// silhouettes
router.get('/silhouettes', function (req, res) {
    var db = req.db;
    db.collection('silhouettes').find().toArray(function (err, items) {
        res.json(items);
    });
});
router.get('/removesilhouettes', function (req, res) {
    var db = req.db;
    db.collection('silhouettes').remove({}, function (err, items) {
        res.json(items);
    });
});
router.post('/addsilhouettes', function (req, res) {
    var db = req.db;
    db.collection('silhouettes').insert(req.body, function (err, result) {
        res.send(
            (err === null) ? { msg: '' } : { msg: err }
        );
    });
});
// predictions
router.get('/predictions', function (req, res) {
    var db = req.db;
    db.collection('predictions').find().toArray(function (err, items) {
        res.json(items);
    });
});
router.get('/removepredictions', function (req, res) {
    var db = req.db;
    db.collection('predictions').remove({}, function (err, items) {
        res.json(items);
    });
});
router.post('/addpredictions', function (req, res) {
    var db = req.db;
    db.collection('predictions').insert(req.body, function (err, result) {
        res.send(
            (err === null) ? { msg: '' } : { msg: err }
        );
    });
});
module.exports = router;
