import java.io.*;
import java.util.*;
import window.*;
import mishin870.*;
import javax.microedition.lcdui.*;

public class Script{
String file;
Vector subs = new Vector();
Vector lines;
Vector vars = new Vector();
WinForm wf;
String arg;

public Script(WinForm wf,InputStream is,String arg){
this.wf = wf;
this.arg = arg;
lines = getFile(is);
parseScript();
}

public Vector getButtons(){
Vector ret = new Vector();
for(int i=0;i<vars.size();i++){
	ScriptVar sv = (ScriptVar) vars.elementAt(i);
	if(sv.value instanceof Button){
		ret.addElement(sv);
	}
}
return ret;
}

public Vector getTextBoxes(){
Vector ret = new Vector();
for(int i=0;i<vars.size();i++){
	ScriptVar sv = (ScriptVar) vars.elementAt(i);
	if(sv.value instanceof WinText){
		ret.addElement(sv);
	}
}
return ret;
}

public Vector getImgs(){
Vector ret = new Vector();
for(int i=0;i<vars.size();i++){
	ScriptVar sv = (ScriptVar) vars.elementAt(i);
	if(sv.value instanceof WinImage){
		ret.addElement(sv);
	}
}
return ret;
}

public void addVar(String name,Object value){
ScriptVar sv = new ScriptVar(name,value);
vars.addElement(sv);
}

public Object getVar(String name){
if(name.equals("$g")){
	return wf.g;
}
if(name.equals("$win")){
	return wf;
}
if(name.equals("$null")){
	return null;
}
int temp = findVar(name);
if(temp!=-1){
	return ((ScriptVar) vars.elementAt(temp)).value;
} else {
	return null;
}
}

public int findVar(String name){
for(int i=0;i<vars.size();i++){
	ScriptVar sv = (ScriptVar) vars.elementAt(i);
	if(sv.name.equals(name)){
		return i;
	}
}
return -1;
}

public void setVar(String name,Object value){
int temp = findVar(name);
if(temp!=-1){
	ScriptVar sv = (ScriptVar) vars.elementAt(temp);
	sv.value = value;
} else {
	addVar(name,value);
}
}

public void parseScript(){
for(int i=0;i<lines.size();i++){
	String s = (String) lines.elementAt(i);
	try{
		if(s.substring(0,4).equals("sub ")){
			subs.addElement(s.substring(4,s.length()) + ":" + i);
		}
	} catch (Exception e){}
}
}

public void call(String sub){
for(int i=0;i<subs.size();i++){
	String s = (String) subs.elementAt(i);
	int temp = getNumCharSplit(s,":");
	if(s.substring(0,temp).equals(sub)){
		startSub(Integer.parseInt(s.substring(temp+1,s.length())));
	}
}
}

public Object callFunction(String f,Vector arg){
Object[] arr = Splitter.getArrayFromVector(arg);
try{
	if(f.equals("trace")){
		System.out.println((String) arr[0]);
		return null;
	}
} catch (Exception e){}
try{
	if(f.equals("setMouse")){
		Main.midlet.mw.cx = getIntFromObject(arr[0]);
		Main.midlet.mw.cy = getIntFromObject(arr[1]);
		return null;
	}
} catch (Exception e){}
try{
	if(f.equals("getFile")){
		WinFile wf = new WinFile();
		wf.init((String) arr[0]);
		return wf;
	}
} catch (Exception e){}
try{
	if(f.equals("p")){
		float arg1 = getFloatFromObject(arr[0]);
		float arg2 = getFloatFromObject(arr[1]);
		float ret = arg1+arg2;
		return String.valueOf(ret);
	}
} catch (Exception e){}
try{
	if(f.equals("m")){
		float arg1 = getFloatFromObject(arr[0]);
		float arg2 = getFloatFromObject(arr[1]);
		float ret = arg1-arg2;
		return String.valueOf(ret);
	}
} catch (Exception e){}
try{
	if(f.equals("setPos")){
		if(arr[0] instanceof Button){
			Button b = (Button) arr[0];
			b.setPosition(getIntFromObject(arr[1]), getIntFromObject(arr[2]));
		}
		if(arr[0] instanceof WinText){
			WinText wt = (WinText) arr[0];
			wt.setPosition(getIntFromObject(arr[1]), getIntFromObject(arr[2]));
		}
		if(arr[0] instanceof WinImage){
			WinImage i = (WinImage) arr[0];
			i.setPosition(getIntFromObject(arr[1]), getIntFromObject(arr[2]));
		}
		if(arr[0] instanceof WinForm){
			WinForm fo = (WinForm) arr[0];
			fo.setPosition(getIntFromObject(arr[1]), getIntFromObject(arr[2]));
		}
		return null;
	}
} catch (Exception e){}
try{
	if(f.equals("end")){
		Main.midlet.mw.removeForm(wf.id);
		return null;
	}
} catch (Exception e){}
try{
	if(f.equals("drawImage")){
		Graphics g = (Graphics) arr[0];
		Image img = (Image) arr[1];
		int x = getIntFromObject(arr[2]);
		int y = getIntFromObject(arr[3]);
		g.drawImage(img,x,y,20);
		return null;
	}
} catch (Exception e){}
try{
	if(f.equals("createImage")){
		return wf.createImageFromFile((String) arr[0]);
	}
} catch (Exception e){}
try{
	if(f.equals("createControl")){
		if(arr[0].equals("b")){
			return wf.createButton((String) arr[1],getIntFromObject(arr[2]),getIntFromObject(arr[3]),getIntFromObject(arr[4]),getIntFromObject(arr[5]));
		}
		if(arr[0].equals("t")){
			return wf.createWinText((String) arr[1],getIntFromObject(arr[2]),getIntFromObject(arr[3]),getIntFromObject(arr[4]),getIntFromObject(arr[5]),getIntFromObject(arr[6]));
		}
		if(arr[0].equals("i")){
			return wf.createImage(getIntFromObject(arr[1]),getIntFromObject(arr[2]),getIntFromObject(arr[3]),getIntFromObject(arr[4]));
		}
	}
} catch (Exception e){}
try{
	if(f.equals("resizeControl")){
		if(arr[0] instanceof Button){
			Button b = (Button) arr[0];
			b.resize(b.caption, getIntFromObject(arr[1]), getIntFromObject(arr[2]), b.md);
			wf.refresh();
		}
		if(arr[0] instanceof WinText){
			WinText wt = (WinText) arr[0];
			wt.resize(wt.caption, getIntFromObject(arr[1]), getIntFromObject(arr[2]));
			wf.refresh();
		}
		if(arr[0] instanceof WinImage){
			WinImage i = (WinImage) arr[0];
			i.resize(getIntFromObject(arr[1]), getIntFromObject(arr[2]), i.scale);
			wf.refresh();
		}
		if(arr[0] instanceof WinForm){
			WinForm fo = (WinForm) arr[0];
			fo.resize2(getIntFromObject(arr[1]), getIntFromObject(arr[2]));
		}
		return null;
	}
} catch (Exception e){}
try{
	if(f.equals("setCaption")){
		if(arr[0] instanceof Button){
			Button b = (Button) arr[0];
			b.resize((String) arr[1], b.width, b.height, b.md);
			wf.refresh();
		}
		if(arr[0] instanceof WinText){
			WinText wt = (WinText) arr[0];
			wt.resize((String) arr[1], wt.wid, wt.hei);
			wf.refresh();
		}
		if(arr[0] instanceof WinForm){
			WinForm fo = (WinForm) arr[0];
			fo.name = (String) arr[1];
		}
		return null;
	}
} catch (Exception e){}
try{
	if(f.equals("paint")){
		Object control = arr[0];
		Graphics g = (Graphics) arr[1];
		if(g!=null){
			if(control instanceof Button){
				Button b = (Button) control;
				b.paint(g);
			}
			if(control instanceof WinText){
				WinText wt = (WinText) control;
				wt.paint(g);
			}
			if(control instanceof WinImage){
				WinImage wi = (WinImage) control;
				wi.paint(g);
			}
		}
		return null;
	}
} catch (Exception e){}
return null;
}

public int getIntFromObject(Object obj){
	return (int) getFloatFromObject(obj);
}

public float getFloatFromObject(Object obj){
	return Float.parseFloat((String) obj);
}

public void startSub(int line){
Vector v = getSubLines(line);
for(int i=1;i<v.size();i++){
	callCommand((String) v.elementAt(i));
}
}

public ScriptCommand parseCommand(String f){
try{
	StringDataInputStream sdis = new StringDataInputStream(f);
	ScriptCommand ret;
	int start=0;
	String name;
	Vector arg = new Vector();
	int ch;
	while((ch=sdis.read())!=40){
		if(ch==-1){
			return null;
		}
		start++;
	}
	name = f.substring(0,start);
	String s;
	while((s=getS(sdis))!=null){
		arg.addElement(s);
	}
	ret = new ScriptCommand(name,arg);
	return ret;
} catch (Exception e){e.printStackTrace();}
return null;
}

String getS(StringDataInputStream sdis){
try{
	String s = "";
	boolean quote=false;
	int ch;
	while(((ch=sdis.read())!=41 && ch!=44 && ch!=-1) || quote){
		if(ch==34){
			quote=!quote;
		}
		s+=String.valueOf((char) ((ch >= 0xc0 && ch <= 0xFF) ? (ch + 0x350) : ch));
	}
	if(ch==-1 && s.length()==0){
		return null;
	} else {
		return s;
	}
} catch (Exception e){}
return null;
}

public Object prepareArgument(String s){
int g = getIsProperty(s);
if(g==-1){
	if(s.charAt(0)=='"' && s.charAt(s.length()-1)=='"'){
		String s2 = s.substring(1,s.length()-1);
		return s2;
	}
	if(s.charAt(0)=='$'){
		try{
			return getVar(s);
		} catch (Exception e){}
	}
} else {
	String varName = s.substring(0,g);
	String prop = s.substring(g+1,s.length());
	return callProperty(varName,prop);
}
return null;
}

Object getVar2(String v){
int g = getIsProperty(v);
if(g==-1){
	return getVar(v);
} else {
	return callProperty(v.substring(0,g),v.substring(g+1,v.length()));
}
}

int getIsProperty(String f){
return getOmenPos(f,46);
}

int getIsCommand(String f){
return getOmenPos(f,40);
}

int getOmenPos(String f,int omen){
try{
	StringDataInputStream dis = new StringDataInputStream(f);
	int ch;
	int ret=0;
	boolean quote=false;
	while(((ch=dis.read())!=omen && ch!=-1) || quote){
		if(ch==34){
			quote=!quote;
		}
		ret++;
	}
	if(ch!=-1){
		return ret;
	} else {
		return -1;
	}
} catch (Exception e){}
return -1;
}

int getEqual(String f){
return getOmenPos(f,61);
}

public void callCommand(String func){
String[] a;
int g = getEqual(func);
if(g!=-1){
	Object ret;
	int g2 = getIsCommand(func.substring(g+1,func.length()));
	if(g2!=-1){
		ScriptCommand sc = parseCommand(func.substring(g+1,func.length()));
		for(int i=0;i<sc.arg.size();i++){
			String s = (String) sc.arg.elementAt(i);
			Object obj = prepareArgument(s);
			sc.arg.setElementAt(obj,i);
		}
		ret = callFunction(sc.name,sc.arg);
	} else {
		ret = prepareArgument(func.substring(g+1,func.length()));
	}
	String otv = func.substring(0,g);
	setVar(otv,ret);
} else {
	ScriptCommand sc = parseCommand(func);
	for(int i=0;i<sc.arg.size();i++){
		String s = (String) sc.arg.elementAt(i);
		Object obj = prepareArgument(s);
		sc.arg.setElementAt(obj,i);
	}
	int g2 = getIsProperty(sc.name);
	if(g2==-1){
		callFunction(sc.name,sc.arg);
	} else {
		callProperty2(sc.name.substring(0,g2),sc.name.substring(g2+1,sc.name.length()),sc.arg);
	}
}
}

class ReaderThread implements Runnable {
	private WinFile wif;
	private String varName;
	private Script scr;
	
