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

package samegame;

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

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

/**
 * Widget to draw same puzzle
 *
 * @author	Carl W. Haynes III
 * @version
 */
public class ScoreWidget extends Widget implements ClockClient {
    static final int SHOWSCORE = 0;
    static final int FLASHNUM = 1;
    int state = SHOWSCORE; 

    // info for flashing number
    int flashnum = 0;
    boolean flashState = true;

    static final int HUNDREDS = 0;
    static final int THOUSANDS = 1;
    int scope = THOUSANDS;

    static Options scopeOptions = new Options();
    static {
    	scopeOptions.add("hundreds", HUNDREDS);
	scopeOptions.add("thousands", THOUSANDS);
    }

    Image num[] = new Image[10];

    final static String	numberFileName[] = {
	"images/0.gif",
	"images/1.gif",
	"images/2.gif",
	"images/3.gif",
	"images/4.gif",
	"images/5.gif",
	"images/6.gif",
	"images/7.gif",
	"images/8.gif",
	"images/9.gif"};


    final static int numWidth = 18;
    final static int numHeight = 24;

    int panelWidth = (18 * ((scope==HUNDREDS)?3:4)) + 10;
    final static int panelHeight = 24;

    private int score = 0;
    private int targetscore = 0;

    /**
     * Default constructor.
     */
    public ScoreWidget() {
    }


    public void init() {
        panelWidth = (18 * ((scope==HUNDREDS)?3:4)) + 10;
    	reshape(x, y, panelWidth, panelHeight);
    	for (int i = 0 ; i < 10 ; i++) {
	    num[i] = getImage(numberFileName[i]);
            if (Toolkit.getDefaultToolkit().prepareImage(num[i], -1, -1, this)) {
            }
	}
    }

    /**
     * Get the properties of this widget.
     */
    public void getProperties(PropertyList list) {
    	super.getProperties(list);
    	list.setOption("scope", scopeOptions, scope, THOUSANDS);
    }

    /**
     * Set the properties of this widget.
     */
    public void setProperties(PropertyList list) {
    	super.setProperties(list);
    	scope = list.getOption("scope", scopeOptions, THOUSANDS);
    }


    /**
      * paint the widget
      */
    public void paint(Graphics g) {
	switch (state) {
	  case SHOWSCORE:
	    paint_showscore(g);
	    break;
	  case FLASHNUM:
	    paint_flashnum(g);
	    break;
	}
    }

    /**
      * paint the widget
      */
    public void paint_flashnum(Graphics g) {
	if (flashState == true) {
	    g.setColor(Color.black);
	    g.fillRect(0, 0, width, height);
	    int thousands = flashnum / 1000;
	    int hundreds = (flashnum - (thousands * 1000)) / 100;
	    int tens = (
			(flashnum - (thousands * 1000))
			- (hundreds * 100)
			   ) / 10;
	    int ones = (
			(flashnum - (thousands * 1000))
			- (hundreds * 100)
			   ) - (tens * 10);

	    int imageLeft = 0;
	    int imageWidth = 0;

	    imageLeft = width - (numWidth + 5);
	    imageWidth = num[ones].getWidth(null);
	    g.drawImage(num[ones], (imageLeft + numWidth) - imageWidth, 0, null);

	    imageLeft -= numWidth;
	    imageWidth = num[tens].getWidth(null);
	    g.drawImage(num[tens], (imageLeft + numWidth) - imageWidth, 0, null);

	    imageLeft -= numWidth;
	    imageWidth = num[hundreds].getWidth(null);
	    g.drawImage(num[hundreds], (imageLeft + numWidth) - imageWidth, 0, null);

	    if (scope==THOUSANDS) {
		imageLeft -= numWidth;
		imageWidth = num[thousands].getWidth(null);
		g.drawImage(num[thousands], (imageLeft + numWidth) - imageWidth, 0, null);
	    }
	    flashState = false;
	}
	else {
	    g.setColor(Color.black);
	    g.fillRect(0, 0, width, height);
	    flashState = true;
	}
    }

    /**
      * paint the widget
      */
    public void paint_showscore(Graphics g) {
        g.setColor(Color.black);
        g.fillRect(0, 0, width, height);

	int thousands = score / 1000;
	int hundreds = (score - (thousands * 1000)) / 100;
	int tens = (
	    (score - (thousands * 1000))
	    - (hundreds * 100)
	    ) / 10;
	int ones = (
	    (score - (thousands * 1000))
	    - (hundreds * 100)
	    ) - (tens * 10);

	int imageLeft = 0;
	int imageWidth = 0;

	imageLeft = width - (numWidth + 5);
	imageWidth = num[ones].getWidth(null);
	g.drawImage(num[ones], (imageLeft + numWidth) - imageWidth, 0, null);

	imageLeft -= numWidth;
	imageWidth = num[tens].getWidth(null);
	g.drawImage(num[tens], (imageLeft + numWidth) - imageWidth, 0, null);

	imageLeft -= numWidth;
	imageWidth = num[hundreds].getWidth(null);
	g.drawImage(num[hundreds], (imageLeft + numWidth) - imageWidth, 0, null);

        if (scope==THOUSANDS) {
    	    imageLeft -= numWidth;
	    imageWidth = num[thousands].getWidth(null);
	    g.drawImage(num[thousands], (imageLeft + numWidth) - imageWidth, 0, null);
        }
    }

    public void setScore(int newScore) {
	if (state == FLASHNUM)
	    stopFlashing();

    	if (score == newScore)
	    return;
	targetscore = newScore;
	if (score>targetscore)
	    score--;
	else
	    score++;
        repaint();
	Clock.master.add(this, System.currentTimeMillis() + 50, null);
    }

    public long tick_flashnum(long tm, Object arg) {
	repaint();
	return System.currentTimeMillis() + 500;
    }

    public long tick_showscore(long tm, Object arg) {
	if (score == targetscore) {
	    //DebugStr("removing clock");
	    Clock.master.remove(this);
	    return(-1);
	}
	if (score>targetscore) {
	    int diff = score-targetscore;
	    if (diff > 100)
		score -= 100;
	    else if (diff > 10)
		score -= 10;
	    else
		score -= 1;
//	    score -= ((score-targetscore)>10)?10:1;
	}
	else {
	    int diff = targetscore-score;
	    if (diff > 100)
		score += 100;
	    else if (diff > 10)
		score += 10;
	    else
		score += 1;
//	    score += ((targetscore-score)>10)?10:1;
	}
	repaint();
	return System.currentTimeMillis() + 50;
    }
    
    public long tick(long tm, Object arg) {
	switch (state) {
	  case SHOWSCORE:
	    return(tick_showscore(tm, arg));
	  case FLASHNUM:
	    return(tick_flashnum(tm, arg));
	}
	return System.currentTimeMillis() + 50;
    }

    /** 
     *start flashing a number
     */
    public void startFlashing(int num) {
	state = FLASHNUM;
	flashnum = num;
	Clock.master.add(this);
	flashState = true;
    }

    /**
     * stop the flashing
     */
    public void stopFlashing() {
	if (state != FLASHNUM)
	    return;

	state = SHOWSCORE;
	Clock.master.remove(this);
	repaint();
    }


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

