// Copyright 1996, Marimba Inc. All Rights Reserved.
// @(#)WordSmithApplication.java, 1.6, 11/21/96

package wordsmith;

import java.net.*;
import java.io.*;
import java.awt.*;
import java.util.*;

import marimba.channel.*;
import marimba.gui.*;
import marimba.io.*;
import marimba.desktop.*;
import marimba.persist.*;

/**
 * Comment here
 *
 * @author      Carl W. Haynes III
 * @version     1.6, 11/21/96
 */

public class WordSmithApplication extends ApplicationPlayerFrame implements Application {

    /** high scores */
    HighScoreManager highScores;

    AppMenuBar mb;
    Menu fileMenu;
    Menu editMenu;
    Menu optionsMenu;
    AppMenuItem undoMenu = null;
    AppMenuItem redoMenu = null;

    static final String highScoreFile = "hsm.txt";
    static final String stateFile = "state.txt";

    /**
     * Create a new ApplicationPlayerFrame.
     */
    public WordSmithApplication() {
	super();
	setMenuBar(mb = new AppMenuBar());
	fileMenu = new Menu("File");
	mb.add(fileMenu);
	fileMenu.add(new AppMenuItem("New Game", null, "New Game", "New Game"));
	fileMenu.add(new AppMenuItem("-", "-", "-", "-"));
	fileMenu.add(new AppMenuItem("Quit", null, "Quit", "Quit"));
	
//	mb.add(editMenu = new Menu("Edit"));
//	editMenu.add(undoMenu = new AppMenuItem("Undo", null, "Undo", "Undo"));
//	editMenu.add(redoMenu = new AppMenuItem("Redo", null, "Redo", "Redo"));
	
	mb.add(optionsMenu = new Menu("Options"));
	optionsMenu.add(new AppMenuItem("High Scores...", null, "High Scores", "High Scores"));

	mb.add(optionsMenu = new Menu("Help"));
	optionsMenu.add(new AppMenuItem("Playing WordSmith...", null, "Playing WordSmith", "Playing WordSmith"));
	optionsMenu.add(new AppMenuItem("About WordSmith...", null, "About WordSmith", "About WordSmith"));
    }

    /**
     * Initialize the Channel.
     */
    public void start() {
        super.start();

	highScores.setContext(context);
	highScores.readLocalHighScores();
	highScores.readGlobalHighScores();

	readState();
	show();
    }

    /**
     * Destroy the Channel.
     */
    public void stop() {
	highScores.writeLocalHighScores();
	writeState();
	hide();
	dispose();
    }


    public void showHighScoreList() {
	try {
	    new HighScoreListFrame(new URL(context.getBase(),
					    "highscorelist.gui"),
				    highScores,
				    null);
	}
	catch(MalformedURLException e) {
	    e.printStackTrace();
	}
    }
    
    /**
     * This methods is called by the system to initialize the Application.
     * It will try to find and load the 'presentation' specified in
     * the channel properties.
     */
    public void setContext(ApplicationContext context) {
        super.setContext(context);
	
	highScores = new HighScoreManager();


	WordSmithWidget wsw = (WordSmithWidget)util.getWidget("wordsmith");
	wsw.setContext(context);
	wsw.setHighScoreManager(highScores);
    }

    /**
     * Updated data is available. By default, if property "update.action"
     * is "restart", then the entire channel is restarted (with the new
     * updated data).
     */
    public void notifyAvailable(String dir) {
	if ("restart".equals(context.getParameter("update.action"))) {
	    context.restart();
	}
	
	// force the install
	context.installData(dir);
    }
    
    /**
     * Data has been installed. Called to notify the application
     * that new data has been installed. This is only called if
     * the application explicitly requests installation of new data
     * using installChannel().
     */
    public void notifyInstall(String dir) {
	// handle new top 10 scores here
	highScores.readGlobalHighScores();
    }

    /**
     * Event handler for Window Management events: Asks for the
     * channel to be stopped (and soon destroyed) when the presentation
     * is closed.
     */
    public boolean handleEvent(Event evt) {
	switch (evt.id) {
	  case Event.ACTION_EVENT:
	    if ((""+evt.arg).equalsIgnoreCase("quit")) {
		context.stop();
	    }
	    else if ((""+evt.arg).equalsIgnoreCase("high scores...")) {
		showHighScoreList();
	    }
	    else if ((""+evt.arg).equalsIgnoreCase("New Game")) {
		WordSmithWidget wsw = (WordSmithWidget)util.getWidget("wordsmith");
		wsw.newGame();
	    }
	    else if ((""+evt.arg).equalsIgnoreCase("Undo")) {
	    }
	    else if ((""+evt.arg).equalsIgnoreCase("Redo")) {
	    }
	    else if ((""+evt.arg).equalsIgnoreCase("Playing WordSmith...")) {
		try {
		    PlayerFrame pf = new PlayerFrame();
		    pf.util.setPresentation(new URL(context.getBase(), "help.gui"));
		    pf.show();
		}
		catch(MalformedURLException e) {
		    e.printStackTrace();
		}   
	    }
	    else if ((""+evt.arg).equalsIgnoreCase("About WordSmith...")) {
		try {
		    PlayerFrame pf = new PlayerFrame();
		    pf.util.setPresentation(new URL(context.getBase(), "about.gui"));
		    pf.show();
		}
		catch(MalformedURLException e) {
		    e.printStackTrace();
		}	    
	    }
	    return true;

	  case DATA_AVAILABLE_EVENT:
	    notifyAvailable((String)evt.arg);
	    return true;
	    
	  case DATA_INSTALLED_EVENT:
    	    notifyInstall((String)evt.arg);
	    return true;

	  case Event.WINDOW_DESTROY:
	    context.stop();
	    return true;
	}
	return super.handleEvent(evt);
    }


    /**
     * able/disable undo menu
     */
    public void setUndoMenuState(boolean state) {
	undoMenu.enable(state);
    }

    /**
     * able/disable redo menu
     */
    public void setRedoMenuState(boolean state) {
	redoMenu.enable(state);
    }

    /** 
     * write current state to disk
     */
    public void writeState() {
    }

    /** 
     * read last state from disk
     */
    public void readState() {
    }

    /**
     * for debugging
     */
    public void DebugStr(String s) {
        System.out.println(this.getClass().getName() + ": " + s);
    }
    
}

