/*
 * Score - Scoring machinery and display
 *
 * by Adam Doppelt
 * http://www.cs.brown.edu/people/amd/
 */
import java.awt.*;

public class Score extends MirrorCanvas {
    static int points_, level_, left_;
    static Score score_;

    private final static String ABOUT =
         "PipeDream v0.91, by Adam Doppelt";
    
    static int y_ = 15;
    static int x1_, x2_, x3_;
    
    private final static int HEIGHT = 20;

    private final static int SCORE_INC = 200;
    private final static int CROSS_INC = 500;
    private final static int REPLACE_INC = -300;    
    
    private final static int SECOND = 10;
    
    private final static int L_1 = 10;
    private final static int L_2 = 40;    
    private final static float L_S = ((float)L_2 - (float)L_1) / (float)SECOND;
    
    private final static int I_1 = 20000;
    private final static int I_2 = 8000;    
    private final static float I_S = ((float)I_2 - (float)I_1) / (float)SECOND;
    
    private final static int W_1 = 80;
    private final static int W_2 = 30;    
    private final static float W_S = ((float)W_2 - (float)W_1) / (float)SECOND;

    public Score(Container container) {
	super(container.size().width, HEIGHT);
	container.add("North", this);
    }

    public void paint(Graphics g) {
	if (offscreen_ == null) {
	    super.paint(g);
	    
	    Font font = new Font("Helvetica", Font.PLAIN, 12);
	    onscreen_.setFont(font);
	    offscreen_.setFont(font);	
	
	    x1_ = 10;
	    x2_ = width_ / 2 - 50;
	    x3_ = width_ - 80;
	
	    score_ = this;
	    points_ = 0;
	    Reset();
	    offscreen_.setColor(Color.yellow);
	    int x = (int)((width_ -
			   getFontMetrics(font).stringWidth(ABOUT)) / 2.0);
	    offscreen_.drawString(ABOUT, x, y_);
	}
	else
	    super.paint(g);
    }

    public static void Reset() {
	level_ = -1;
	points_ = 0;
	Update();
    }
    
    public static void AdvanceLevel() {
	level_++;
	left_ = Math.min((int)((float)L_1 + (float)level_ * L_S), L_2);
	Update();
    }

    public static void AnotherPiece() {
	if (left_ > 0)
	    left_--;
	points_ += SCORE_INC;
	Update();
    }

    public static void CompletedCross() {
	points_ += CROSS_INC;
	Update();
    }
    
    public static void Replaced() {
	points_ += REPLACE_INC;
	Update();
    }


    public static boolean EnoughForNext() {
	return (left_ == 0);
    }

    public static int GetInitialDelay() {
	return Math.max((int)((float)I_1 + (float)level_ * I_S), I_2);
    }

    public static int GetWaterDelay() {
	return Math.max((int)((float)W_1 + (float)level_ * W_S), W_2);	
    }

    
    public static void Update() {
	score_.offscreen_.setColor(Color.black);
	score_.offscreen_.fillRect(0, 0, score_.width_, score_.height_);
	if (level_ != -1) {
	    score_.offscreen_.setColor(Color.yellow);
	    score_.offscreen_.drawString("Level: " + (level_ + 1), x1_, y_);
	    score_.offscreen_.drawString("Score: " + points_, x3_, y_);
	    if (left_ > 0)
		score_.offscreen_.drawString("Remaining: " + left_, x2_, y_);
	}
	score_.repaint();
    }
}
