/**
 * Created by liz on 2015/3/28.
 */
var co = require('co');
var bizCore = require('@gopalroy/biz-core');
var express = require('express');
var ApiHelper = require('./ApiHelper');
var debug = require('debug')('@gopalroy/test-drive');

var drivingReportAPI = (function(){
    function DrivingReportAPI(){
        var router = new express();
        router.get('/', this.get);
        router.post('/', this.update);
        router.delete('/',this.delete);
        router.get('/track',this.getTracks);
        this.router = router;
    }

    DrivingReportAPI.prototype.get = function(req,res){
        res.json(req.incar.drivingReport);
    }

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


    DrivingReportAPI.prototype.resolveDrivingReport = function(req, res, next){
        co(function*(){
            req.incar.drivingReport = yield req.incar.testDrive.getTestDrivingReportByIdG(req.params.reportId);
            next();
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    };

    //修改报告
    DrivingReportAPI.prototype.update = function(req,res){
        co(function*(){
            var data = req.body;
            var reportNew = req.incar.drivingReport;
            for(var k in data){{
                if(data.hasOwnProperty(k))
                    reportNew[k] = data[k];
                }
            }
            yield reportNew.updateG();
            res.json({status:'ok',reportNew:reportNew});
        }).catch(function(ex){
            res.status(500).send(ex.stack);
        });
    }

    //删除
    DrivingReportAPI.prototype.delete = function(req,res){
        co(function*(){
            yield req.incar.testDrive.removeTestDrivingReportG(req.incar.drivingReport);
            res.json({status:'ok'});
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    }

    //获取报告对应的的轨迹记录
    DrivingReportAPI.prototype.getTracks = function(req,res){
        co(function*(){
            var tracks = yield req.incar.drivingReport.getTracksG();
            res.json({status:'ok',tracks:tracks});
        }).catch(function(ex){
            debug(ex.stack);
            res.status(500).send(ex.stack);
        });
    }

    return DrivingReportAPI;
})();

module.exports = drivingReportAPI;
