/**
 * Created by wh on 2015/7/7.
 */
var Express = require('express');
var fs = require('fs');
var Authority = require('./api/Authority');
var TestDriveAPI = require('./api/TestDriveAPI');
var StaffAPI = require('./api/StaffAPI');
//var trackReplay = require('@gopalroy/track-replay');
var fence = require('@gopalroy/fence');
var FenceAPI = require('./api/FenceAPI');
// EXPRESS级联
// 模块的外部使用者能自行决定"根路由"
var app = new Express();

//app.use('/:fleetId/track-replay',Authority.checkToken, Authority.requireStaffOrSystemAdmin,trackReplay);//轨迹回放

app.use('/api/*', Authority.checkToken); // 安全检查

app.get('/api/testDrive', Authority.requireSystemAdmin, TestDriveAPI.getAll); // 查询所有试乘试驾
app.post('/api/testDrive',Authority.requireSystemAdmin, TestDriveAPI.post); // 新增一个试乘试驾
app.get('/api/loginStaff', StaffAPI.getStaffLogin); // 查询当前登录用户

var testDriveAPI = new TestDriveAPI();
app.use('/api/testDrive/:tdriveId',Authority.requireStaffOrSystemAdmin, testDriveAPI.resolveTestDrive, testDriveAPI.getRouter()); // 具体一个试乘试驾


app.post('/apix/:topic', FenceAPI.onTopic); // 响应AliMNS消息


function xUrlMap(req, res){
    fs.readFile(__dirname + '/html/index.html', function(err, data){
        res.type('html');
        res.status(200).send(data);
    });
}

app.get('/:tdriveId([0-9]+)', xUrlMap);
app.get('/:tdriveId([0-9]+)/*', xUrlMap);

app.use('/fence', fence);
//静态资源
app.use('/', Express.static(__dirname + '/html'));

module.exports = app;
