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

public class Minwin extends Canvas{
int ku=getKeyCode(UP);
int kd=getKeyCode(DOWN);
int kl=getKeyCode(LEFT);
int kr=getKeyCode(RIGHT);
int kf=getKeyCode(FIRE);
int k;
Vector w = new Vector();
Vector focuses = new Vector();
Image back;
Image cur;
int cx,cy;
int move=-1;
int mx=0,my=0;
DownBar db;
boolean isPointer=false;

public Minwin() {
setFullScreenMode(true);
cx = getWidth()/2;
cy = getHeight()/2;
setTheme(Main.midlet.getTheme(Main.midlet.theme));
addForm("script.mwp","");
}

public void addForm(String name,String arg){
if(!Main.midlet.debug){
	try{
		if(name.substring(0,7).equals("file://")){
			FileConnection fc = (FileConnection) Connector.open(name);
			if(!fc.exists()){
				name = "/error.mwp";
				arg = "Файл не найден!";
			}
		}
	} catch (Exception e){}
}
int t = Main.midlet.theme;
windowTheme wt = null;
boolean type = Main.midlet.getType();
if(type){
	wt = new vistaTheme();
} else {
	wt = new classicTheme();
}
WinForm f = new WinForm(w.size(),name,arg);
f.init(wt,"",50,50);
db.add(w.size(),f.theme.ico);
f.setPosition(10,10);
w.addElement(f);
String s = String.valueOf(w.size()-1);
focuses.addElement(s);
formUp(w.size()-1);
}

public void setTheme(String t){
Main.midlet.createZa(t);
if(Main.midlet.za==null){
	addForm("/error.mwp","Файл не найден!");
}
boolean type = Main.midlet.getType();
if(!type){
	z.za = Main.midlet.za;
	classicTheme ct = new classicTheme();
	setThemeWindows(ct);
	try{
		cur = Image.createImage(Main.midlet.getInput("cur.png"));
		back = Image.createImage(Main.midlet.getInput("background.png"));
		back = ImageUtils.resize_image(back,getWidth(),getHeight());
	} catch (Exception e){}
	Vector temp = null;
	try{temp = db.dt.w;}catch (Exception e){}
	db = new DownBar(new classicDownTheme(getWidth(),getHeight()),getWidth(),getHeight());
	if(temp!=null){
		db.dt.w = temp;
	}
	return;
} else {
	z.za = Main.midlet.za;
	vistaTheme vt = new vistaTheme();
	setThemeWindows(vt);
	try{
		cur = Image.createImage(Main.midlet.getInput("cur.png"));
		back = Image.createImage(Main.midlet.getInput("background.png"));
		back = ImageUtils.resize_image(back,getWidth(),getHeight());
	} catch (Exception e){}
	Vector temp = null;
	try{temp = db.dt.w;}catch (Exception e){}
	db = new DownBar(new vistaDownTheme(getWidth(),getHeight()),getWidth(),getHeight());
	if(temp!=null){
		db.dt.w = temp;
	}
	return;
}
}

void nextTheme(){
int t = Main.midlet.theme;
t++;
if(t>=Main.midlet.num_themes){
	t=0;
}
Main.midlet.theme = t;
setTheme(Main.midlet.getTheme(t));
}

void setThemeWindows(windowTheme wt){
for(int i=0;i<w.size();i++){
	WinForm temp = (WinForm) w.elementAt(i);
	temp.setTheme(wt);
}
}

public void removeForm(int num){
w.removeElementAt(num);
db.remove(num);
for(int i=num;i<w.size();i++){
	WinForm temp = (WinForm) w.elementAt(i);
	temp.id--;
}
focuses.removeElementAt(getNumFocuse(0,focuses));
downAllFocuses();
}

void downAllFocuses(){
for(int i=0;i<focuses.size();i++){
	int temp = Integer.parseInt((String) focuses.elementAt(i));
	temp--;
	focuses.setElementAt(String.valueOf(temp),i);
}
}

//Отладочная функция для вывода всех фокусов
void traceFocuses(){
System.out.println("begin");
for(int i=0;i<focuses.size();i++){
	System.out.println("focuses[" + i + "]=" + Integer.parseInt((String) focuses.elementAt(i)));
}
System.out.println("end");
}

void formUp(int num){
int[] f = getNormFocuses(focuses);
if(f[0]==num){
	return;
}
try{
	for(int i=f.length-1;i>0;i--){
		int temp = f[i-1];
		if(temp!=num){
			f[i] = temp;
		}
	}
	f[0] = num;
} catch (Exception e){}
focuses = focusesToVector(f);
}

public void paint(Graphics g) {
g.drawImage(back,0,0,20);
for(int i=w.size()-1;i>=0;i--){
	paintFocus(g,i);
}
db.paint(g);
g.drawImage(cur,cx,cy,20);
temp();
}

void paintFocus(Graphics g,int foc){
try{
	WinForm temp = (WinForm) w.elementAt(getNumFocuse(foc,focuses));
	temp.paint(g);
} catch (Exception e){}
}

public void temp(){
try{Thread.sleep(10);} catch (Exception e){}
if(k==kl){
	cx-=2;
}
if(k==kr){
	cx+=2;
}
if(k==ku){
	cy-=2;
}
if(k==kd){
	cy+=2;
}
if(cx<0){cx=0;}
if(cx>=getWidth()){cx=getWidth()-1;}
if(cy<0){cy=0;}
if(cy>=getHeight()){cy=getHeight()-1;}
if(move!=-1){
	if(cy>getHeight()-15){
		cy = getHeight()-15;
	}
	if(k==kl || k==kr || k==ku || k==kd || isPointer){
		WinForm temp = (WinForm) w.elementAt(move);
		temp.x = cx-mx;
		temp.y = cy-my;
	}
}
repaint();
}

public void checkMouse(boolean down,int pointer){
Vector temp = new Vector();
if(cy<getHeight()-14){
	for(int i=0;i<w.size();i++){
		WinForm f = (WinForm) w.elementAt(i);
		if(f.visible){
			if(getCollisionMouse(cx,cy,f.x,f.y,f.wid+f.theme.offsetXC,f.hei+f.theme.offsetYC)){
				temp.addElement(focuses.elementAt(i));
			} else {
				temp.addElement(String.valueOf(-1));
			}
		} else {
			temp.addElement(String.valueOf(-1));
		}
	}
}
checkClick(temp,down,pointer);
}

void checkClick(Vector v,boolean down,int pointer){
if(cy<getHeight()-14){
	if(v==null){return;}
	if(v.size()<=0){return;}
	int mini=-1;
	int min = 50000;
	for(int i=0;i<v.size();i++){
		try{
			int temp = Integer.parseInt((String) v.elementAt(i));
			if(temp>=0){
				if(temp<min){
					min = temp;
					mini = i;
				}
			}
		} catch (Exception e){}
	}
	if(mini!=-1){
		formUp(mini);
		click(mini,down,pointer);
	}
} else {
	checkDownBar(down,pointer);
}
}

void checkDownBar(boolean down,int pointer){
if(pointer==0){
	if(!down){
		int ret = db.dt.checkClick(cx,cy-(getHeight()-14));
		if(ret==1){
			Main.midlet.startMenu();
			return;
		}
		if(ret>1){
			ret = ret-2;
			WinForm f = (WinForm) w.elementAt(ret);
			f.visible=!f.visible;
			if(f.visible){
				formUp(ret);
			}
			return;
		}
	}
} else {
	if(pointer==1){
		int ret = db.dt.checkClick(cx,cy-(getHeight()-14));
		if(ret==1){
			Main.midlet.startMenu();
			return;
		}
		if(ret>1){
			ret = ret-2;
			WinForm f = (WinForm) w.elementAt(ret);
			f.visible=!f.visible;
			if(f.visible){
				formUp(ret);
			}
			return;
		}
	}
}
}

void click(int num,boolean down,int pointer){
WinForm temp = (WinForm) w.elementAt(num);
int state = temp.getClick(cx-temp.x,cy-temp.y);
if(pointer==1){
	down = true;
}
if(pointer==3){
	down = false;
}
if(state==3){
	temp.minSet(down);
	if(!down){
		temp.visible = false;
	}
}
if(state==4){
	temp.maxSet(down);
	if(!down){
		temp.maximize(getWidth(),getHeight()-14);
	}
}
if(state==5){
	temp.cloSet(down);
	if(!down){
		removeForm(num);
	}
}
if(state==1){
	if(pointer==0){
		if(!down){
			if(move==-1){
				if(!temp.maximized){
					move = num;
					mx = cx-temp.x;
					my = cy-temp.y;
				}
			} else {
				move = -1;
			}
		}
	} else {
		if(pointer==1){
			if(!temp.maximized){
				isPointer = true;
				move = num;
				mx = cx-temp.x;
				my = cy-temp.y;
			}
		} else {
			isPointer = false;
			move = -1;
		}
	}
}
if(state==6){
	point p = temp.theme.getXYClick(cx-temp.x,cy-temp.y);
	temp.formMouse(p.x,p.y,down);
}
}

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 void keyPressed(int keyCode){
k = keyCode;
if(k==kf){
	checkMouse(true,0);
	k = 0;
}
if(k==49){
	nextTheme();
}
}

public void keyReleased(int keyCode){
if(keyCode==kf){
	checkMouse(false,0);
}
k = 0;
}

protected void pointerPressed(int x,int y){
cx = x;
cy = y;
checkMouse(true,1);
}

protected void pointerDragged(int x,int y){
cx = x;
cy = y;
//checkMouse(true,2);
}

protected void pointerReleased(int x,int y){
cx = x;
cy = y;
checkMouse(false,3);
isPointer = false;
move = -1;
}

//Вспомогательные функции для упрощения работы с фокусами
int[] getNormFocuses(Vector f){
int[] ret = new int[f.size()];
for(int i=0;i<f.size();i++){
	ret[i] = getNumFocuse(i,f);
}
return ret;
}

Vector focusesToVector(int[] f){
Vector ret = new Vector();
for(int i=0;i<f.length;i++){
	ret.addElement(String.valueOf(getNumFocuse(i,f)));
}
return ret;
}

int getNumFocuse(int foc,int[] f){
for(int i=0;i<focuses.size();i++){
	int tempi = f[i];
	if(tempi==foc){
		return i;
	}
}
return -1;
}

int getNumFocuse(int foc,Vector f){
for(int i=0;i<focuses.size();i++){
	String s = (String) focuses.elementAt(i);
	int tempi = Integer.parseInt(s);
	if(tempi==foc){
		return i;
	}
}
return -1;
}
//Конец вспомогательных функций

}