const mockApis = require('./configurations/mock.json').sdcConfig;
const proxy = require('http-proxy-middleware');
const devPort = 9000;

const fePort = 8181;
const feHost = "localhost";
const protocol="http";
const isDirectToFE = true;

const portalCookieValue = "randomValue"; //for dev solely, in production - the webseal would add the cookie by itself.

module.exports = function (env) {

    // Set default user role
    if (!env) {
        env = {
            role: "designer"
        };
    }
    console.log("Starting dev server with role: " + env.role);

    const serverConfig = {
        port: devPort,
        historyApiFallback: true,
        inline: true,
        stats: {
            colors: true,
            exclude: ['node_modules']
        },
        setup: server => {
            let userType = mockApis.userTypes[env.role];

            let middlewares = [
                (req, res, next) => {
                    res.cookie(mockApis.cookie.userIdSuffix, req.headers[mockApis.cookie.userIdSuffix] || userType.userId);
                    res.cookie(mockApis.cookie.userEmail, req.headers[mockApis.cookie.userEmail] || userType.email);
                    res.cookie(mockApis.cookie.userFirstName, req.headers[mockApis.cookie.userFirstName] || userType.firstName);
                    res.cookie(mockApis.cookie.userLastName, req.headers[mockApis.cookie.userLastName] || userType.lastName);
                    res.cookie(mockApis.cookie.portalCookie, portalCookieValue);
                    next();
                }
            ];

            // Redirect all '/sdc1/feProxy/rest' to feHost
            let feProxyOptions = {
                target: protocol + '://' + feHost + ':' + fePort,
                changeOrigin: true,
                secure: false,
                logLevel: 'debug'
            }    
            if (!isDirectToFE) {
              feProxyOptions.pathRewrite= {
                '^/sdc1/feProxy/rest' : '/sdc2/rest'
              }
            }
            middlewares.push(
                proxy(['/sdc1/feProxy/rest'], feProxyOptions));

            // Redirect all '/sdc1/feProxy/uicache' to feHost
            middlewares.push(
              proxy(['/sdc1/feProxy/uicache'], {
                target: protocol + '://' + feHost + ':' + fePort,
                changeOrigin: true,
                secure: false
              }));

            // Redirect all '/sdc1/rest' to feHost
            middlewares.push(
                proxy(['/sdc1/rest'],{
                    target: protocol + '://' + feHost + ':' + fePort,
                    changeOrigin: true,
                    secure: false
                }));

            // Redirect dcae urls to feHost
            middlewares.push(
                proxy(['/dcae','/sdc1/feProxy/dcae-api'], {
                    target: protocol + '://' + feHost + ':' + fePort,
                    changeOrigin: true,
                    secure: false,
                    onProxyRes: (proxyRes, req, res) => {
                        let setCookie = proxyRes.headers['set-cookie'];
                        if (setCookie) {
                            setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, '');
                        }
                    }
                }));

            // Redirect onboarding urls to feHost
            middlewares.push(
                proxy(['/onboarding', '/sdc1/feProxy/onboarding-api'], {
                    target: protocol + '://' + feHost + ':' + fePort,
                    changeOrigin: true,
                    secure: false,
                    onProxyRes: (proxyRes, req, res) => {
                        let setCookie = proxyRes.headers['set-cookie'];
                        if (setCookie) {
                            setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, '');
                        }
                    }
                }));

            // Redirect workflow urls to feHost
            middlewares.push(
                proxy(['/wf', '/sdc1/feProxy/wf'], {
                    target: protocol + '://' + feHost + ':' + fePort,
                    changeOrigin: true,
                    logLevel: 'debug',
                    secure: false,
                    onProxyRes: (proxyRes, req, res) => {
                        let setCookie = proxyRes.headers['set-cookie'];
                        if (setCookie) {
                            setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, '');
                        }
                    }
                }));
            server.use(middlewares);
        }
    };

    return serverConfig;
};