package novotnyr.mobile.dice;
import java.util.Random;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class TestMidlet extends MIDlet implements CommandListener, ItemCommandListener {
    TextBox textBox;
    
    Command exitCommand = new Command("Konec", Command.BACK, 1);;    
    
    Form form;
    
    Command dSixCommand = new Command("d6", Command.ITEM, 1);
    Command dTwentyCommand = new Command("d20", Command.ITEM, 1);
    
    StringItem rolledResult; 
    
    public TestMidlet() {
        form = new Form("Nápovìda");
        form.append("Obdivujte tento midlet dlho a dlho a dlho a dlho (možno pôjdu aj mäkèene a dåžne)");
        form.addCommand(exitCommand);
        
        Item item = new StringItem("d6", "Six");        
        item.addCommand(dSixCommand);
        item.setItemCommandListener(this);
        form.append(item);
        
        item = new StringItem("d20", "Twenty");
        item.addCommand(dTwentyCommand);
        form.append(item);
        
        rolledResult = new StringItem("Result:", "-");
        form.append(rolledResult);
        
        form.setCommandListener(this);
    }
   
    protected void startApp() throws MIDletStateChangeException {
        Display.getDisplay(this).setCurrent(form);        
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }

    
    public void commandAction(Command command, Displayable displayable) {
        if (command.equals(exitCommand)) {
            try {
               destroyApp(true);
               notifyDestroyed();
            } catch (Exception e) {
                //nothing
            }
        }
    }
    
    
    public void commandAction(Command command, Item item) {
        if(command.equals(dSixCommand)) {
            int generated = new Random().nextInt(5) + 1;
            String sGenerated = String.valueOf(generated);
            rolledResult.setText(sGenerated);
        }

    }
}
