import com.sun.kjava.*;

public class MakeEzDB extends Spotlet {
    
    // handle on the singleton Graphics object
    static Graphics g = Graphics.getGraphics();
    
    // the GUI buttons
    Button exitButton;
    Button makeButton;
    MyCheckBox checkFoo;
    MyCheckBox checkBar;
    MyCheckBox checkBaz;

    // used to display the status line
    String header = "Press \"Make DB\" after making choices";
    // the
    static int dbType = 0x457A4442; // 'EzDB'
    static int dbCreator = 0x414B414E; // 'AKAN'
    Database db;

    /**
     * The main method simply creates a Scribble spotlet and
     * registers its event handlers.
     */
    public static void main(String[] args) {
	(new MakeEzDB()).register(NO_EVENT_OPTIONS);
    }
    
    /**
     * Default constructor creates the GUI components and draws them.
     */
    public MakeEzDB() {
	
	// create the button
	makeButton = new Button("Make DB",1,145);
	exitButton = new Button("Exit",139,145);
	// The three checkboxes to hold our choices
	checkFoo = new MyCheckBox(60, 60, "Foo");
	checkBar = new MyCheckBox(60, 70, "Bar");
	checkBaz = new MyCheckBox(60, 80, "Baz");
	paint();
    }
    
    /**
     * Draw the screen
     */
    private void paint() {
	g.clearScreen();
	g.drawString(" Make a DB Demo ", 34, 0, Graphics.INVERT);
	g.drawString(header, 5, 40);
	// Draw GUI controls and buttons
	checkFoo.paint();
	checkBar.paint();
	checkBaz.paint();
	exitButton.paint();
	makeButton.paint();
    }
    
    /**
     * Handle a pen down event.
     */
    public void penDown(int x, int y) {
	// if we push "Make DB", we write to the database
	if (makeButton.pressed(x,y)) {
	    db = new Database(dbType, dbCreator, Database.WRITEONLY);
	    if (db.isOpen()) {
		// clear it out
		int numRecords = db.getNumberOfRecords();
		for (int i=(numRecords -1); i > -1; i--) {
		    db.deleteRecord(i);
		}
	    } else {
		// if you didn't just successfully open it, makeit
		Database.create(0, "EzDB", dbCreator, dbType,
				false);
		db = new Database(dbType, dbCreator,
				  Database.WRITEONLY);
	    }
	    // The flashId is the ID of the Palm device you are
	    // running on
	    String flashId = getFlashID();
	    // flashId will be null in the emulator
	    if (flashId == null){
		flashId="FlashID = POSE Emulator"; 
	    } else {
		flashId="FlashID = " + flashId;
	    }
	    flashId +='\0';
	    
	    // recordWithNull is a place to convert our Java Strings
	    // to classic '\0' terminated C strings
	    String recordWithNull;

	    // First process the flashId
	    recordWithNull = flashId + '\0';
    	    boolean success = db.addRecord(recordWithNull.getBytes());
//    	    boolean success = db.setRecord(db.getNumberOfRecords(), recordWithNull.getBytes());
	    // now get the checkboxes that we selected
	    if (checkFoo.getState()) {
		recordWithNull = checkFoo.getText() + '\0';
//  		db.addRecord(recordWithNull.getBytes());
		success = db.addRecord(recordWithNull.getBytes());		
	    }
	    if (checkBar.getState()) {
		recordWithNull = checkBar.getText() + '\0';
//  		db.addRecord(recordWithNull.getBytes());
		success = db.addRecord(recordWithNull.getBytes());	    }
	    if (checkBaz.getState()) {
		recordWithNull = checkBaz.getText() + '\0';
//  		db.addRecord(recordWithNull.getBytes());
		success = db.addRecord(recordWithNull.getBytes());
	    }
	    
	    // Lastly, update our status line
	    header = "Made EzDB: wrote flashId and " +
		(db.getNumberOfRecords() - 1)
		+ " items";

	    db.close();
	    paint();
	}
	else if (exitButton.pressed(x,y)) {
	    System.exit(0);
	}
	else if (checkFoo.pressed(x,y)) {
	    checkFoo.handlePenDown(x,y);
	}
	else if (checkBar.pressed(x,y)) {
	    checkBar.handlePenDown(x,y);
	}
	else if (checkBaz.pressed(x,y)) {
	    checkBaz.handlePenDown(x,y);
	}
    }

    /** need this because CheckBox doesn't have a getState() or
	getText() method.
    */
    public class MyCheckBox extends CheckBox {

	boolean checked = false;
	String label="";

	public MyCheckBox(int x, int y, String label) {
	    super(x, y, label);
	    this.label = label;
	}

	public void handlePenDown(int x, int y) {
	    super.handlePenDown(x, y);
	    checked = !checked;
	}

	public boolean getState() {
	    return checked;
	}

	public String getText() {
	    return label;
	}
    }
}
