package mishin870;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import java.util.Vector;

public class MenuGUI extends Canvas{
public int ku=getKeyCode(UP);
public int kd=getKeyCode(DOWN);
public int kl=getKeyCode(LEFT);
public int kr=getKeyCode(RIGHT);
public int kf=getKeyCode(FIRE);
int sel=0;
Image testimg = Image.createImage(1,1);
FontClass FC;
MenuTheme theme;
Vector Elements = new Vector();
Vector Sizes = new Vector();
Vector Enabled = new Vector();
int ry=-100;
Image sel_img;
int off_sel=0;
int left_sel=0;
int offY=0;

public MenuGUI(){
}

public final void init(MenuTheme theme){
ry = -100;
this.theme = theme;
sel_img = theme.select;
if(sel_img!=null){
off_sel = sel_img.getHeight()/2;
left_sel = getWidth()/2-sel_img.getWidth()/2;
}
setFullScreenMode(true);
FC = new FontClass();
FC.Init(theme.nameFont);
}

public final void setTheme(MenuTheme theme){
ry = -100;
this.theme = theme;
sel_img = theme.select;
if(sel_img!=null){
off_sel = sel_img.getHeight()/2;
left_sel = getWidth()/2-sel_img.getWidth()/2;
}
FC.Destroy();
FC = null;
FC = new FontClass();
FC.Init(theme.nameFont);
}

public final void addElement(String str){
Elements.addElement(str);
Enabled.addElement("1");
Sizes.addElement(getSize(str));
}

public final void setElement(String str, int index){
Elements.setElementAt(str,index);
Sizes.setElementAt(getSize(str),index);
}

String getSize(String str){
String ret = String.valueOf(FC.getWidth(testimg.getGraphics(),str));
return ret;
}

public final void clear(){
Elements.removeAllElements();
Sizes.removeAllElements();
}

public final void removeElementAt(int index){
Elements.removeElementAt(index);
Sizes.removeElementAt(index);
Enabled.removeElementAt(index);
}

public int size(){
return Elements.size();
}

public void beforePaint(Graphics g){
}

public void afterPaint(Graphics g){
}

public final void setOffsetY(int val){
offY = val;
}

public void paint(Graphics g){
beforePaint(g);
g.setColor(theme.backColor);
g.fillRect(0,0,getWidth(),getHeight());
if(sel_img!=null){
	g.drawImage(sel_img,left_sel,ry,20);
}
for(int i=0;i<size();i++){
	String str = (String) Elements.elementAt(i);
	int s = Integer.parseInt((String) Sizes.elementAt(i));
	if(sel==i){
		if((String) Enabled.elementAt(i)=="1"){
			FC.setColor(theme.selColor);
		} else {
			FC.setColor(theme.unselDisColor);
		}
	} else {
		if((String) Enabled.elementAt(i)=="1"){
			FC.setColor(theme.unselColor);
		} else {
			FC.setColor(theme.disColor);
		}
	}
	FC.drawString(g,str,getWidth()/2-s/2,i*20+offY,0,0);
}
afterPaint(g);
if(sel_img!=null){
	if(ry!=sel*20+offY){
		temp();
	}
}
}

void temp(){
try{Thread.sleep(50);} catch (Exception e){}
if(sel_img!=null){
	int destY = sel*20+offY;
	if(ry<destY){
		ry+=(destY-ry)/3;
		if(destY-ry<=2){
			ry = destY;
		}
	}
	if(ry>destY){
		ry-=(ry-destY)/2;
		if(ry-destY<=2){
			ry = destY;
		}
	}
}
repaint();
}

public void select(int sel){
}

public void left(int sel){
}

public void right(int sel){
}

public void setEnabled(boolean e,int index){
if(e){
	Enabled.setElementAt("1",index);
} else {
	Enabled.setElementAt("0",index);
}
}

public void keyPressed(int k){
int s = size()-1;
if(k==kf || k==53){
	if((String) Enabled.elementAt(sel)=="1"){
		select(sel);
	}
}
if(k==kl){left(sel);}
if(k==kr){right(sel);}
if(k==ku || k==50){sel--;}
if(k==kd || k==56){sel++;}
if(sel<0){sel=s;}
if(sel>s){sel=0;}
repaint();
}

}