	public ReaderThread(WinFile wif, String varName, Script scr){
		this.wif = wif;
		this.varName = varName;
		this.scr = scr;
	}
	
	public void run(){
		wif.read(varName, scr);
	}
	
}

public void callProperty2(String varName,String prop,Vector arg){
try{
Object[] arr = Splitter.getArrayFromVector(arg);
Object obj = getVar(varName);
if(obj instanceof WinFile){
	if(prop.equals("write")){
		WinFile f = (WinFile) obj;
		f.save((String) arr[0]);
		return;
	}
	if(prop.equals("read")){
		new Thread(new ReaderThread((WinFile) obj, varName, this)).start();
		return;
	}
}
if(obj instanceof Button){
	if(prop.equals("paint")){
		Graphics g = (Graphics) arr[0];
		Button b = (Button) obj;
		b.paint(g);
		return;
	}
}
if(obj instanceof WinText){
	if(prop.equals("paint")){
		Graphics g = (Graphics) arr[0];
		WinText wt = (WinText) obj;
		wt.paint(g);
		return;
	}
}
if(obj instanceof WinImage){
	if(prop.equals("paint")){
		Graphics g = (Graphics) arr[0];
		WinImage wi = (WinImage) obj;
		wi.paint(g);
		return;
	}
}
} catch (Exception e){}
}

public void FileReaded(String varName, String str){
	setVar("$inname",varName);
	setVar("$instr",str);
	call("file.readed");
}

public Object callProperty(String varName,String prop){
try{
Object obj = getVar(varName);
if(obj==null){
	return null;
} else {
	if(obj instanceof WinFile){
		if(prop.substring(0,4).equals("read")){
			new Thread(new ReaderThread((WinFile) obj, varName, this)).start();
			return null;
		}
		if(prop.substring(0,5).equals("write")){
			WinFile f = (WinFile) obj;
			f.save((String) prepareArgument(prop.substring(6,prop.length()-1)));
			return null;
		}
	}
	if(obj instanceof Button){
		if(prop.substring(0,1).equals("x")){
			Button b = (Button) obj;
			return String.valueOf(b.x);
		}
		if(prop.substring(0,1).equals("y")){
			Button b = (Button) obj;
			return String.valueOf(b.y);
		}
		if(prop.substring(0,1).equals("w")){
			Button b = (Button) obj;
			return String.valueOf(b.width);
		}
		if(prop.substring(0,1).equals("h")){
			Button b = (Button) obj;
			return String.valueOf(b.height);
		}
		if(prop.substring(0,6).equals("paint(")){
			Vector v = new Vector();
			v.addElement(obj);
			v.addElement(prepareArgument(prop.substring(6,prop.length()-1)));
			callFunction("paint",v);
			return null;
		}
		if(prop.substring(0,7).equals("caption")){
			Button b = (Button) obj;
			return b.caption;
		}
	}
	if(obj instanceof WinText){
		if(prop.substring(0,1).equals("x")){
			WinText wt = (WinText) obj;
			return String.valueOf(wt.x);
		}
		if(prop.substring(0,1).equals("y")){
			WinText wt = (WinText) obj;
			return String.valueOf(wt.y);
		}
		if(prop.substring(0,1).equals("w")){
			WinText wt = (WinText) obj;
			return String.valueOf(wt.wid);
		}
		if(prop.substring(0,1).equals("h")){
			WinText wt = (WinText) obj;
			return String.valueOf(wt.hei);
		}
		if(prop.substring(0,6).equals("paint(")){
			Vector v = new Vector();
			v.addElement(obj);
			v.addElement(prepareArgument(prop.substring(6,prop.length()-1)));
			callFunction("paint",v);
			return null;
		}
		if(prop.substring(0,7).equals("caption")){
			WinText wt = (WinText) obj;
			return wt.caption;
		}
	}
	if(obj instanceof WinForm){
		if(prop.substring(0,1).equals("x")){
			WinForm f = (WinForm) obj;
			return String.valueOf(f.x);
		}
		if(prop.substring(0,1).equals("y")){
			WinForm f = (WinForm) obj;
			return String.valueOf(f.y);
		}
		if(prop.substring(0,1).equals("w")){
			WinForm f = (WinForm) obj;
			return String.valueOf(f.wid);
		}
		if(prop.substring(0,1).equals("h")){
			WinForm f = (WinForm) obj;
			return String.valueOf(f.hei);
		}
		if(prop.substring(0,2).equals("mx")){
			return String.valueOf(Main.midlet.mw.cx);
		}
		if(prop.substring(0,2).equals("my")){
			return String.valueOf(Main.midlet.mw.cy);
		}
		if(prop.substring(0,3).equals("mlx")){
			return String.valueOf(wf.getLocalMouseX());
		}
		if(prop.substring(0,3).equals("mly")){
			return String.valueOf(wf.getLocalMouseY());
		}
		if(prop.substring(0,7).equals("caption")){
			WinForm f = (WinForm) obj;
			return f.name;
		}
	}
	if(obj instanceof WinImage){
		if(prop.substring(0,1).equals("g")){
			WinImage wi = (WinImage) obj;
			return wi.g;
		}
		if(prop.substring(0,1).equals("x")){
			WinImage wi = (WinImage) obj;
			return String.valueOf(wi.x);
		}
		if(prop.substring(0,1).equals("y")){
			WinImage wi = (WinImage) obj;
			return String.valueOf(wi.y);
		}
		if(prop.substring(0,1).equals("w")){
			WinImage wi = (WinImage) obj;
			return String.valueOf(wi.wid);
		}
		if(prop.substring(0,1).equals("h")){
			WinImage wi = (WinImage) obj;
			return String.valueOf(wi.hei);
		}
		if(prop.substring(0,6).equals("paint(")){
			Vector v = new Vector();
			v.addElement(obj);
			v.addElement(prepareArgument(prop.substring(6,prop.length()-1)));
			callFunction("paint",v);
			return null;
		}
	}
}
} catch (Exception e){}
return null;
}

public Vector getSubLines(int line){
Vector ret = new Vector();
boolean whbol=true;
int temp=line;
try{
	while(whbol){
		try{
			if(((String) lines.elementAt(temp)).substring(0,7).equals("end sub")){
				whbol = false;
			} else {
				ret.addElement((String) lines.elementAt(temp));
			}
		} catch (Exception e){ret.addElement((String) lines.elementAt(temp));}
		temp++;
	}
} catch (Exception e){}
return ret;
}

int getNumCharSplit(String s,String delim){
char temp = delim.charAt(0);
for(int i=0;i<s.length();i++){
	if(s.charAt(i)==temp){
		return i;
	}
}
return 0;
}

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

public Vector getFile(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;
	sb = new StringBuffer();
	try {
		while ((ch = dis.read()) != -1 & !whbol) {
			if(ch!=13 & ch!=10 & ch!=-1){
				sb.append((char) getCh(ch));
			} else {
				whbol = true;
			}
		}
		String s = sb.toString();
		if(s.length()>2){
			if(!s.substring(0,2).equals("//")){
				v.addElement(s);
			}
		}
		if(ch==-1){
			b = true;
		}
	} catch (Exception e) {e.printStackTrace();}
}
return v;
}

}