
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.*;
import javax.microedition.media.*;
import java.io.*;

/**
 * 
 */
public class MillionGame extends MIDlet implements CommandListener {

    private Display display;        // Reference to Display object for this MIDlet
    private Form form;              // Main form of the game
    private Command exit;           // Exit the Game
    private Command play;          // Start Playing game
    private Command admin;          // Go to admin Menu
    private Command cancel;         // Go back to home screen
    private Command login;          // Login in the admin Menu
	private Command start;          // Start Game Mode
	private Command addQ;
	private Command insert;
	private Command next;
	private Command finish;
	private TextField q,a,b,c,d,ans; 
	private Command update;
	private Command delete;
	private Command view;
	private StringItem question;
	private ChoiceGroup answers;
	 private ImageItem imageItem;
    private TextField passwrd;         // used to enter password
    private Image img1, img2, img3;      // to append images
   
   // Get the Display to start Midlet
    public MillionGame() {
        display = Display.getDisplay(this);
        // Create the Form, add Commands, listen for events
    }
    public void startApp() {
        mainMenu();
    }
    public void pauseApp() {
    }
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
	public void mainMenu() {
        form = new Form("Millionaire Game");
        exit = new Command("Exit", Command.EXIT, 1);
        play = new Command("Play", Command.SCREEN, 2);
        admin = new Command("Admin Menu", Command.SCREEN, 3);
        form.append("Select Play or Admin from menu.");
        form.addCommand(exit);
        form.addCommand(play);
        form.addCommand(admin);
        form.setCommandListener(this);
        display.setCurrent(form);
    }
//Listen for Command Events
    public void commandAction(Command cm, Displayable s) {
        if (cm == exit) {
            destroyApp(false);
            notifyDestroyed();
        }  else if (cm == play) {
            gameMode();
        }  else if (cm == admin) {
            loginUser();
        } else if (cm == login) {
            allowUser(passwrd.getString());
        }  else if (cm == cancel){
			mainMenu();
		}  else if (cm == addQ) {
			addQuestion();
		}
	}
// Authenticate for Admin Mode
    public void loginUser() {
        form = new Form("Admin Menu Login");
        cancel = new Command("Cancel", Command.CANCEL, 2);
        login = new Command("Login", Command.OK, 2);
        form.addCommand(cancel);
        form.addCommand(login);
        form.setCommandListener(this);
        //form.append("Enter password");
        form.append(passwrd = new TextField("Enter password", "", 4, TextField.PASSWORD));
        display.setCurrent(form);
    }
	public void gameMode() {
        form = new Form("you're playing Millionaire");
		question = new StringItem("Question: ","What is the capital city of Malaysia?");
		answers = new ChoiceGroup("Select to move to the next Q",Choice.EXCLUSIVE);
		answers.append("Kuala Lumpure",null);
		answers.append("Sana'a",null);
		answers.append("Parise",null);
		answers.append("Berlin",null);
        start = new Command("Select", Command.SCREEN, 1);
		cancel = new Command("Cancel", Command.CANCEL, 2);
        form.addCommand(start);
		form.addCommand(cancel);
		form.append(question);
		form.append(answers);
        form.setCommandListener(this);
        display.setCurrent(form);
    }
	public void allowUser(String password) {
        if (password.equals("1234")) {
            adminMenu();
        } else {
            tryAgain();
        }
	}
	public void adminMenu() {
        form = new Form("Weclome to Admin Menu");
        cancel = new Command("Game Menu", Command.CANCEL, 1);
        addQ = new Command("Add", Command.SCREEN, 2);
        update = new Command("Update", Command.SCREEN, 3);
        delete = new Command("Delete", Command.SCREEN, 4);
        view = new Command("View", Command.SCREEN, 5);
        form.append("- Select from menu to Add, Update or Delete Questions.\n");
        form.append("- Select the View option to observe the changes at anytime.");
        form.addCommand(cancel);
        form.addCommand(addQ);
        form.addCommand(update);
        form.addCommand(delete);
        form.addCommand(view);
        form.setCommandListener(this);
        display.setCurrent(form);
    }
	public void tryAgain() 
	{
        Alert error = new Alert("Incorrect Login", "Please try again", null, AlertType.ERROR);
        error.setTimeout(3000);
        passwrd.setString("");
        display.setCurrent(error, form);
		//mainMenu();
    }
	// Display the Add a Question Screen
    public void addQuestion() {
        form = new Form("Add Quesion");
        insert = new Command("Insert", Command.SCREEN, 1);
        next = new Command("Next", Command.SCREEN, 2);
        finish = new Command("Finish", Command.SCREEN, 3);
        form.addCommand(insert);
        form.addCommand(finish);
        form.addCommand(next);
        form.setCommandListener(this);
        form.append(q = new TextField("Q:", "", 60, TextField.ANY));
        form.append(a = new TextField("A.", "", 12, TextField.ANY));
        form.append(b = new TextField("B.", "", 12, TextField.ANY));
        form.append(c = new TextField("C.", "", 12, TextField.ANY));
        form.append(d = new TextField("D.", "", 12, TextField.ANY));
        form.append(ans = new TextField("Answer:", "", 12, TextField.ANY));
        display.setCurrent(form);
    }
    
}