import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.*;
import net.sf.zipme.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import window.WinText;

public class Main extends MIDlet implements CommandListener{
public Display dis;
public Minwin mw;
public int theme=0;
public int num_themes;
StartMenu sm;
TextBox tf;
TextBox tftheme;
Command tfok;
Command tfb;
Themes t;
ZipArchive za;
private TextInput ti;
public boolean debug=false;

public static Main midlet;

public Main() {
//debug = true;
midlet=this;
}

public void startApp() {
if(dis==null){
t = new Themes();
num_themes = t.t.size();
mw = new Minwin();
dis = Display.getDisplay(this);
dis.setCurrent(mw);
}
}

public boolean getType(){
String s = getFile(getInput("type.cfg"));
try{s=s.substring(0,1);} catch (Exception e){}
if(s.equals("1")){
	return true;
} else {
	return false;
}
}

public String getFile(InputStream is){
DataInputStream dis = new DataInputStream(is);
StringBuffer sb = new StringBuffer();
try{
	int ch=0;
	while((ch = dis.read())!=-1) {
		sb.append((char) ((ch >= 0xc0 && ch <= 0xFF) ? (ch + 0x350) : ch));
	}
} catch (Exception e){}
return sb.toString();
}

public void createZa(String name){
try{
	if(name.length()>7){
		if(name.substring(0,7).equals("file://")){
			FileConnection fc = (FileConnection) Connector.open(name);
			if(fc.exists()){
				za = new ZipArchive(fc.openInputStream());
			} else {
				za = null;
			}
		} else {
			za = new ZipArchive(getClass().getResourceAsStream(name));
		}
	} else {
		za = new ZipArchive(getClass().getResourceAsStream(name));
	}
} catch (Exception e){za=null;}
}

public InputStream getInput(String name){
try{
	ZipEntry ze = za.getEntry(name);
	InputStream is = za.getInputStream(ze);
	return is;
} catch (Exception e){}
return null;
}

public String getTheme(int num){
return t.getTheme(num);
}

public void startMenu(){
sm = new StartMenu();
dis.setCurrent(sm);
}

public void hideMenu(){
dis.setCurrent(mw);
sm = null;
}

public void inputText(WinText wt, WinForm wf){
	ti = new TextInput(wt,wf);
	dis.setCurrent(ti);
}

public void run(){
tf = new TextBox("Адрес программы","",1000,TextField.ANY);
tfok = new Command("Ок",Command.OK,1);
tfb = new Command("Назад",Command.CANCEL,1);
tf.addCommand(tfok);
tf.addCommand(tfb);
tf.setCommandListener(this);
dis.setCurrent(tf);
}

public void theme(){
tftheme = new TextBox("Адрес темы","",1000,TextField.ANY);
tfok = new Command("Ок",Command.OK,1);
tfb = new Command("Назад",Command.CANCEL,1);
tftheme.addCommand(tfok);
tftheme.addCommand(tfb);
tftheme.setCommandListener(this);
dis.setCurrent(tftheme);
}

public void commandAction(Command c,Displayable d){
if(c==tfok){
	if(d==tf){
		String[] s = Splitter.split(tf.getString(),63);
		if(s.length>1){
			mw.addForm(s[0],s[1]);
		} else {
			mw.addForm(s[0],"");
		}
		dis.setCurrent(mw);
		tf = null;
		tfok = null;
		tfb = null;
		sm = null;
		tftheme = null;
		return;
	}
	if(d==tftheme){
		mw.setTheme(tftheme.getString());
		dis.setCurrent(mw);
		tf = null;
		tfok = null;
		tfb = null;
		sm = null;
		tftheme = null;
		return;
	}
}
if(c==tfb){
	dis.setCurrent(sm);
	tf = null;
	tftheme = null;
	tfok = null;
	tfb = null;
	return;
}
}

public void pauseApp() {}
public void destroyApp(boolean u) {notifyDestroyed();}
}