import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

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.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordEnumeration;


public class AddressMIDlet
    extends MIDlet
    implements CommandListener {

    private Form addForm;
    private TextField name;
    private TextField number;

    private Form listForm;

    private Command exitCommand;
    private Command okCommand;
    private Command newAddressCommand;
    private Command backCommand;

    private Display display;

    private String url;

    private Storage storage = new Storage();

    /**
     * Konstruktor třídy - vytvoří formulář
     */
    public AddressMIDlet() {
        addForm = new Form("Nový záznam");

        name = new TextField("Jméno", "", 40, TextField.ANY);
        addForm.append(name);

        number =
            new TextField("Telefon", "", 10, TextField.NUMERIC);
        addForm.append(number);

        okCommand = new Command("OK", Command.BACK, 1);
        addForm.addCommand(okCommand);

        backCommand = new Command("Zrušit", Command.SCREEN, 2);
        addForm.addCommand(backCommand);

        addForm.setCommandListener(this);

        listForm = new Form("");

        exitCommand = new Command("Konec", Command.BACK, 1);
        listForm.addCommand(exitCommand);

        newAddressCommand = new Command("Přidej", Command.OK, 1);
        listForm.addCommand(newAddressCommand);

        listForm.setCommandListener(this);

    }

    /**
     * Tuto metodu volá aplikační manažer při přechodu midletu
     * do aktivního stavu. 
     */
    protected void startApp()
        throws MIDletStateChangeException {
        if (display == null) {
            display = Display.getDisplay(this);
        }
        storage.openStore("address");
        actualizeList();
        display.setCurrent(listForm);
    }

    /**
     * Tuto metodu volá aplikační manažer při přechodu midletu
     * do pasivního stavu
     */
    protected void pauseApp() {
        storage.closeStore();
    }

    /**
     * Tuto metodu volá aplikační manažer při ukončení aplikace
     */
    protected void destroyApp(boolean unconditional)
        throws MIDletStateChangeException {
        storage.closeStore();
    }

    /**
     * Metoda rozhraní CommandListener
     */
    public void commandAction(Command c, Displayable d) {
        if (c.equals(exitCommand)) {
            try {
                destroyApp(true);
                notifyDestroyed();
            } catch (Exception e) {
            }
        } else if (c.equals(okCommand)) {
            ByteArrayOutputStream buffer =
                new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(buffer);
            try {
                dos.writeUTF(name.getString());
                dos.writeUTF(number.getString());
                storage.add(buffer);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    dos.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            actualizeList();
            display.setCurrent(listForm);
        } else if (c.equals(backCommand)) {
            display.setCurrent(listForm);
        } else if (c.equals(newAddressCommand)) {
            name.setString("");
            number.setString("");
            display.setCurrent(addForm);
        }
    }
    
    /**
     * Aktualizace formuláře se seznamem všech telefonních čísel
     */
    private void actualizeList() {
        RecordEnumeration enum = storage.getEnumeration();
        if (enum != null) {
            for (int i = listForm.size() - 1; i >= 0; i--) {
                listForm.delete(i);
            }
            try {
                while (enum.hasNextElement()) {
                    DataInputStream dis =
                        new DataInputStream(
                            new ByteArrayInputStream(
                                enum.nextRecord()));

                    String s = dis.readUTF();
                    String num = dis.readUTF();
                    listForm.append(new StringItem(s, num));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

