import BodyContext from './bodyContext';
import { isFlow, isInfc, isFunc, isHtml, isStudio, setUrlParam, getContentType, syncRequest, getRountMap } from './tool';
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];
  let userName = process.env.VITE_APP_PN.split("/")[1];
  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,
    VITE_APP_PN: process.env.VITE_APP_PN,
    root: process.cwd(),
  };
  // var routerOption = {
  //   url: `${VITE_APP_BASE_API}/map.html?pn=${process.env.VITE_APP_PN}`,
  //   method: "GET",
  //   json: true,
  //   headers: {
  //     "Content-Type": "application/json;charset=UTF-8"
  //   }
  // }
  const routerMapLocal = ["/main.html", "/index.html", "/login.html", "/studio.html"];
  var routerMapUrl = `${VITE_APP_BASE_API}/map.html?pn=${process.env.VITE_APP_PN}`
  var routerMap = {};
  //  = getRountMap(routerMapUrl);
  const TokenKey = 'EAP-Token';
  let postData;
  let config;
  return {
    name: 'vite-plugin-func', // 必须的，将会显示在 warning 和 error 中
    configResolved(resolvedConfig) {
      // 存储最终解析的配置
      config = resolvedConfig
    },
    configureServer(server) {
      server.middlewares.use(async (req, res, next) => {
        var bodyContext = new BodyContext();
        // 设置响应头部
        res.setHeader('Service-Worker-Allowed', '/');
        var Cookies = {};
        if (req.headers.cookie != null) {
          req.headers.cookie.split(';').forEach(l => {
            var parts = l.split('=');
            Cookies[parts[0].trim()] = (parts[1] || '').trim();
          });
        }
        const token = Cookies[TokenKey];
        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];
          }
        }
        const pathname = (req._parsedUrl.pathname == "/") ? "/index.html" : req._parsedUrl.pathname;//根路径访问定位到index.html;


        // 渲染页面
        function renderHtml(pcform, isGuest, body) {
          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, Object.assign({ bodyContext: bodyContext }, 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;
              }`
          if (bodyContext.getDefaultLayout()) {
            let defaultLayoutOption = Object.assign({
              bodyContext: bodyContext,
              bodyContent: html
            }, options);
            let defaultLayoutContent = fs.readFileSync(bodyContext.getDefaultLayout(), {
              encoding: 'utf8'
            })
            try {
              html = ejs.render(defaultLayoutContent, defaultLayoutOption);
            } catch (e) {
              res.end(e.message);
              return;
            }
          }
          let titleStr = html.match(/<head(.*?)>/g)[0] || "";
          if (!isStudio(pathname)) {
            html = html.replace(
              /<head(.*?)>/g,
              `${titleStr}<script>${queryScript};window.isGuest=${isGuest};
                        window.${varName} = Object.assign(_viteGetQuery${funcName}(),${postDataStr});
                        window._global=${varGlobal}</script>
                        `
            )
            let titleEnd = html.match(/<\/head(.*?)>/g)[0] || "";
            let lang = result["locale"] || Cookies["locale"] || "zh-CN";
            lang = lang.replace("_", "-");
            var sam = "";
            var samDef = "";
            if (pcform.indexOf("hivuiSam/index.html") != -1) {//统计分析
              samDef = `<script src="/${projectName}/hivuiSam/lang/zh-CN.js"></script>`
              sam = `<script src="${VITE_APP_BASE_API}/lang/ref?pn=${process.env.VITE_APP_PN}&vmId=tpl&filePath=${userName}/${projectName}/hivuiSam/lang/def.language&locale=${lang}"></script>`
            };
            var platfPlugin = '';
            var platfPluginDef = '';
            if (pcform.indexOf("hivuiBirt/index.html") != -1) {//平台插件
              platfPluginDef = `<script src="/${projectName}/hivuiBirt/lang/zh-CN.js"></script>`
              platfPlugin = `<script src="${VITE_APP_BASE_API}/lang/ref?pn=${process.env.VITE_APP_PN}&vmId=tpl&filePath=${userName}/${projectName}/hivuiBirt/lang/def.language&locale=${lang}"></script>`
            };
            let funcDefLangMap = {
              'hivuiLogin/index.html': `/${projectName}/hivuiLogin/lang/zh-CN.js`,
              'hivuiMain/index.html': `/${projectName}/hivuiMain/lang/zh-CN.js`,
            }
            let funcLangMap = {
              'hivuiLogin/index.html': `${VITE_APP_BASE_API}/lang/ref?pn=${process.env.VITE_APP_PN}&vmId=tpl&filePath=${userName}/${projectName}/hivuiLogin/lang/def.language&locale=${lang}`,
              'hivuiMain/index.html': `${VITE_APP_BASE_API}/lang/ref?pn=${process.env.VITE_APP_PN}&vmId=tpl&filePath=${userName}/${projectName}/hivuiMain/lang/def.language&locale=${lang}`,
              'platf/permission': `${VITE_APP_BASE_API}/lang/ref?pn=${process.env.VITE_APP_PN}&vmId=scheme&bizPath=${projectName}.pro/setting.project_setrootdir/global.dir/rolescheme.roletp&locale=${lang}`
            }

            let curFuncLangScript = ''
            for (let k in funcDefLangMap) {
              if (pathname.indexOf(k) > -1 || (routerMap[pathname] && routerMap[pathname].url && routerMap[pathname].url.indexOf(k) > -1)) {
                curFuncLangScript += `<script src="${funcDefLangMap[k]}"></script>`
                break;
              }
            }
            for (let k in funcLangMap) {
              if (pathname.indexOf(k) > -1 || (routerMap[pathname] && routerMap[pathname].url && routerMap[pathname].url.indexOf(k) > -1)) {
                curFuncLangScript += `<script src="${funcLangMap[k]}"></script>`
                break;
              }
            }
            var viteData = html.match(/<vite>([\s\S]*)<\/vite>/);
            var viteTpl = "", viteprojectTpl = "";
            if (viteData) {
              //流引入多语言
              var viteDataJson = JSON.parse(html.match(/<vite>([\s\S]*)<\/vite>/)[1]);
              viteprojectTpl = `<script src="${VITE_APP_BASE_API}/lang/static?pn=${process.env.VITE_APP_PN}&vmId=${viteDataJson.project_vmid}&locale=${lang}"></script>`
              if (viteDataJson.module_vmid && viteDataJson.bizpath)
                viteTpl = `<script src="${VITE_APP_BASE_API}/lang/ref?pn=${process.env.VITE_APP_PN}&vmId=${viteDataJson.module_vmid}&bizPath=${viteDataJson.bizpath}&locale=${lang}"></script>`
            }
            html = html.replace(
              /<\/head(.*?)>/g,
              `<script src="/assets_platform/vue-i18n/vue-i18n.js"></script>
            <script src="/${projectName}/lang/element-ui/${lang}.js"></script>
            <script src="${VITE_APP_BASE_API}/lang/ref?pn=${process.env.VITE_APP_PN}&vmId=hiui&filePath=${userName}/${projectName}/lang/hi-ui/def.language&locale=${lang}"></script>
            ${curFuncLangScript}
            ${viteprojectTpl}
            ${viteTpl}
            ${samDef}
            ${platfPluginDef}
            ${sam}
            ${platfPlugin}
            <script>
                try{
                  window.global={
                    pName:"${projectName}",
                    pUser:"${userName}",
                    pn:"${projectName}/${userName}"
                  }
                    // 统一输出语言包 zh-CN 名
                    // if(!window.GLOBAL_LANG_TPL){
                    //   console.log('不影响运行,提示:[[${req.url},当前模板语言包不存在]]')
                    // }
                    // if(!window.GLOBAL_LANG_HIUI){
                    //   console.log('不影响运行,提示:[[${req.url},hiui语言包不存在]]')
                    // }
                    window.lang = window.lang ||{};
                    window.lang.keys = Object.assign({},window.GLOBAL_LANG_TPL||{},window.GLOBAL_LANG_HIUI||{},window.GLOBAL_LANG_PROJECT||{},window.GLOBAL_LANG_MODULE||{},window.GLOBAL_LANG_SCHEME||{});
                    Vue.prototype._i18n = new VueI18n({
                        locale: 'localelang',
                        messages: {
                          'localelang':window.lang.keys,
                        },
                        silentTranslationWarn:true,
                    });
                    (function(lang){
                        lang = lang.split('-');
                        lang= lang[0]+((lang[1]||'').charAt(0).toUpperCase() + (lang[1]||'').slice(1));
                        ELEMENT.locale(ELEMENT.lang[lang])
                    })('${lang}');
                }catch(e){
                  console.log('语言包加载出错！')
                }
            </script>
            <script src="/assets_platform/customSysCfg/index.js"></script>
            <script src="/${projectName}/setting/desktop/base.js" type="text/javascript"></script>
            <script src="/${projectName}/setting/desktop/development.js" type="text/javascript"></script>

            ${titleEnd}`)
          }
          res.writeHead(statusCode, {
            "serverInfo": `${VITE_APP_BASE_API},${VITE_APP_SERVER}`,
            // 'Content-Length': html.length,
            'Content-Type': 'html'
          });
          res.write(html)
          res.end();
        }
        function handleFunc(path) {
          // let token = Cookies[TokenKey];
          let requestUrl = setUrlParam(path, "pn", process.env.VITE_APP_PN);
          if (param && !isFlow(pathname)) {
            requestUrl = requestUrl + "&" + param;
          }
          console.log("----requestUrl:", requestUrl, "token:", token);
          if (!isFlow(pathname)) { Object.assign(post_data, postData); }
          let lang = result["locale"] || Cookies["locale"] || "zh-CN";
          lang = lang.replace("_", "-");
          var requestOption = {
            url: requestUrl,
            method: isFlow(pathname) ? "GET" : "POST",
            json: true,
            body: post_data,
            headers: {
              "token": token,
              "Cookie": `locale=${lang}`,
              "Authorization": "Bearer " + token,
              "Content-Type": "application/json;charset=UTF-8"
            }
          }

          if (isFlow(pathname)) {
            requestOption["qs"] = post_data;
          } else if (isFunc(pathname)) { //页面流用表单提交
            requestOption["form"] = post_data;
          }
          request(requestOption, requestCallback, (msg) => {
            res.writeHead(404, {
              "Content-Type": "application/json;charset=UTF-8"
            });
            res.end(JSON.stringify(msg));
            // console.log(msg)
          });
        }
        function renderVm() {
          res.writeHead(200, {
            'Content-Type': 'html'
          });
          let projectName = process.env.VITE_APP_PN.split("/")[0];
          let url = serverPath + req.url;
          url = setUrlParam(url, "pn", process.env.VITE_APP_PN);
          url = setUrlParam(url, "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="${url}";
                  </script>
                  `
          res.end(html);
        }
        function requestCallback(error, response, body) {
          let fmodelpath = pathname;
          if (typeof body == "string") {
            try {
              body = JSON.parse(body);
            } catch (error) {
              res.writeHead(response.statusCode, {
                "Content-Type": "html"
              });
              res.end(body);
              return;
            }
          }
          //console.log(error, response, body);
          if (!error && response.statusCode == 200) {
            let pcform;
            let designPath;
            let isGuest = false;
            if (body) {
              if (isFlow(fmodelpath) && body.outParameter) {
                pcform = body.outParameter.task.bizpcform;
                if (!result["locale"] && body.outParameter.locale) {
                  result["locale"] = body.outParameter.locale;
                }
              } else if (isFunc(fmodelpath) || isInfc(fmodelpath)) {
                if (body && body.logicflow || isInfc(fmodelpath)) { //逻辑流
                  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 (!result["locale"] && body.dataPack.locale) {
                    result["locale"] = body.dataPack.locale;
                  }
                }
              }
            }
            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"))) {
              renderVm();
              return;
            }
            // var fullPath = "eaptpl/12112/mokuai/shitumulu/biaodan/dtv/1.0.0/desktop/index.html";
            renderHtml(pcform, isGuest, body);
          } 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>
                          //判断是否跨域请求变量
                          let isCrossDomain=false;
                          try{
                              let __isCrossDomain=top.window.SysPage;
                          } catch(e){
                              isCrossDomain=true;
                          }
                          if(!isCrossDomain&&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 {
              console.log("--------------------responselog-------------------------", error, response, body);
              res.writeHead(response?.statusCode || 200, {//保证有响应头 200
                "serverInfo": `${VITE_APP_BASE_API},${VITE_APP_SERVER}`,
                // "Content-Type": "application/json;charset=UTF-8"
                'Content-Type': 'html'
              });
              res.end(body ? JSON.stringify(body) : '')// error.toString() error可能是空
              // res.end(JSON.stringify(body||{}));
            }
            //res.end(requestUrl + ":" + JSON.stringify(error) + JSON.stringify(body));
          }
        }

        var serverPath = VITE_APP_BASE_API + VITE_APP_SERVER;
        if (req.url.indexOf("/development.js") != -1 || req.url.indexOf("/base.js") != -1 || req.url.indexOf("/studio/appsetting.js") != -1) {
          if (req.headers.referer) {
            if (req.headers.referer.startsWith("https")) {
              options["VITE_APP_BASE_API"] = options["VITE_APP_BASE_API"].replace("http://", "https://");
            } else {
              options["VITE_APP_BASE_API"] = options["VITE_APP_BASE_API"].replace("https://", "http://");
            }
          }
          let content, url;
          try {
            if (req.url.indexOf("/studio/appsetting.js") != -1) {//动态流程用本地文件
              throw error();
            }
            url = `${VITE_APP_BASE_API}/vm/render/${userName}/${projectName}/setting/desktop/${req.url.split("/").pop()}`;
            content = await syncRequest(url);
          } catch (e) {
            try {
              content = fs.readFileSync(req.url.substr(1), {
                encoding: 'utf8'
              })
            } catch {
              res.writeHead(404, {
                "Content-Type": "application/json;charset=UTF-8"
              });
              res.end("获取", req.url + "文件异常!");
              return;
            }
          }
          try {
            var html = ejs.render(content, options);
            html = `// serverPath:${serverPath}\n\n` + html;
          } catch (e) {
            res.end(e.message);
            return;
          }
          res.end(html);
          return;
        } else if (req.url.startsWith("/render")) {
          if (req.headers.referer) {
            if (req.headers.referer.startsWith("https")) {
              options["VITE_APP_BASE_API"] = options["VITE_APP_BASE_API"].replace("http://", "https://");
            } else {
              options["VITE_APP_BASE_API"] = options["VITE_APP_BASE_API"].replace("https://", "http://");
            }
          }
          let content, url;
          try {
            if (req.url.indexOf("/studio/appsetting.js") != -1) {//动态流程用本地文件
              throw error();
            }
            const pathAfterRender = req.url.split('render').pop();
            if (pathAfterRender.includes(projectName)) {
              const resourcePath = pathAfterRender.split(projectName).pop();
              url = `${VITE_APP_BASE_API}/render/${userName}/${projectName}${resourcePath}`;
            } else {
              url = `${VITE_APP_BASE_API}/render${pathAfterRender}`;
            }
            const cleanReqPath = req.url.split('?')[0];
            // 根据扩展名判断是否为二进制资源，远程请求时也以二进制模式获取
            const ext = cleanReqPath.substring(cleanReqPath.lastIndexOf('.')).toLowerCase();
            const binaryExts = ['.png', '.jpg', '.jpeg', '.gif', '.ico', '.woff', '.woff2', '.ttf', '.eot', '.mp3', '.mp4', '.pdf'];
            if (binaryExts.includes(ext)) {
              content = await syncRequest(url, null, true); // Buffer
            } else {
              content = await syncRequest(url);
            }
          } catch (e) {
            try {
              // 检查是否为二进制文件，如果是则不使用文本编码
              const cleanReqPath = req.url.split('?')[0];
              const ext = cleanReqPath.substring(cleanReqPath.lastIndexOf('.')).toLowerCase();
              const binaryExts = ['.png', '.jpg', '.jpeg', '.gif', '.ico', '.woff', '.woff2', '.ttf', '.eot', '.mp3', '.mp4', '.pdf'];
              if (binaryExts.includes(ext)) {
                content = fs.readFileSync(cleanReqPath.substr(1));
              } else {
                content = fs.readFileSync(cleanReqPath.substr(1), {
                  encoding: 'utf8'
                });
              }
            } catch {
              res.writeHead(404, {
                "Content-Type": "application/json;charset=UTF-8"
              });
              res.end("获取" + req.url + "文件异常!");
              return;
            }
          }
          try {
            // 如果是二进制资源则直接发送，不做 ejs 渲染
            const cleanReqPath2 = req.url.split('?')[0];
            const ext = cleanReqPath2.substring(cleanReqPath2.lastIndexOf('.')).toLowerCase();
            const binaryExts = ['.png', '.jpg', '.jpeg', '.gif', '.ico', '.woff', '.woff2', '.ttf', '.eot', '.mp3', '.mp4', '.pdf'];
            if (binaryExts.includes(ext)) {
              const contentType = getContentType(cleanReqPath2);
              res.setHeader('Content-Type', contentType);
              // content 可能是 Buffer（远程或本地），直接返回
              res.end(content);
              return;
            }
            var html = ejs.render(content, options);
            // html = `// serverPath:${serverPath}\n\n` + html;
          } catch (e) {
            res.end(e.message);
            return;
          }

          // 为静态资源设置正确的Content-Type
          const contentType = getContentType(req.url);
          res.setHeader('Content-Type', contentType);

          res.end(html);
          return;
        } else if (routerMapLocal.indexOf(pathname) != -1) {
          if (!routerMap[pathname]) {
            routerMap = await getRountMap(routerMapUrl);
          }
          if (routerMap[pathname].attributes.custom_page) {
            let url = `${routerMap[pathname].attributes.custom_page}`;
            let html = `<script>
                        window.location.href="${url}"
                        </script>
                        `
            res.end(html);
          } else {
            let pcform = routerMap[pathname].url;
            renderHtml(pcform, false, {}, true)
          }
        } else if (isFlow(pathname) || isFunc(pathname) || isInfc(pathname)) {
          // let url = req.url;
          let post = '';
          postData = '';
          let path;
          let fmodelpath = pathname; // url.split("?")[0];
          var post_data = {};
          // var param = req.url.split("?")[1];

          if (isFlow(fmodelpath)) {
            var fversion = "";
            var ftaskguid = "";
            var fbzid = "";
            if (param) {
              fversion = result["_version"] || result["version"];
              ftaskguid = result["_ftaskguid"] || result["ftaskguid"];
              fbzid = result["fbzid"];
            }
            //flow/open参数改地址栏，切岗位不能用post
            post_data = {
              fmodelpath: fmodelpath,
              fversion,
              ftaskguid, fbzid
            }
            if (result["fnumber"]) {
              post_data = Object.assign(post_data, { fnumber: result["fnumber"] })
            }
            path = serverPath + `/flow/open?origin=pre&skip=false`;
          } else if (isInfc(fmodelpath)) {
            path = serverPath + fmodelpath;
          } else {
            var viewItemId = ""
            if (param) {
              viewItemId = result["__viewItemId"] || result["viewItemId"] || "";
            }
            path = serverPath + fmodelpath + "?origin=pre&skip=false&viewItemId=" + viewItemId;
          }
          // let pn = url.split("/")[1];

          req.on('data', (chunk) => {
            post += chunk;
            postData = querystring.parse(post);
          });
          req.on('end', (chunk) => {
            handleFunc(path)
          });
          return;
        } else {
          if (isHtml(pathname)) {
            renderHtml(pathname.substr(1), false, {})
          } else {
            var newPath = `/render/${userName}`;
            var assets_platform = "/render/assets_platform";
            if (pathname.indexOf(newPath) != -1) {
              req.url = req.url.replace(newPath, "");
            }
            if (pathname.indexOf(assets_platform) != -1) {
              req.url = req.url.replace(assets_platform, "/assets_platform");
            }
            next();
          }
        }
      })
    },
  }
}
export default myPlugin;
