const ftlParser = require('../ftl_parse');
const urlMap = require('../mock/index');
const onMockFinish = urlMap.onMockFinish;
const logUtil = require(`../util/logUtil`);
const uploadImg = require('./uploadImg');
const urlsReturn200 = require('../mock/ajax/retCode200');

function initRequestMap(app) {
    //初始化页面入口路由
    urlMap.forEach(function(page) {
        try {
            app.get(page.entry, (req, res, next) => {
                if (req.xhr) {
                    next();
                } else {
                    ftlParser.render(page.ftlPath, (html) => {
                        res.send(html);
                    }, page.ftlData);
                }
            });
        } catch (e) {
            throw `Error initializing page entry:${page.entry}`;
        }
    });
    //初始化ajax路由
    urlMap.forEach((page) => {
        if (!page.ajax) return;
        page.ajax.forEach((opt) => {
            try {
                opt.method = opt.method || 'post';
                opt.method.split(',').forEach(function(m) {
                    app[m.toLowerCase()](opt.url, (req, res, next) => {
                        res.json(opt.result);
                    });
                });
            } catch (e) {
                throw `Error initializing ajax:${opt.url}`;
            }
        });
    });

    urlsReturn200.forEach((url) => {
        try {
            app.use(url, (req, res) => {
                res.json({
                    retCode: 200
                });
            });
        } catch (e) {
            throw `Error initializing retCode200 ajax:${url}`;
        }
    });

    app.use((req, res) => {
        console.info(404);
        console.info(req.originalUrl);
        res.status(404).send('Page not found.');
    });
}

module.exports = function(app) {
    return new Promise((resolve, reject) => {
        try {
            //初始化图片上传路由
            uploadImg(app);
            //初始化页面入口路由
            urlMap.forEach(function(page) {
                app.get(page.entry, function(req, res, next) {
                    if (req.xhr) {
                        next();
                    } else {
                        ftlParser.render(page.ftlPath, function(html) {
                            res.send(html);
                        }, page.ftlData);
                    }
                });
            });

            onMockFinish(() => {
                try {
                    initRequestMap(app);
                    resolve();
                } catch (e) {
                    reject(e);
                }
            });
        } catch(e) {
            reject(e);
        }
    });
};

@@ -1,12 +1,13 @@
+%EF%BB%BF
 const ftlPar
@@ -1902,451 +1902,8 @@
 pp);
-%0A            //%E5%88%9D%E5%A7%8B%E5%8C%96%E9%A1%B5%E9%9D%A2%E5%85%A5%E5%8F%A3%E8%B7%AF%E7%94%B1%0A            urlMap.forEach(function(page) %7B%0A                app.get(page.entry, function(req, res, next) %7B%0A                    if (req.xhr) %7B%0A                        next();%0A                    %7D else %7B%0A                        ftlParser.render(page.ftlPath, function(html) %7B%0A                            res.send(html);%0A                        %7D, page.ftlData);%0A                    %7D%0A                %7D);%0A            %7D);
 %0A%0A  

delete duplicated code