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

public class WinText{
public Image textimg,tex;
public FontClass fc = new FontClass();
public String caption;
public int wid, hei;
private int r,g,b;
public int x,y;
public int max;
private Vector v;

public WinText(String caption,int width,int height,int max){
	this.caption = caption;
	this.wid = width;
	this.hei = height;
	this.max = max;
	loadConfig(z.getInput("tconfig.cfg"));
	fc.Init("ArialNarrow8");
	fc.setColor(255,0,0,0);
	updateTheme();
}

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

public void updateTheme(){
	try{textimg = Image.createImage(z.getInput("textbox.png"));} catch (Exception ex){}
	loadConfig(z.getInput("tconfig.cfg"));
	resize(caption, wid, hei);
}

public void loadConfig(InputStream is){
	v = z.getFile(is);
	r = toInt(0);
	g = toInt(1);
	b = toInt(2);
	v = null;
}

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

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

public void resize(String caption,int width,int height){
	this.caption = caption;
	tex = textboxcreate(caption,width,height);
}

/*
public String getTextFromLine(int tline,String str){
if(tline!=0){
int num_line = 0;
boolean linebol = false;
int i = 0;
int lstr = str.getBytes().length;
while(!linebol){
try{
if((byte) str.charAt(i)==10){
num_line++;
if(num_line==tline){
linebol = true;
}
}
} catch (Exception e) {
linebol = true;
}
i++;
}
return str.substring(i,lstr);
} else {
return str;
}
}
*/

public Image textboxcreate(String caption,int width,int height){
	Image ret = Image.createImage(width,height);
	Graphics h = ret.getGraphics();
	h.setColor(r,g,b);
	h.fillRect(0,0,width,height);
	fc.drawString(h,caption,4,0,width-6,height-6);
	h.drawRegion(textimg, 0, 0, 3, 3, Sprite.TRANS_NONE, 0, 0, 20);
	h.drawRegion(textimg, 3, 3, 3, 3, Sprite.TRANS_NONE, width-3, height-3, 20);
	h.drawRegion(textimg, 0, 3, 3, 3, Sprite.TRANS_NONE, 0, height-3, 20);
	h.drawRegion(textimg, 3, 0, 3, 3, Sprite.TRANS_NONE, width-3, 0, 20);
	for(int i = 3;i<width-3;i+=1){
		h.drawRegion(textimg, 2, 0, 1, 3, Sprite.TRANS_NONE, i, 0, 20);
	}
	for(int i = 3;i<width-3;i+=1){
		h.drawRegion(textimg, 2, 3, 1, 3, Sprite.TRANS_NONE, i, height-3, 20);
	}
	for(int i = 3;i<height-3;i+=1){
		h.drawRegion(textimg, 0, 2, 3, 1, Sprite.TRANS_NONE, 0, i, 20);
	}
	for(int i = 3;i<height-3;i+=1){
		h.drawRegion(textimg, 3, 2, 3, 1, Sprite.TRANS_NONE, width-3, i, 20);
	}
h = null;
return ret;
}

}