import win.ui;
/*DSG{{*/
mainForm = win.form(text="开始页.....";left=0;top=0;right=599;bottom=399;border="none";exmode="none";mode="child";parent=...;scroll=1)
mainForm.add()
/*}}*/

import ide;
import web.layout;
global.onError = function( err,over ){ 
	ide.setConfig("startpage","off");
	mainForm.msgboxErr('抱歉！\n开始页遇到错误，\n已自动禁用开始页。',"aardio")
	ide.pluginViewShow(mainForm);
	win.close(win.getParent(win.getParent(mainForm.hwnd)));

}

if( _WINE ){
	ide.pluginViewShow(mainForm);
	win.close(win.getParent(...))
	return;
}

var wbLayout = web.layout(mainForm);   
wbLayout.go("\res\start-page.htm");  
ide.pluginViewShow(mainForm);
wbLayout.wait();
 
import string.xml;
getProjectInfo = function(path){
 	if(!path) return;
 	if( ..string.startWith(path,"\\") ) return;
 	
	var xmlstr = string.load(path); 
	if(!xmlstr) return;
	
	var xmlDoc = string.xml( xmlstr )
 	if(!xmlDoc) return;


	var project = xmlDoc.queryEles( tagName = "project"); 
	if(!project) return;
	return project[1];
}

loadRecentProjectFromFile = function(){
	var recentList = {};
	var cfgRecentProject = ..string.load("~/config/RECENT.PROJECT.TABLE");
	if( cfgRecentProject ){
		try{
			recentList = eval(cfgRecentProject)
		} 
	}	
	return recentList;
}

refreshRecentProject = function(){
	var ltList = wbLayout.getEle("recent-projects-list"); 
	ltList.innerHTML = "";
	
	var recentList = loadRecentProjectFromFile();
	if( #recentList ){
		var len = #recentList;
		if( len > 12 ) len = 12;
		for(i=1;len;1){
			if(!recentList[i])break;
			var inf = getProjectInfo( recentList[i] );
			if(!inf) {
				if( #recentList + 1 > len ){
					table.remove(recentList,i);
					i--;
				}
				continue;
			}
			ltList.printf( '<li href="%s" name="recent-project-item">%s</li> '
				,recentList[i],inf.name ); 
		}
	}
	else {
		ltList.innerHTML =  '<li href="http://api.aardio.com/v10/pages/first-project" >我的第一个工程</li>'
	}	
}
refreshRecentProject(); 

