var querystring = require('querystring');
var url = require('url');
const fs = require('fs')
var https = require('https');
var request = require('request');
var ejs = require('ejs');
function myPlugin(rawOptions) {
    let projectName = process.env.VITE_APP_PN.split("/")[0];
    var previewPath = `${projectName}/setting/studio/preview.json`
    var setting
    try {
        let settingContent = fs.readFileSync(previewPath, { encoding: 'utf8' });
        setting = JSON.parse(settingContent)
    } catch (e) {
        console.log("preview数据有错！")
    }
    var VITE_APP_BASE_API, VITE_APP_SERVER;
    if (setting) {
        VITE_APP_BASE_API = (setting.server || {}).VITE_APP_BASE_API || process.env.VITE_APP_BASE_API;
        VITE_APP_SERVER = (setting.server || {}).VITE_APP_SERVER || process.env.VITE_APP_SERVER;
    } else {
        VITE_APP_BASE_API = process.env.VITE_APP_BASE_API;
        VITE_APP_SERVER = process.env.VITE_APP_SERVER;
    }
    const options = {
        isProduction: process.env.NODE_ENV === 'production',
        ...rawOptions,
        VITE_APP_BASE_API: VITE_APP_BASE_API,
        VITE_APP_SERVER: VITE_APP_SERVER,
        root: process.cwd(),
    };
    let postData; let config;
    return {
        name: 'vite-plugin-func', // 必须的，将会显示在 warning 和 error 中 
        configResolved(resolvedConfig) {
            // 存储最终解析的配置
            config = resolvedConfig
        },
        configureServer(server) {
            server.middlewares.use((req, res, next) => {
                var serverPath = VITE_APP_BASE_API + VITE_APP_SERVER;
                if (req.url.indexOf("/development.js") != -1) {
                    let content;
                    try {
                        content = fs.readFileSync(req.url.substr(1), { encoding: 'utf8' })
                    } catch (e) {
                        res.writeHead(404, {
                            "Content-Type": "application/json;charset=UTF-8"
                        });
                        res.end("获取", req.url + "文件异常!");
                        return;
                    }
                    try {
                        var html = ejs.render(content, options);
                    } catch (e) {
                        res.end(e.message);
                        return;
                    }
                    res.end(html);
                    return;
                }

                let pathname = req._parsedUrl.pathname;
                //console.log(new Date(), 'req.url:', req.url)
                if (pathname.endsWith(".flow") || pathname.endsWith(".func")) {
                    // let url = req.url;
                    let post = '';
                    postData = '';
                    let path;
                    let fmodelpath = pathname;// url.split("?")[0];
                    var post_data = {};
                    // var param = req.url.split("?")[1];
                    var param = req._parsedUrl.query;
                    var result = {};
                    if (param) {
                        let values = param.split("&");
                        for (let i = 0; i < values.length; i++) {
                            const element = values[i];
                            let _val = element.split("=");
                            result[_val[0]] = _val[1];
                        }
                    }
                    if (fmodelpath.endsWith(".flow")) {
                        var fversion = "";
                        var ftaskguid = "";
                        if (param) {
                            fversion = result["_version"] || result["version"];
                            ftaskguid = result["_ftaskguid"] || result["ftaskguid"];
                        }
                        post_data = { fmodelpath: fmodelpath, fversion, ftaskguid }
                        path = serverPath + "/flow/open?pn=";
                    } else {
                        var viewItemId = ""
                        if (param) {
                            viewItemId = result["__viewItemId"] || result["viewItemId"] || "";
                        }
                        path = serverPath + fmodelpath + "?skip=false&viewItemId=" + viewItemId + "&pn="
                    }
                    // let pn = url.split("/")[1];
                    const TokenKey = 'EAP-Token';
                    function handleFunc() {
                        var Cookies = {};
                        if (req.headers.cookie != null) {
                            req.headers.cookie.split(';').forEach(l => {
                                var parts = l.split('=');
                                Cookies[parts[0].trim()] = (parts[1] || '').trim();
                            });
                        }
                        let token = Cookies[TokenKey];
                        let requestUrl = path + process.env.VITE_APP_PN;
                        requestUrl = requestUrl + "&" + param;
                        console.log("----requestUrl:", requestUrl, "token:", token);
                        Object.assign(post_data, postData);
                        request({
                            url: requestUrl,
                            method: "POST",
                            json: true,
                            body: post_data,
                            headers: {
                                "token": token,
                                "Authorization": "Bearer " + token,
                                "Content-Type": "application/json;charset=UTF-8"
                            }
                        }, function (error, response, body) {
                            //console.log(error, response, body);
                            if (!error && response.statusCode == 200) {
                                let pcform;
                                let designPath;
                                let isGuest = false;
                                if (body) {
                                    if (fmodelpath.endsWith(".flow") && body.outParameter) {
                                        pcform = body.outParameter.task.bizpcform;
                                    } else if (fmodelpath.endsWith(".func")) {
                                        if (body && body.logicflow) {  //逻辑流
                                            let statusCode = body.status || response.statusCode;
                                            res.writeHead(statusCode, {
                                                "Content-Type": "application/json;charset=UTF-8"
                                            });
                                            res.end(JSON.stringify(body));
                                            return;
                                        } else if (body.dataPack) {
                                            pcform = body.dataPack.path;
                                            designPath = body.dataPack.designPath;
                                            isGuest = body.dataPack.isGuest;
                                        }
                                    }
                                }
                                if (!pcform) {
                                    res.writeHead(500, {
                                        "serverInfo": `${VITE_APP_BASE_API},${VITE_APP_SERVER}`,
                                        "Content-Type": "application/json;charset=UTF-8"
                                    });
                                    res.end(JSON.stringify(body));
                                    return;
                                }
                                if (designPath && (designPath.endsWith(".vm") || designPath.endsWith(".dvm"))) {
                                    res.writeHead(200, {
                                        'Content-Type': 'html'
                                    });
                                    let projectName = process.env.VITE_APP_PN.split("/")[0];
                                    var pn = `pn=${process.env.VITE_APP_PN}`
                                    if (req.url.indexOf("?") == -1) {
                                        pn = "?" + pn;
                                    } else {
                                        pn = "&" + pn;
                                    }
                                    let accessToken = ""
                                    if (token) {
                                        accessToken = `&access_token=${token}`;
                                    }
                                    let html = `
                            <script src="/assets_platform/eap/eap.umd.min.js"></script>
                            <script>
                                window._global ={
                                    env :"dev"
                                }
                            </script>
                            <script src="/${projectName}/setting/desktop/development.js" type="text/javascript"></script>
                            <script>
                                window.location.href="${serverPath}${req.url}${pn}${accessToken}";
                            </script>
                            `
                                    res.end(html);
                                    return;
                                }
                                // var fullPath = "eaptpl/12112/mokuai/shitumulu/biaodan/dtv/1.0.0/desktop/index.html";
                                let content;
                                try {
                                    content = fs.readFileSync(pcform, { encoding: 'utf8' })
                                } catch (e) {
                                    res.writeHead(404, {
                                        "Content-Type": "application/json;charset=UTF-8"
                                    });
                                    res.end("pcform:" + pcform + "文件异常!");
                                    return;
                                }
                                const statusCode = 200;
                                if (!body)
                                    throw new Error(`No body text found for the ${statusCode} status code`);
                                try {
                                    var html = ejs.render(content, options);
                                } catch (e) {
                                    res.end(e.message);
                                    return;
                                }

                                let postDataStr = JSON.stringify(postData || {});
                                let varName = options.varName || 'viteRequestData';
                                let varGlobal = JSON.stringify(options.global || {});
                                let funcName = new Date().valueOf();
                                let queryScript = `
            function _viteGetQuery${funcName}() {
                var url = window.location.search;
                var theRequest = new Object();
                if (url.indexOf("?") != -1) {
                  var str = url.substr(1);
                  strs = str.split("&");
                  for(var i = 0; i < strs.length; i ++) {
                    theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);
                  }
                }
                return theRequest;
              }`
                                let titleStr = html.match(/<head(.*?)>/g)[0] || "";
                                html = html.replace(
                                    /<head(.*?)>/g,
                                    `${titleStr}<script>${queryScript};window.isGuest=${isGuest};window.${varName} = Object.assign(_viteGetQuery${funcName}(),${postDataStr}); window._global=${varGlobal}</script>`
                                )
                                res.writeHead(statusCode, {
                                    "serverInfo": `${VITE_APP_BASE_API},${VITE_APP_SERVER}`,
                                    // 'Content-Length': html.length,
                                    'Content-Type': 'html'
                                });
                                res.write(html)
                                res.end();
                            } else {

                                if (response && response.statusCode == 401) {
                                    res.writeHead(200, {
                                        "serverInfo": `${VITE_APP_BASE_API},${VITE_APP_SERVER}`,
                                        'Content-Type': 'html'
                                    });
                                    let projectName = process.env.VITE_APP_PN.split("/")[0];
                                    let html = `
                            <script src="/assets_platform/eap/eap.umd.min.js"></script>
                            <script>
                                window._global ={
                                    env :"dev"
                                }
                            </script>
                            <script src="/${projectName}/setting/desktop/development.js" type="text/javascript"></script>
                            <script>
                                if(top.window.SysPage&&top.window.SysPage.openMiniLogin){//小窗口
                                    top.window.SysPage.openMiniLogin(true);
                                }else{
                                    window.location.href=window.HIVUI_SETTING.loginUrl;
                                }
                            </script>
                            `
                                    res.end(html);
                                } else {
                                    res.writeHead(500, {
                                        "serverInfo": `${VITE_APP_BASE_API},${VITE_APP_SERVER}`,
                                        "Content-Type": "application/json;charset=UTF-8"
                                    });
                                    res.end(JSON.stringify(body));
                                }
                                //res.end(requestUrl + ":" + JSON.stringify(error) + JSON.stringify(body));
                            }
                        }, (msg) => {
                            res.writeHead(404, {
                                "Content-Type": "application/json;charset=UTF-8"
                            });
                            res.end(JSON.stringify(msg));
                            // console.log(msg)
                        });
                    }
                    req.on('data', (chunk) => {
                        post += chunk;
                        postData = querystring.parse(post);
                    });
                    req.on('end', (chunk) => {
                        handleFunc()
                    });
                    return;
                } else {
                    next();
                }
            })
        },
    }
}
export default myPlugin;