import ide;
import fsys.dlg;
import win.util.tray
import protobuf.parser
import win.ui;
/*DSG{{*/
var winform = win.form(parent=...; text="protobuf编译器";right=538;bottom=295 )
winform.add( 
lbTip={ dl=1;bottom=288;text="本程序编译proto描述文件,并自动在用户库中生成类库代码
建议将此代码文件置入您的工程目录下,打开工程后运行该代码.";left=16;top=261;z=3;db=1;transparent=1;right=524;cls="static" };
editProtoFilePath={ dr=1;dl=1;bottom=33;right=406;left=18;multiline=1;top=8;z=1;text="";dt=1;edge=1;cls="edit" };
tnOpenOutDir={ dr=1;bottom=62;text="选择输出目录";left=414;dt=1;top=35;z=8;right=526;cls="button" };
btnOpenProtoFile={ dr=1;bottom=33;right=526;left=414;dt=1;top=6;z=2;text="选择proto文件...";cls="button" };
editOutDir={ dr=1;dl=1;disabled=1;bottom=61;right=405;left=18;dt=1;top=36;z=7;text="\res\";multiline=1;edge=1;cls="edit" };
editProto={ dr=1;dl=1;vscroll=1;right=524;left=18;dt=1;db=1;cls="edit";bottom=215;text="";multiline=1;top=69;z=4;hscroll=1;edge=1 };
btnGenText={ dr=1;bottom=253;text="编译生成";left=408;top=225;z=5;db=1;right=520;cls="button" };
cmbLang={ dr=1;bottom=248;text="combobox";left=273;right=397;
items={ "AAuto语言";"C++语言";"C++语言( Lite )";"Java语言";"python语言" };top=228;z=6;db=1;mode="dropdown";edge=1;cls="combobox" }
)
/*}}*/ 

if( ! io.exist("\res\protoc.exe") ){ 
	var str = string.load("\res\protoc.exe");
	if(str){
		fsys.createDir("\res\")
		string.save( "\res\protoc.exe", str)
	} 
}

winform.cmbLang.oncommand = function(id,event){
	winform.editProto.disabled = (winform.cmbLang.selIndex!=1)
	if( winform.cmbLang.selIndex!=1){
		winform.editOutDir.disabled = false; 
		winform.editOutDir.text = "\res\out\"
		if( ! #winform.editProtoFilePath.text ){
			if( #winform.editProto.text ){
				string.save("\res\temp.proto", winform.editProto.text )
				winform.editProtoFilePath.text  = "\res\temp.proto"
			}
		}
	}
	else {
		winform.editOutDir.disabled = true;
		winform.editOutDir.text = "\res\"
	}
	
	 
}


winform.cmbLang.selIndex = 1;

winform.editProto.text = /*
message Person {
	required string name = 1;
	required int32 id = 2;
	optional string email = 3;
	
	enum PhoneType {
		MOBILE = 0;
		HOME = 1;
		WORK = 2;
	}
	
	message PhoneNumber {
		required string number = 1;
		optional PhoneType type = 2 [default = HOME];
	}
	// a simple comment
	repeated PhoneNumber phone = 4;
}
	
message AddressBook {
	repeated Person person = 1;
}
*/

import process;
protoc = function( out_type,out_dir,protofile_path){
	out_dir = io.fullpath( out_dir )  
	fsys.createDir(out_dir)
	
	protofile_path = io.fullpath(protofile_path)
	var tpath  = io.splitpath(protofile_path)
	
	protofile_shortpath = fsys.shortpath(protofile_path)
	var tshortpath  = io.splitpath(protofile_shortpath)
 
 	io.open()
	var prcs = process( 
		fsys.shortpath( "\res\protoc.exe" ),
		' --proto_path=' + tshortpath.dir 
		+' --' + out_type + '=' + fsys.shortpath ( out_dir ) 
		+ ' ' + fsys.joinpath(tshortpath.dir,tpath.file)   
	) 
	thread.wait( prcs.thandle );
	io.close()
}

winform.btnGenText.oncommand = function(id,event){
	var selText = winform.cmbLang.selText;
	
	if( selText == "AAuto语言"){
		var p = protobuf.parser();
		p.parse( winform.editProto.text )
		winform.lbTip.text = "编译完成，已添加protobuf类到用户库" 
	}
	else {
		if( ! io.exist(winform.editProtoFilePath.text) ){
			winform.msgbox("请指定有效的proto文件路径","标题")
			winform.editProtoFilePath.setFocus()
			return;
		}
		var map = { ["C++语言"]="cpp_out";["Java语言"]="java_out";["python语言"]="python_out";["C++语言( Lite )"]="cpp_out" };
		
		var protoPath = winform.editProtoFilePath.text
		if( selText = "C++语言( Lite )" ){
			var str = string.load(protoPath)
			str = 'option optimize_for = LITE_RUNTIME;\r\n' + str;
			string.save(protoPath, str)
		}
		else {
			var str = string.load(protoPath)
			str = 'option optimize_for = LITE_RUNTIME;\r\n' + str;
			str = string.replace(str,'@option optimize_for = LITE_RUNTIME;\r\n',"");
			string.save(protoPath, str)	
		}
		
		protoc(  map[selText],winform.editOutDir.text, winform.editProtoFilePath.text);
		process.explore_select(winform.editOutDir.text)
		winform.lbTip.text = '已输出到:' + io.fullpath(winform.editOutDir.text)
		win.util.tray.pop('已生成类库,请打开目录\n' + io.fullpath(winform.editOutDir.text),"Protobuf UI")
	}
	
}
 

winform.btnOpenProtoFile.oncommand = function(id,event){
	var path  = fsys.dlg.open("proto描述文件|*.proto||")
	if(path)
		winform.editProtoFilePath.text = path;
		 
	var tpath = io.splitpath( path )
	if( string.cmp(tpath.ext,".proto") != 0 ){
		winform.msgbox("不是有效的proto文件","错误")
		return;
	}
	winform.editProto.text = string.load(path)
}

winform.tnOpenOutDir.oncommand = function(id,event){
	var path  = fsys.dlg.opendir( )
	if(path)
		winform.editOutDir.text = path
	else {
		winform.editOutDir.text = "";
	} 
}

winform.wndproc = function(hwnd,message,wParam,lParam){
	select(message) {//判断消息类型
		case 0x233/*_WM_DROPFILES*/  { 
			winform.editProtoFilePath.text = win.getDropFile(wParam )[1];   
		} 
	}
}//endproc
 

winform.show() 
win.loopMessage();
return winform;
