'use strict';

var express = require('express'),
    router = express.Router(),
    _ = require('lodash'),
    logger = require('../logger'),
    transformerProxy = require('transformer-proxy'),
    mock = require('../service/mock'),
    proxy = require('../proxy'),
    utils = require('../utils'),
    Mock = require('mockjs');

router.use((req, res, next) => {
    logger.info('%s %s %s', req.method, req.url, req.path);
    next();
});

/**
 * handle all api not match system apis
 * @method handleApi
 * @param  {[Request]}   req  [description]
 * @param  {[Response]}   res  [description]
 * @param  {Function} next [description]
 * @return {[type]}        [description]
 */
router.use(async(req, res, next) => {
    let token = req.headers.mocktoken;
    let type = req.headers.mocktype;
    if (!type) { // not mock request
        next();
    } else if (type && !token) { // mock request, but without access-token
        return res.json({
            result: '缺少token'
        });
    } else {
        delete req.headers.token;
        delete req.headers.type;

        let url = decodeURI(req.url);
        let path = decodeURI(req.path);
        let prefix = (url.match(/\/\w+/) || [""])[0];
        let author = req.headers.mockauthor;
        if (author) {
            prefix = author;
            delete req.headers.author;
        }
        var project = await mock.getProjects(prefix, token);
        if (project) {
            var normalApis = await mock.getNormalApis(project._id, token) || {};
            var regApis = await mock.getRegApis(project._id, token);
            var api = normalApis[path];
            if (api && api.type == req.method) { // match normal
                if (api.dataHandler == "over") {
                    let data = JSON.parse(api.result);
                    return res.json(Mock.mock(data));
                } else {
                    req.proxy = project.proxy;
                    req._extendData = api.result;
                    next();
                }
            } else if (regApis) { // match reg
                regApis.forEach((api) => {
                    if (new RegExp(api.regexp).test(url) && api.type == req.method) {
                        if (api.dataHandler == "over") {
                            let data = JSON.parse(api.result);
                            return res.json(Mock.mock(data));
                        } else {
                            req.proxy = project.proxy;
                            req._extendData = api.result;
                            next();
                        }
                    }
                });
                if (url.indexOf(':') != -1) {
                    return res.status(200).json({
                        result: "error happens! have you replace your parameter? "
                    });
                }
                next();
            } else {
                next();
            }
        } else {
            next();
        }
    }
});

// override data
router.use(transformerProxy((data, req, res) => {
    if (req._extendData) {
        var ret = JSON.parse(data);
        var extend = JSON.parse(req._extendData);
        ret = _.merge(ret, Mock.mock(extend));

        return JSON.stringify(ret);
    }

    return data;
}));

router.use((req, res, next) => {
    if (req.proxy) {
        proxy.web(req, res, {
            target: req.proxy,
            toProxy: true,
            changeOrigin: true
        });
    } else {
        next();
    }
});

var deps = [];
var ret = [{
    router: router,
    route: '/'
}];
deps.forEach((dep) => {
    ret.push(require(dep));
});

module.exports = ret;

@@ -3067,24 +3067,42 @@
 tendData) %7B%0A
+        try %7B%0A    
         var 
@@ -3129,24 +3129,28 @@
 a);%0A        
+  
+  
 var extend =
@@ -3187,16 +3187,20 @@
         
+    
 ret = _.
@@ -3231,16 +3231,20 @@
 end));%0A%0A
+    
         
@@ -3267,24 +3267,154 @@
 ngify(ret);%0A
+        %7D catch (e) %7B%0A            return JSON.stringify(%7B%0A                result: '%E8%A7%A3%E6%9E%90%E5%87%BA%E9%94%99%EF%BC%8C%E6%97%A0%E6%B3%95%E5%90%88%E5%B9%B6%E4%B8%BAjson%E6%A0%BC%E5%BC%8F%E6%95%B0%E6%8D%AE'%0A            %7D);%0A        %7D%0A%0A
     %7D%0A%0A    r

fix bug: data overlying