import process;
wbLayout.sinking = { 
	onHyperlinkClick = {
		["recent-project-item"] = function (ltTarget,ltEle,reason,behaviorParams) {
			
			var path = ltEle.href; 
			if( io.exist(path) ){ 
				thread.invoke( 
					function(path){
						import ide;
						ide.openDocument(path);
					},path
				) 
			}
			return true;
		}
		["create-project"] = function (ltTarget,ltEle,reason,behaviorParams) {  
			thread.invoke( 
					function(path){
						import ide;
						ide.newProject();
					},path
				)
			return true;
		}
		["open-project"] = function (ltTarget,ltEle,reason,behaviorParams) { 
		
			import fsys.dlg;
			var defDir = ide.getConfig("project_default_dir");
			
			var recentList = loadRecentProjectFromFile();
			if(#recentList) defDir = ..fsys.getParentDir( recentList[1] ); 
			
			var path = fsys.dlg.open("aardio工程文件|*.aproj||","请选择要打开的aardio工程文件",defDir,ide.getMainHwnd(),,"default.aproj")
			if( path ? io.exist(path) ){ 
				thread.invoke( 
					function(path){
						import ide;
						ide.openDocument(path);
					},path
				) 
			}
			return true;
		}
		["update-tool"] = function (ltTarget,ltEle,reason,behaviorParams) { 
			loadcode("~\tools\4.update.aardio")("自动更新");
			ltEle.delete();
			return true;
		} 
		
		["lnkdsk"]= function (ltTarget,ltEle,reason,behaviorParams) { 
			import fsys.lnk;

			lnk = fsys.lnk(); 
			lnk.description = "aardio:网站/桌面软件快速开发工具"  
			
			lnk.path = io._exepath //设置目标路径
			lnk.setIcon(io._exepath,0); //设置图标
			 
			lnk.save(
				fsys.joinpath( 
					fsys.getSpecial(0x0010 /*_CSIDL_DESKTOPDIRECTORY*/ )
					,"aardio.lnk" 
					)
			) 
			
			loadcode("/dsktip.aardio")();
			return true;
		} 
		
		["jiaqun"] = function (ltTarget,ltEle,reason,behaviorParams) {
			import web.form;
			var frmJiaQun = win.form(text="Web Form";right=1;bottom=1);
			var wb = web.form( frmJiaQun ,,,,true/*securityTrusted*/ );
			wb.noScriptErr=true;
			wb.go(ltEle.href);
			wb.wait(,5000);
			frmJiaQun.close();
			return true;
		}
		["default"] = function (ltTarget,ltEle,reason,behaviorParams) {
			var path = ltEle.href;  
			if( ..string.endWith(path,".aardio",true)  ){  
				if( ..io.exist(path)) {
					ide.createProcess(path);
					return true;
				}
				
				import inet.url; 
				var code,err = win.invoke(
					function(url){
						import inet.http;
						return inet.http(null,"").get(url)
					},path
				)
				if(!code) {
					if(path) process.execute(path);
					return true;
				}
				
				import fsys;
				fsys.createDir("/.sampleCode/");
				
				var codePath = "/.sampleCode/" + ..string.match(path,"[^/]+$");
				string.save(codePath,code ) 
				ide.openDocument(codePath)
				return true;
			} 
			
			if(path) { 
				thread.invoke( 
					function(path){
						import process;
						process.execute(path);
					},path
				)
			} 
			
			return true;
		}
	}
}

wbLayout.onButtonClick = {
	["search-tool"] = function (ltTarget,ltEle,reason,behaviorParams) { 
		loadcode("~\tools\search.aardio")("自动更新"); 
		return true;
	} 
}


wbLayout.onMenuItemClick  = {
	["open-project"] = function (ltTarget,ltOwner,reason,behaviorParams) {
		var ltPopupOwner = ..web.layout.element( behaviorParams.he )
		ide.openDocument(ltPopupOwner.href)
	}
	["open-project-as"] = function (ltTarget,ltOwner,reason,behaviorParams) {
		var ltPopupOwner = ..web.layout.element( behaviorParams.he )
		process.execute(io._exepath,ltPopupOwner.href);
	}
	["explore-project"] = function (ltTarget,ltOwner,reason,behaviorParams) {
		var ltPopupOwner = ..web.layout.element( behaviorParams.he )
		process.explore_select(ltPopupOwner.href);
	}
	["delete-project"] = function (ltTarget,ltEle,reason,behaviorParams) {
		var ltPopupOwner = ..web.layout.element( behaviorParams.he )
		var recentList = loadRecentProjectFromFile(); 
		if(#recentList){
			var idx = ..table.find( recentList,ltPopupOwner.href ); 
			if(idx){ 
				table.remove( recentList,idx)   
				..string.save("~/config/RECENT.PROJECT.TABLE", table.tostring(recentList)); 
				refreshRecentProject(); 
			} 
		}                
	}
	["delete-project-all"] = function (ltTarget,ltEle,reason,behaviorParams) {
		..string.save("~/config/RECENT.PROJECT.TABLE", "{}"); 
		refreshRecentProject();              
	}
}

import ide;
import thread.event
mainForm.updateNews = function(html){
	wbLayout.getEle("start-page-news").innerHTML =  html : "<li href='#'>获取官网最新消息失败......<li>";

	var event = thread.event(ide.processGroup(".startPage")); 
	event.set();
}

import ide.command;
ide.command.join( ide.processGroup() );
var listener = ide.command();
listener.aardioQuickerRecentProjectChanged = function(){
	refreshRecentProject();
}

listener.aardioQuickerUpdated = function(strVersion){
	if(!strVersion)return;
	var ltEle = wbLayout.getEle("update-tool")
	if( ltEle ) ltEle.delete();
	
	var html =  "<li href='#' #update-tool style='color:red;'>发现aardio已更新版本:" + strVersion + ",点这里立即更新<li>"
	wbLayout.getEle("start-page-news").insertAdjacentHTML("afterBegin", html);
	ide.mdiActivate( mainForm.hwnd );
} 

listener.aardioQuickerUpdateFinish = function(){
	var ltEle = wbLayout.getEle("update-tool")
	if( ltEle ) ltEle.delete();
}

thread.invoke(
	function(mainForm){ 
		import inet.http;
		var http = inet.http(); 
		var html  = http.get("http://api.aardio.com/v10/pages/startpage/"); 
		http.close();
		mainForm.updateNews(html);
	},mainForm
)

mainForm.onDestroy = function(){
    win.quitMessage()
}

mainForm.show(0x3/*_SW_MAXIMIZE*/);
mainForm.setTimeout(function(){
	mainForm.show(0x3/*_SW_MAXIMIZE*/);
},1000);

//转发IDE快捷键
var ideHwnd = ide.getMainHwnd();
mainForm.isDialogMessage = function(hwnd,msg){
	if( msg.message == 0x100/*_WM_KEYDOWN*/){
		var vk = msg.wParam;
		if( ( vk >0x70/*_VK_F1*/ && vk < 0x7B/*_VK_F12*/)
			|| vk == 0x11/*_VK_CTRL*/ || vk==0x10/*_VK_SHIFT*/ || vk==0x12/*_VK_ALT*/ ){
			::PostMessage(ideHwnd,msg.message,msg.wParam,msg.lParam);
			return true;
		}
			
		var ctrl = ::GetKeyState( 0x11/*_VK_CONTROL*/ ) & 0x8000;
    	var alt = ::GetKeyState( 0x12/*_VK_ALT*/ ) & 0x8000;
    	var shift = ::GetKeyState( 0x10/*_VK_SHIFT*/ ) & 0x8000;
    	if( ctrl || alt || shift ){
    		::PostMessage(ideHwnd,msg.message,msg.wParam,msg.lParam);
			return true;
    	} 
	}
	return win.isDialogMessage(hwnd,msg);
}

win.loopMessage();
