// Copyright 1996, Marimba Inc. All Rights Reserved.
// @(#)HighScoreDialog.java, 1.7, 10/20/96


package wordsmith;

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

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

/**
 * Tell the user that there are no more moves available
 *
 * @author	Carl W. Haynes III
 * @version     1.7, 10/20/96
 */
public class HighScoreFrame extends PlayerFrame {
    
    HighScoreManager hsm = null;
    Frame frame;
    Presentation pres;
    int score;
    
    /**
     * Constructor
     */
    public HighScoreFrame(Frame frame, URL gui, HighScoreManager hsm, int score, Presentation pres) {
	 super(gui);
	 this.hsm = hsm;
	 this.frame = frame;
	 this.pres = pres;
	 this.score = score;
	 
	 TextBoxWidget tb = (TextBoxWidget)player.getPresentation().getWidget("name");
	 tb.setText(hsm.lastNameUsed);

	 tb = (TextBoxWidget)player.getPresentation().getWidget("email");
	 tb.setText(hsm.lastEmailUsed);
	 
	 tb = (TextBoxWidget)player.getPresentation().getWidget("score");
	 tb.setText(""+score);
     }

    /**
     * Something went wrong. Show the error message.
     */
    private void error(String msg) {
    	util.setText("message", msg);
	    show();
    }

    public void showHighScoreList(HighScore hs) {
	try {
	    new HighScoreListFrame(new URL(pres.getBase(),
					    "highscorelist.gui"),
				    hsm,
				    hs).show();
	}
	catch(MalformedURLException e) {
	    e.printStackTrace();
	}
    }
    
    public void handleOKButton() {
	TextBoxWidget name_tbw = (TextBoxWidget)player.getPresentation().getWidget("name");
	String name = name_tbw.getText().trim();
	hsm.lastNameUsed = name;
	if (name.equals(""))
	    name = "<empty>";
	TextBoxWidget email_tbw = (TextBoxWidget)player.getPresentation().getWidget("email");
	String email = email_tbw.getText().trim();
	hsm.lastEmailUsed = email;
	if (email.equals(""))
	    email = "<empty>";
	HighScore hs = new HighScore(score, name, email, new Date());
	hsm.insert(hs);

	//showHighScoreList(hs);
	
	dispose();
    }
    
    /**
     * Handle events.
     */
    public boolean handleEvent(Event evt) {
	if (evt.id == Event.ACTION_EVENT) {
	    if (evt.target instanceof CommandButtonWidget) {
		if (evt.target == player.getPresentation().getWidget("dialog-ok")) {
		    handleOKButton();
		    return(true);
		}
		if (((CommandButtonWidget)evt.target).label.equals("dialog-cancel")) {
		    dispose();
		    return(true);
		}
	    }
	}

	return super.handleEvent(evt);
    }
}
