// DigitalClockDisplay class, September/October 2001
//
// Designed for Brian's Superbly Incredible and Well-Commented Non-Functional
// Clock.
//
// Objects of this class display six Digits and can be combined with a
// DigitalClockFace to make a DigitalClock.

import aLibrary.*;
import java.awt.*;

public class DigitalClockDisplay extends AView {
// DigitalClockDisplay combines objects on an AView

  /*
   * CLASS VARIABLES
   */

  private Digit[] digit; // array of six Digits
  private ARectangle[] colon; // two ARectangles for the colon

  /*
   * CONSTRUCTOR
   */

  public DigitalClockDisplay(int x, int y, int w, int h) {
  // Instantiates a new DigitalClockDisplay.
  //
  // Preconditions:
  //   none
  //
  // Postconditions:
  //   A new DigitalClockDisplay is created.
  //   The various components are placed on this.

    super(x, y, w, h);  // AView constructor

    digit = new Digit[6];
    // set up array for Digits

    double xUnit = w / 123d;
    double yUnit = h / 21d;
    int stdY = (int)((4 * yUnit) + 0.5);
    int bigW = (int)((12 * xUnit) + 0.5);
    int smallW = (int)((6 * xUnit) + 0.5);
    int bigH = (int)((13 * yUnit) + 0.5);
    int smallH = (int)((6.5 * yUnit) + 0.5);
    // Compute often-used numbers to make things easier for us

    digit[0] = new Digit((int)((12 * xUnit) + 0.5), stdY, bigW, bigH);
    digit[1] = new Digit((int)((30 * xUnit) + 0.5), stdY, bigW, bigH);
    digit[2] = new Digit((int)((60 * xUnit) + 0.5), stdY, bigW, bigH);
    digit[3] = new Digit((int)((78 * xUnit) + 0.5), stdY, bigW, bigH);
    digit[4] = new Digit((int)((96 * xUnit) + 0.5), stdY, smallW, smallH);
    digit[5] = new Digit((int)((105 * xUnit) + 0.5), stdY, smallW, smallH);
    // Create new Digits

    for (int i = 0; i < 6; i++)
      digit[i].place(this);
    // place Digits on this

    colon = new ARectangle[2];
    // set up array for colon

    for (int i = 0; i < 2; i++) {
      colon[i] = new ARectangle((int)((49.5 * xUnit) + 0.5),
        (int)((7 * yUnit) + (i * 5.5 * yUnit) + 0.5),
        (int)((3 * xUnit) + 0.5),
        (int)((1.5 * yUnit) + 0.5)); // nasty formula here too
      colon[i].setToFill();
      colon[i].setColor(Color.red);
      colon[i].place(this);
    }
    // Draw and place colon

  }

  /*
   * UPDATE METHODS
   */

  public void magicallyAppear(double time) {
  // Causes a time to magically appear on the display. Oh, oh, it's magic...
  //
  // Precondition:
  //   0 <= time < 86400
  //
  // Postcondition:
  //   The time is magically redrawn, but not repainted.

    // Display the first digit:
    if (Timekeeper.getDigit(time, 0, false) == 0)
      // We don't like zeros, so make it blank instead.
      digit[0].blank();
    else
      digit[0].setNumber(Timekeeper.getDigit(time, 0, false));

    // Display the last five digits:
    for (int i = 1; i < 6; i++)
      digit[i].setNumber(Timekeeper.getDigit(time, i, false));

  }

}