/**
 * Created by liz on 2015/7/10.
 */
var express = require('express');
var debug = require('debug')('@gopalroy/test-drive:TestDriving');
var co = require('co');
//试驾API
var testDrivingAPI =(function(){
     function TestDrivingAPI(){
         var router = new express();
         router.get('/', this.get);
         router.put('/', this.update);
         router.delete('/', this.delete);
         router.get('/reportinfo',this.getReportInfo);
         router.get('/alarminfo',this.getAlarmInfo);
         router.get('/track',this.getTracks);
         router.get('/report',this.report);
         this.router = router;
     }

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

    TestDrivingAPI.prototype.resolveTestDriving = function(req, res, next){
        co(function*(){
            req.incar.testDriving = yield req.incar.testDrive.getTestDrivingByIdG(req.params.tDrivingId);
            next();
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    };

    TestDrivingAPI.prototype.get = function(req,res){
        res.json(req.incar.testDriving);
    }

    TestDrivingAPI.prototype.update = function(req,res){
        co(function*(){
            var data = req.body;
            var testDriving = req.incar.testDriving;

            for(var k in data){
                if(data.hasOwnProperty(k)){
                    testDriving[k] = data[k]
                }
            }
            yield testDriving.updateG();
            res.json({status:'ok',testDriving:testDriving});
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    }

    TestDrivingAPI.prototype.delete = function(req,res){
        co(function*(){
            yield req.incar.testDrive.removeTestDrivingG(req.incar.testDriving);
            res.json({status:'ok'});
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    }

    //获取一条试驾记录需要统计的行程信息
    TestDrivingAPI.prototype.getReportInfo = function(req,res){
        co(function*(){
            var trips = yield req.incar.testDriving.getReportInfoG();
            res.json({status:'ok',trips:trips});
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    }

    //获取一条试驾记录产生的报警信息
    TestDrivingAPI.prototype.getAlarmInfo = function(req,res){
        co(function*(){
            var alarms = yield req.incar.testDriving.getAlarmInfoG();
            res.json({status:'ok',alarms:alarms});
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    }

    //获取一次试驾的轨迹记录
    TestDrivingAPI.prototype.getTracks = function(req,res){
        co(function*(){
            var tracks = yield req.incar.testDriving.getTracksG();
            res.json({status:'ok',tracks:tracks});
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    }

    //获取报告信息
    TestDrivingAPI.prototype.report = function(req,res){
        co(function*(){
            var report = yield req.incar.testDriving.getReportG();
            res.json({status:'ok',report:report});
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    }

    return TestDrivingAPI;
})();
module.exports = testDrivingAPI;
