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

public class Button{
public boolean md=false;
public Image butimg,but;
public FontClass fc = new FontClass();
public int x,y;
public String caption;
public int width,height;
int r1,g1,b1,r2,g2,b2;
Vector v;

public Button(String caption,int width,int height){
	this.caption = caption;
	this.width = width;
	this.height = height;
	fc.Init("ArialNarrow8");
	fc.setColor(255,0,0,0);
	updateTheme();
	mouse(false);
}

public void loadConfig(InputStream is){
	v = z.getFile(is);
	r1 = toInt(0);
	g1 = toInt(1);
	b1 = toInt(2);
	r2 = toInt(3);
	g2 = toInt(4);
	b2 = toInt(5);
	v = null;
}

int toInt(int num){
	return Integer.parseInt((String) v.elementAt(num));
}

public void refresh(){
	mouse(md);
}

public void updateTheme(){
	try{butimg = Image.createImage(z.getInput("button.png"));} catch (Exception ex){}
	loadConfig(z.getInput("bconfig.cfg"));
	refresh();
}

public void mouse(boolean mousedown){
	md = mousedown;
	resize(caption,width,height,mousedown);
}

public void paint(Graphics h){
	h.drawImage(but,x,y,20);
}

public void setPosition(int x,int y){
	this.x = x;
	this.y = y;
}

public void resize(String caption,int width,int height,boolean mousedown){
	this.caption = caption;
	this.md = mousedown;
	but = buttoncreate(caption,width,height,mousedown);
}

public Image buttoncreate(String caption,int width,int height,boolean mousedown){
	Image captionsize = Image.createImage(1,1);
	Graphics h = captionsize.getGraphics();
	int captionwidthint = fc.getWidth(h,caption);
	captionsize = null;
	Image ret = Image.createImage(width,height);
	h = ret.getGraphics();
	if(mousedown==false){
		h.setColor(r1,g1,b1);
		h.fillRect(0,0,width,height);
		fc.drawString(h,caption,(width/2)-(captionwidthint/2),(height/2)-7,width-4,height-4);
		h.drawRegion(butimg, 0, 0, 2, 2, Sprite.TRANS_NONE, 0, 0, 20);
		h.drawRegion(butimg, 2, 2, 2, 2, Sprite.TRANS_NONE, width-2, height-2, 20);
		h.drawRegion(butimg, 0, 2, 2, 2, Sprite.TRANS_NONE, 0, height-2, 20);
		h.drawRegion(butimg, 2, 0, 2, 2, Sprite.TRANS_NONE, width-2, 0, 20);
		for(int i = 2;i<width-2;i+=2){
			h.drawRegion(butimg, 1, 0, 2, 2, Sprite.TRANS_NONE, i, 0, 20);
		}
		for(int i = 2;i<width-2;i+=2){
			h.drawRegion(butimg, 1, 2, 2, 2, Sprite.TRANS_NONE, i, height-2, 20);
		}
		for(int i = 2;i<height-2;i+=2){
			h.drawRegion(butimg, 0, 1, 2, 2, Sprite.TRANS_NONE, 0, i, 20);
		}
		for(int i = 2;i<height-2;i+=2){
			h.drawRegion(butimg, 2, 1, 2, 2, Sprite.TRANS_NONE, width-2, i, 20);
		}
	} else {
		h.setColor(r2,g2,b2);
		h.fillRect(0,0,width,height);
		fc.drawString(h,caption,(width/2)-(captionwidthint/2)+2,(height/2)-5,width-4,height-4);
		h.drawRegion(butimg, 5, 0, 2, 2, Sprite.TRANS_NONE, 0, 0, 20);
		h.drawRegion(butimg, 5, 4, 2, 2, Sprite.TRANS_NONE, 0, height-2, 20);
		h.drawRegion(butimg, 9, 4, 2, 2, Sprite.TRANS_NONE, width-2, height-2, 20);
		h.drawRegion(butimg, 9, 0, 2, 2, Sprite.TRANS_NONE, width-2, 0, 20);
		for(int i = 2;i<width-2;i+=2){
			h.drawRegion(butimg, 7, 0, 2, 2, Sprite.TRANS_NONE, i, 0, 20);
		}
		for(int i = 2;i<width-2;i+=2){
			h.drawRegion(butimg, 7, 4, 2, 2, Sprite.TRANS_NONE, i, height-2, 20);
		}
		for(int i = 2;i<height-2;i+=2){
			h.drawRegion(butimg, 5, 2, 2, 2, Sprite.TRANS_NONE, 0, i, 20);
		}
		for(int i = 2;i<height-2;i+=2){
			h.drawRegion(butimg, 9, 2, 2, 2, Sprite.TRANS_NONE, width-2, i, 20);
		}
	}
h = null;
return ret;
}

}