//dev 前端调试端口

import crypt.bin;
import wsock.tcp.client;
import fsys;

 
namespace chrome;

class dev{
	ctor( theApp,url,devPort,timeout ){
		theApp.httpHandler["/aardio.dev" ] = function(response,request){
    		response.contentType = "text/html";
    		response.write(..string.replace(self.html,"@${DEV_PORT}",tostring(devPort)),) 
		}
		
		if(!theApp.external){
			theApp.external = {};
		}
			
		theApp.external.____devSrc_aardio____ = function(){
			..fsys.enum( "/", "*.*",
				function(dir,filename,fullpath,findData){ 
					if(!filename) {
						if(..io.exist(..io.joinpath(fullpath,"package.json")) ){
							loadcodex("~\extensions\trigger\edit.aardio",fullpath);
							return false; 
						}
					}
				} 
				,false
			); 
		} 
		
		timeout = timeout : 0.5;
		var tcp = ..wsock.tcp.client();
		if( tcp.connectTimeout("localhost",devPort,timeout) ){
			theApp.start("http://localhost:"+devPort)
			tcp.close();
		};
		else {
			theApp.start("/aardio.dev")
			
			var form = ..win.form().messageOnly()
		
			form.setInterval( 
				function(){
					if( tcp.connectTimeout("localhost",devPort,timeout) ){ 
						theApp.publish("devServer.started","http://localhost:"+devPort);
						tcp.close();
						
						form.close();
						return false;
					};
				},1000 
			) 
		}
	};
}

dev.html = `
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>正在等待 http://localhost:${DEV_PORT} 启动……</title>
    <style type="text/css">
    html,body{ height:100%; margin:30px; } 
    @font-face {
  	font-family: 'FontAwesome'; 
  	src:url(`+..crypt.bin.base64DataUrl( $"~\lib\fonts\.res\FontAwesome.ttf", "application/x-font-ttf;charset=utf-8")+`) format('truetype');
  	font-weight: normal;
  	font-style: normal;
	}
	.fa {
  	display: inline-block;
  	font: normal normal normal 14px/1 FontAwesome;
  	font-size: inherit;
  	text-rendering: auto;
  	-webkit-font-smoothing: antialiased; 
	}
	
	.fa-spin { 
  	animation: fa-spin 2s infinite linear;
	}
 	
	@keyframes fa-spin {
  	0% { 
    	transform: rotate(0deg);
  	}
  	100% { 
    	transform: rotate(359deg);
  	}
	}
	
	.fa-spinner:before {
  	content: "\f110";
	}
	
	h5{margin-bottom:0;padding-bottom:5px}
    </style>
    <script src="/aardio.js"></script>
    <script>aardio.on("devServer.started",function(url){window.location.replace(url+location.search) }); </script> 
    <script type="text/javascript"></script>
</head>
<body>
<h3><span class ="fa fa-spin fa-spinner"></span> <span>正在等待 http://localhost:${DEV_PORT} 启动……</span></h3>
<p>
请在 aardio 工程内右键点击「网页源码」目录, <br>
在弹出菜单中点击「<a onclick="javascript:aardio.____devSrc_aardio____()" href="javascript:void(0)">用外部编辑器打开</a>」, 在 <a href="https://code.visualstudio.com/" target="_blank">VSCode</a>  中打开前端工程源码，<br>
然后按 Ctrl + `` 打开 <a href="https://code.visualstudio.com/docs/editor/integrated-terminal" target="_blank">VSCode 终端</a>。<br>
<br>
<h5>调试步骤：</h5> 
1、输入命令 <a href="https://docs.npmjs.com/cli/v7/commands/npm-install" target="_blank">npm install</a> 安装依赖，<br>
2、然后运行 npm run start 启动调试服务器。<br>
3、右键点击网页可打开 DevTools。<br>
<br>
<h5>发布步骤：</h5> 
1、运行  npm run build 发布发布前端代码。<br>
2、在 aardio 中按 F7 生成 exe 文件。</p>
</body>
</html>`	