var express = require('express');
var co = require('co');
var debug = require('debug')('@gopalroy/test-dirve');
var bizFleet = require('@gopalroy/biz-fleet');

var FenceAPI = (function(){
    function FenceAPI()
    {
        var router = new express();
        router.get('/', this.get);
        router.post('/', this.save);
        this.router = router;
    }

    FenceAPI.prototype.getRouter = function(){
        return this.router;
    };

    FenceAPI.prototype.get = function(req, res){
        co(function*(){
            var fence = new bizFleet.Fence(req.incar.testDrive.id);
            var result = yield fence.getG();
            res.json(result);
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack).end();
        });
    };

    FenceAPI.prototype.save = function(req, res){
        co(function*(){
            var fence = new bizFleet.Fence(req.incar.testDrive.id);
            yield fence.saveG(req.body);
            res.status(200).end();
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack).end();
        });
    };

    FenceAPI.onTopic = function(req, res){
        co(function*(){
            bizFleet.Fence.onTopic(req.params.topic, req.body);
            res.status(204).end();
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack).end();
        });
    }

    return FenceAPI;
})();

module.exports = FenceAPI;
