import ide;
import fsys;
import web.msxml;
import win.ui;
/*DSG{{*/
var winform = win.form( text="快手工程向导 - 创建控制台应用程序";bottom=225;parent=...;right=583;max=false )
winform.add( 
btnEsc={ bottom=208;right=330;left=208;top=174;z=2;text="取消(Esc)";cls="button" };
chkSaveToDefaultDir={ bottom=102;right=326;left=115;top=81;z=11;text="设置父目录为创建工程的默认目录";cls="checkbox" };
lbProjectName={ bottom=130;align="right";text="工程名称:";left=274;top=113;z=8;right=339;transparent=1;cls="static" };
editProjectFile={ bottom=135;right=274;left=115;top=107;z=5;edge=1;cls="edit" };
labProjeFile={ bottom=130;align="right";text="工程文件:";left=35;top=113;z=7;right=100;transparent=1;cls="static" };
editProjectDir={ bottom=72;right=463;left=115;top=44;z=4;edge=1;cls="edit" };
btnDlgDir={ bottom=72;text="浏览...";left=466;top=41;z=10;right=529;cls="button" };
lbProjectDir={ bottom=68;align="right";text="工程目录:";left=35;top=51;z=6;right=100;transparent=1;cls="static" };
editProjectName={ bottom=135;right=528;left=347;top=107;z=9;edge=1;cls="edit" };
groupbox={ bottom=163;text=" console 工程配置 ";left=27;top=16;z=3;right=553;edge=1;cls="groupbox" };
btnCreateProject={ bottom=208;text="创建工程(Enter)";left=336;top=174;z=1;right=510;cls="button" }
)
/*}}*/

var srcTemplate = io.fullpath("/console/") 

import fsys.dlg;
winform.btnDlgDir.oncommand = function(id,event){
	var dir = fsys.dlg.opendir( fsys.getParentDir( winform.editProjectDir.text ),winform.hwnd );
	if( dir ){
		winform.editProjectDir.text = fsys.joinpath(dir,winform.editProjectName.text)
	} 
}

winform.btnEsc.oncommand = function(id,event){
	winform.close()
}

winform.btnCreateProject.oncommand = function(id,event){
	var dstDir = winform.editProjectDir.text;
	dstDir = fsys.createDir(dstDir);
	if( ! dstDir ){
		winform.msgboxErr("请设定正确的工程目录路径!","快手工程向导")
		return;
	}
	  
	fsys.enum(  srcTemplate, 
				"*.*", 
				function(dir,file,fullpath,findData){   
					fsys.copy(fullpath,dstDir )
				},
				false 
	);
	 
	
	var projectFileTemplate = fsys.joinpath(dstDir,"default.aproj");
	if(!io.exist( projectFileTemplate ) ){
		winform.msgboxErr("复制工程文件到目标目录失败!","快手工程向导")
	}
	
	var projectFile = fsys.joinpath(dstDir,winform.editProjectFile.text)
	if( ! string.endWith(projectFile,".aproj",true) ){
		projectFile = projectFile + ".aproj";
	}
	if( ! io.rename(projectFileTemplate,projectFile) ){
		winform.msgboxErr("请设定正确的工程文件名!","快手工程向导")
		return;
	};
	
	var projectName = string.trim( winform.editProjectName.text );
	if(!#projectName){
		winform.msgboxErr("请设定正确的工程名称!","快手工程向导")
		return;
	}
	
	try{
		var xmlDoc = web.msxml();
		xmlDoc.load(projectFile);
		xmlDoc.documentElement.setAttribute("name",winform.editProjectName.text ) ;
		xmlDoc.save(projectFile)
	}
	
	ide.openDocument(projectFile); 
	
	if( winform.chkSaveToDefaultDir.checked ){
		var dir = fsys.getParentDir(dstDir);
		ide.setConfig("project_default_dir",dir)
	}
	winform.close();
}
winform.editProjectDir.text = ide.getProjectDir();
var tpath = io.splitpath( ide.getProjectPath() );
winform.editProjectFile.text = tpath.file;
winform.editProjectName.text = string.match(ide.getProjectDir(),"\\([^\\]+?)\\$");
 
winform.show();
win.loopMessage();
 
