// Copyright 1996, Marimba Inc. All Rights Reserved.
// @(#)TextBoxWidget.java, 1.55, 27 Aug 1996

package samegame;

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

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

/**
 * Widget to draw same puzzle
 *
 * @author	Carl W. Haynes III
 * @version
 */
public class SameGameWidget extends GroupWidget {

    TileWidget tileWidget = null;

    Button newGameButton = null;
    Button undoGameButton = null;
    Button redoGameButton = null;

    HighScoreManager highScores;
    
    /**
     * Default constructor.
     */
    public SameGameWidget() {
    }

    /*
     * init
     */
    public void init() {
	tileWidget = (TileWidget)getWidget("tilewidget");

    }

    /** starting up - get the high score manager*/
    public void start() {
        PlayerPanel pp = getPlayerPanel();
        if (pp == null) {
            //DebugStr("pp == null");
            return;
        }

	// need to do this to view app in builder
	if (!(getPlayerPanel().getParent() instanceof SamegameApplication))
	    return;

	SamegameApplication app = (SamegameApplication)pp.getParent();
	if (app == null)
	    return;
	
	highScores = app.getHighScoreManager();
	if (highScores != null) {
	    tileWidget.setHighScoreManager(highScores);
	}
    }

    /**
      * paint
      */
    public void paint(Graphics g) {
	super.paint(g);
	//        g.setColor(background);
	//        Bevel.fillRect(g, 0, 0, width, height, false, 2);
    }


    /*
     * set the flashing
     */
    public void startFlashing(int i) {
        ScoreWidget sw = (ScoreWidget)getWidget("score");
	if (sw != null)
	    sw.startFlashing(i);
    }

    /*
     * stop the flashing
     */
    public void stopFlashing() {
        ScoreWidget sw = (ScoreWidget)getWidget("score");
	if (sw != null)
	    sw.stopFlashing();
    }

    /*
     * set the score
     */
    public void setScore(int i) {
        ScoreWidget sw = (ScoreWidget)getWidget("score");
	if (sw != null)
	    sw.setScore(i);
    }

    /*
     * set the tiles left
     */
    public void setTilesLeft(int i) {
        ScoreWidget sw = (ScoreWidget)getWidget("tiles");
	    if (sw != null)
	        sw.setScore(i);
    }

    /*
     * enable or disable the undo button
     */
    public void setUndoButtonState(boolean enabled) {
        CommandButtonWidget cbw = (CommandButtonWidget)getWidget("undo");
	if (cbw != null)
	    cbw.disable(!enabled);

	// need to do this to view app in builder
	Object o = getPlayerPanel().getParent();
	if (o instanceof SamegameApplication)
	    ((SamegameApplication)getPlayerPanel().getParent()).setUndoMenuState(enabled);
    }

    public void setRedoButtonState(boolean enabled) {
        CommandButtonWidget cbw = (CommandButtonWidget)getWidget("redo");
	if (cbw != null)
	    cbw.disable(!enabled);

	// need to do this to view app in builder
	Object o = getPlayerPanel().getParent();
	if (o instanceof SamegameApplication)
	    ((SamegameApplication)getPlayerPanel().getParent()).setRedoMenuState(enabled);
    }

    public void newGame() {
        if (tileWidget!=null) {
            tileWidget.newGame();
	        setScore(0);
	        setTilesLeft(200);
        }
    }

    public void undoButton() {
	tileWidget.handleUndo();
    }

    public void redoButton() {
	tileWidget.handleRedo();
    }

    /*
     * read current state from disk
     */
    public void readState(FastInputStream fis) {
	tileWidget.readState(fis);
    }

    /*
     * write current state to disk
     */
    public void writeState(FastOutputStream fos) {
	tileWidget.writeState(fos);
    }

    public void setHighScoreManager(HighScoreManager hsm) {
    }

    public void DebugStr(String s) {
        System.out.println("[" + (new Date()).toString() + "] " + ": SameGameWidget: " + s);
        System.out.flush();
    }
}

