const Path=require("path"),Fs=require("fs"),FsExtra=require("fs-extra"),Express=require("express"),CfgUtil=Editor.require("packages://hot-update-tools/core/CfgUtil.js"),Util=Editor.require("packages://hot-update-tools/core/Util.js"),OutPut=Editor.require("packages://hot-update-tools/core/OutPut.js"),GoogleAnalytics=Editor.require("packages://hot-update-tools/core/GoogleAnalytics.js"),{shell:shell}=require("electron");Vue.component("env-test",{template:Fs.readFileSync(Editor.url("packages://hot-update-tools/panel/env-test.html"),"utf-8"),mixins:[Editor.require("packages://hot-update-tools/panel/mixin.js")],data:()=>({serverApp:null,serverUrl:null,localServerPath:"",serverVersion:null,serverPackageUrl:null,testEnvALi:!1,testEnvEmail:!1,testEnvSelect:0,emailContent:"邮件内容!",addMailPeople:"",emailPeopleArray:["xu_yanfeng@126.com"]}),created(){this.$nextTick(()=>{let e=CfgUtil.cfgData;e&&(this.localServerPath=e.localServerPath),this._initLocalServerDir(),this._updateServerVersion()})},methods:{_getAvailableNetPort(e,t){let r=require("net").createServer().listen(e);r.on("listening",()=>{r.once("close",()=>{t&&t(e)}),r.close()}),r.on("error",r=>{this._getAvailableNetPort(e+1,t)})},onBtnClickOpenStaticFileServer(){let e=this.localServerPath;e&&Fs.existsSync(e)&&this._getAvailableNetPort(5520,t=>{let r=Express();r.use(Express.static(e)),r.listen(t),this.serverApp=r;let i=Util.getLocalIP();this.serverUrl=`http://${i}:${t}/`,this.log(`Http文件服务开启: ${e}`)})},onCopyFileToLocalServer(){GoogleAnalytics.eventCustom("copyFileToLocalServer");let{localServerPath:e}=this,{manifestResDir:t}=Util,{resourceRootDir:r}=CfgUtil.cfgData,{manifestDir:i}=OutPut;if(!Fs.existsSync(e))return void this.log("本地测试服务器目录不存在:"+e);let s=Path.join(r,"src"),l=Path.join(r,t);if(!Fs.existsSync(r))return void this.log("资源目录不存在: "+r+", 请先构建项目");if(!Fs.existsSync(s))return void this.log(r+"不存在src目录, 无法拷贝文件");if(!Fs.existsSync(l))return void this.log(r+"不存在res目录, 无法拷贝文件");let o=Path.join(i,"project.manifest"),a=Path.join(i,"version.manifest");!i||i.length<=0?this.log("manifest文件生成地址未填写"):Fs.existsSync(o)?Fs.existsSync(a)?(FsExtra.emptyDirSync(e),FsExtra.copySync(s,Path.join(e,"src")),FsExtra.copySync(l,Path.join(e,t)),FsExtra.copyFileSync(o,Path.join(e,"project.manifest")),FsExtra.copyFileSync(a,Path.join(e,"version.manifest")),this.log(`已经将热更包copy到: ${e}`),this._updateServerVersion()):this.log(a+"不存在, 请点击生成热更包"):this.log(o+"不存在, 请点击生成热更包")},onTestEnvChange(e){let t=e.target.value;this.testEnvALi=!1,this.testEnvEmail=!1,"0"===t||("1"===t?this.testEnvALi=!0:"2"===t&&(this.testEnvEmail=!0))},onCleanSimRemoteRes(){let e=process.platform,t=null;if("win32"===e){let e=Path.join(Editor.appPath,"../cocos2d-x/simulator/");t=Path.join(e,"win32/remote-asset")}else if("darwin"===e){let e=Path.join(Editor.appPath,"../cocos2d-x/simulator/");t=Path.join(e,"mac/Simulator.app/Contents/Resources/remote-asset")}t&&(Fs.existsSync(t)?(FsExtra.emptyDirSync(t),this.log("[清理热更缓存] 清空目录 "+t+" 成功.")):this.log("[清理热更缓存] 目录不存在: "+t))},onOpenLocalServer(){this.openDir(this.localServerPath)},onSelectLocalServerPath(e){let t=Editor.projectInfo.path;this.localServerPath&&this.localServerPath.length>0&&Fs.existsSync(this.localServerPath)&&(t=this.localServerPath);let r=Editor.Dialog.openFile({title:"选择本地测试服务器目录",defaultPath:t,properties:["openDirectory"]});-1!==r&&(this.localServerPath=r[0],this._saveConfig(),this._updateServerVersion())},refreshLocalServerVersion(){this._updateServerVersion()},_initLocalServerDir(){this.localServerPath&&this.localServerPath.length>0||(this.localServerPath=OutPut.testServerDir)},_updateServerVersion(){if(this.localServerPath.length>0){let e={"version.manifest":null,"project.manifest":null};for(let t in e){let r=Path.join(this.localServerPath,t);Fs.existsSync(r)?e[t]=JSON.parse(Fs.readFileSync(r,"utf-8")):this.log(`测试环境：未发现文件${t}`)}let t=e["version.manifest"],r=e["project.manifest"];t&&r?t.version===r.version&&t.packageUrl===r.packageUrl&&(this.serverVersion=r.version,this.serverPackageUrl=r.packageUrl):t&&!r?(this.serverVersion=t.version,this.serverPackageUrl=t.packageUrl):!t&&r?(this.serverVersion=r.version,this.serverPackageUrl=r.packageUrl):t||r||this.log("测试环境： 无法获取到本地测试服务器版本号")}else this.log("请选择本机server物理路径")},onBtnClickSendMail(){Mail.sendMail(this.remoteServerVersion,this.emailContent,null,()=>{this.log("发送邮件完毕!")})},onInputMailPeopleOver(){!1===this.isPeopleExist()&&this.emailPeopleArray.push(this.addMailPeople)},isPeopleExist(){if(null===this.addMailPeople||""===this.addMailPeople)return!1;for(let e=0;e<this.emailPeopleArray.length;e++){if(this.emailPeopleArray[e]===this.addMailPeople)return!0}return!1},onClickPackageUrl(){let e=`${this.serverPackageUrl}/version.manifest`;shell&&shell.openExternal(e)},onClickServerUrl(){shell&&shell.openExternal(this.serverUrl)}}});