import window.*;
import javax.microedition.lcdui.*;
import net.sf.zipme.*;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;

public class WinForm extends window{
Image f;
String sname;
Script script;
ZipArchive za;
String arg;
public Graphics g;

public WinForm(int id,String sname,String arg){
super(id);
this.arg = arg;
this.sname = sname;
}

public void load(String n){
try{
	za = null;
	if(n.substring(0,7).equals("file://")){
		FileConnection fc = (FileConnection) Connector.open(n);
		if(fc.exists()){
			za = new ZipArchive(fc.openInputStream());
		} else {
			za = new ZipArchive(getClass().getResourceAsStream("/script.mwp"));
		}
	} else {
		za = new ZipArchive(getClass().getResourceAsStream(n));
	}
	ZipEntry ze = za.getEntry("ico.png");
	InputStream is = za.getInputStream(ze);
	theme.ico = Image.createImage(is);
	ze = za.getEntry("info.mwi");
	is = za.getInputStream(ze);
	getInfo(is);
	ze = za.getEntry("Main.mws");
	is = za.getInputStream(ze);
	script = new Script(this,is,arg);
	script.call("form.load");
	
} catch (Exception e){}
}

public int getCh(int ch){
int ret;
if((ch >= 0xc0 && ch <= 0xFF)){
	ret = (ch + 0x350);
} else {
	ret = ch;
}
return ret;
}

public Vector getLines(InputStream is){
DataInputStream dis = new DataInputStream(is);
Vector v = new Vector();
StringBuffer sb;
boolean b=false;
while(!b){
	boolean whbol = false;
	sb = new StringBuffer();
	int ch = 0;
	try {
		while ((ch = dis.read()) != -1 & !whbol) {
			if(ch!=13 & ch!=10 & ch!=-1){
				sb.append((char) (getCh(ch)));
			} else {
				whbol = true;
			}
		}
		v.addElement(sb.toString());
		if(ch==-1){
			b = true;
		}
	} catch (Exception e) {}
}
return v;
}

public void getInfo(InputStream is){
Vector l = getLines(is);
name = (String) l.elementAt(0);
resize(Integer.parseInt((String) l.elementAt(1)),Integer.parseInt((String) l.elementAt(2)));
}

public void afterInit(int wid,int hei){
onResize(wid,hei);
load(sname);
script.call("form.paint");
}

public void onResize(int wid,int hei){
refresh(wid,hei);
}

public void afterPaint(Graphics g){
if(f!=null){
	g.drawImage(f,x+theme.offsetX,y+theme.offsetY,20);
}
}

public void refresh(int wid,int hei){
f = Image.createImage(wid,hei);
g = f.getGraphics();
if(script!=null){
	script.call("form.paint");
}
}


public void refresh(){
f = Image.createImage(f.getWidth(),f.getHeight());
g = f.getGraphics();
if(script!=null){
	script.call("form.paint");
}
}

public void formMouse(int x,int y,boolean md){
Vector buttons = script.getButtons();
for(int i=buttons.size()-1;i>=0;i--){
	ScriptVar sv = (ScriptVar) buttons.elementAt(i);
	Button b = (Button) sv.value;
	if(getCollisionMouse(x,y,b.x,b.y,b.width,b.height)){
		b.mouse(md);
		refresh();
		if(md){
			script.call(sv.name + ".mouse_down");
		} else {
			script.call(sv.name + ".mouse_up");
		}
		return;
	}
}
Vector textboxes = script.getTextBoxes();
for(int i=textboxes.size()-1;i>=0;i--){
	ScriptVar sv = (ScriptVar) textboxes.elementAt(i);
	WinText wt = (WinText) sv.value;
	if(getCollisionMouse(x,y,wt.x,wt.y,wt.wid,wt.hei)){
		if(md){
			refresh();
			script.call(sv.name + ".mouse_down");
		} else {
			Main.midlet.inputText(wt,this);
		}
		return;
	}
}
Vector imgs = script.getImgs();
for(int i=imgs.size()-1;i>=0;i--){
	ScriptVar sv = (ScriptVar) imgs.elementAt(i);
	WinImage wi = (WinImage) sv.value;
	if(getCollisionMouse(x,y,wi.x,wi.y,wi.wid,wi.hei)){
		if(md){
			script.call(sv.name + ".mouse_down");
		} else {
			script.call(sv.name + ".mouse_up");
		}
		return;
	}
}
}

boolean getCollisionMouse(int x,int y,int fx,int fy,int fw,int fh){
return (x>fx) && (y>fy) && (x<fx+fw) && (y<fy+fh);
}

public Button createButton(String name,int x,int y,int w,int h){
	Button temp = new Button(name,w,h);
	temp.setPosition(x,y);
	return temp;
}

public WinText createWinText(String name,int x,int y,int w,int h,int max){
	WinText temp = new WinText(name,w,h,max);
	temp.setPosition(x,y);
	return temp;
}

public WinImage createImage(int x,int y,int w,int h){
	WinImage temp = new WinImage(w,h);
	temp.setPosition(x,y);
	return temp;
}

public Image createImageFromFile(String f){
	try{
		ZipEntry ze = za.getEntry(f);
		InputStream is = za.getInputStream(ze);
		return Image.createImage(is);
	} catch (Exception e){}
	return null;
}

public int getLocalMouseX(){
return Main.midlet.mw.cx-(x+theme.offsetX);
}

public int getLocalMouseY(){
return Main.midlet.mw.cy-(y+theme.offsetY);
}

public void updateControlsTheme(){
Vector buttons = script.getButtons();
for(int i=0;i<buttons.size();i++){
	ScriptVar sv = (ScriptVar) buttons.elementAt(i);
	Button b = (Button) sv.value;
	b.updateTheme();
}
Vector textboxes = script.getTextBoxes();
for(int i=0;i<textboxes.size();i++){
	ScriptVar sv = (ScriptVar) textboxes.elementAt(i);
	WinText wt = (WinText) sv.value;
	wt.updateTheme();
}
}

}