var Monitors = {};

const Monitor = require('../models/monitor');
const renderError = require('../lib/renderError');
const monitorCron = require('../lib/monitorCron');
// const badge = require('../lib/badge');
const moment = require('moment');

Monitors.new = (req, res)=> {
    return res.render('monitor/edit')
};

function addhttp(url) {
    if (!/^(?:f|ht)tps?\:\/\//.test(url)) {
        url = "http://" + url;
    }
    return url;
}

Monitors.newPost = (req, res)=> {
    const name = req.body.name;
    var url = req.body.url;
    const username = req.user.username;
    const email = req.user.emails[0].value;

    url = addhttp(url);

    new Monitor({
        name,
        url,
        username,
        email
    })
        .save()
        .then(monitor=> {
            monitorCron.add(monitor);
            return res.redirect('/site/' + monitor.id);
        })
        .catch(err=> {
            return renderError(err, res)
        });

};

Monitors.mine = (req, res)=> {

    var currentUsername = req.user.username;

    Monitor
        .filter({username: currentUsername})
        .run()
        .then(monitors=> {
            res.render('monitor/mine', {monitors});
        })
        .catch(err => renderError(err, res));
};

Monitors.show = (req, res)=> {
    var id = req.params.id;
    Monitor.get(id)
        .getJoin({responses: true})
        .run()
        .then((monitor)=> {


            var graph = monitor.responses.sort(function (a, b) {
                return new Date(a.date) - new Date(b.date);
            })
                .map((val)=> {
                    return [val.date, val.time];
                });


            //SORT BY DATE
            // monitor.responses = monitor.responses.sort(function (a, b) {
            //     return new Date(b.date) - new Date(a.date);
            // }).slice(0, 50).reverse();
            //
            // var graph = {
            //     labels: [],
            //     data: []
            // };
            // monitor.responses.map(function (r) {
            //     graph.data.push({
            //         meta: moment(r.date).fromNow(), //19 minutes ago (exmaple)
            //         value: r.time //ms
            //     });
            //     // graph.labels.push(r.time)
            // });

            return res.render('monitor/show', {monitor, graph});
        })
        .catch((err)=> {
            console.log('error:', err);
            return renderError(err, res);
        })
};


Monitors.edit = (req, res)=> {

};

Monitors.delete = (req, res)=> {
    var id = req.params.id;
    Monitor.get(id).delete()
        .then(()=> {
            return res.redirect('/');
        })
        .catch((err)=> {
            return renderError(err);
        });

};

Monitors.upPercentBadge = (req, res)=> {
    var id = req.params.id;

    Monitor.get(id).run()
        .then((monitor)=> {
            monitor.getUpPercentBadge().then((svg)=> {
                return res.type('image/svg+xml').send(svg);
            }).catch(err=> {
                console.log(err)
            });
        })
        .catch((err)=> {
            console.log(err)
        })
};
Monitors.averageResponseBadge = (req, res)=> {
    var id = req.params.id;

    Monitor.get(id).run()
        .then((monitor)=> {
            monitor.getAvgResponseBadge().then((svg)=> {
                return res.type('image/svg+xml').send(svg);
            }).catch(err=> {
                console.log(err)
            });
        })
        .catch((err)=> {
            console.log(err)
        })
};

Monitors.statusBadge = (req, res)=> {
    var id = req.params.id;

    Monitor.get(id).run()
        .then((monitor)=> {
            monitor.getStatusBadge().then((svg)=> {
                return res.type('image/svg+xml').send(svg);
            }).catch(err=> {
                console.log(err)
            });
        })
        .catch((err)=> {
            console.log(err)
        })
};

module.exports = Monitors;

@@ -2594,26 +2594,99 @@
 (id)
-.delete()%0A
+%0A        .then((monitor)=%3E %7B%0A            monitor.deleteAll(%7Bresponses: true%7D)%0A     
         
 .the
@@ -2685,21 +2685,41 @@
     
+   
 .then(
-()=%3E %7B%0A
+function (result) %7B%0A    
     
@@ -2718,32 +2718,36 @@
                 
+    
 return res.redir
@@ -2756,16 +2756,108 @@
 t('/');%0A
+                %7D).catch((err)=%3E %7B%0A                return renderError(err);%0A            %7D);%0A
         
@@ -2913,32 +2913,32 @@
 nderError(err);%0A
+
         %7D);%0A%0A%7D;%0A
@@ -2925,33 +2925,32 @@
 r);%0A        %7D);%0A
-%0A
 %7D;%0A%0AMonitors.upP

delete everything