package de.hinterseher.applets;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;

/**
 * Description: Applet to write a String once a day
 * @author  Michael Hinterseher look <a href="http://www.hinterseher.de">http://www.hinterseher.de</a>
 */

public class QuotationOfTheDay extends Applet {
  boolean isStandalone = false;
  /** Position iterator started*/
  int m_iStartPosi;
  /** Day iterator started*/
  GregorianCalendar m_StartDate;
  /** Container with Strings to be shown */
  Vector m_Quotations;

  /**
   *  Description:  Fill the Container with Strings to be shown
   */

  private void fillContainer(){
    m_Quotations.addElement("Zitat 1 - Die echten Zitate habe ich gelöscht");
    m_Quotations.addElement("Zitat 2 - Die echten Zitate habe ich gelöscht");
    m_Quotations.addElement("Zitat 3 - Die echten Zitate habe ich gelöscht");
  }

  /**
   *  Description:  Write the String to the Applet
   *  @param  g The Graphics, the text is written to
   */

  public void paint(Graphics g){
    Font font = new Font("SansSerif", 0, 14);
    g.setFont(font);
    g.drawString(getStringOfDay(), 2, 16);
  }

  /**
   *  Description:  Get the String of the day out of m_Quotations Container
   *                Iterator is started on m_StartDate at position m_StartPosi and iterates each day one position.
   *                If end of container is reached Iterator starts at beginning.
   *  @return String of the day
   */

  protected String getStringOfDay(){
    return (String) m_Quotations.elementAt(currentIterPosi(m_Quotations.size()));
  }

  /**
   *  Description:   Return the position of Iterator in array, depending on current date.
   *                 Iterate on element each day.
   *  Preconditions: m_StartDate set
   *                 m_iStartPosi set
   *  @param         iContainerSize number of Elements to calculate modulo (wrap around)
   *  @return        The position in array depending on current date.
   */

  private int currentIterPosi(int iContainerSize){
    int iCurrentPosi = 0;
    GregorianCalendar currentDate = new GregorianCalendar();
    if (iContainerSize == 0){
      return 0;
    }
    iCurrentPosi = (int) (m_iStartPosi + daysSinceStart()) % iContainerSize;
    return iCurrentPosi;
  }

   /**
   *  Description:   Return the number of days risen to current date since Iterator started
   *  Preconditions: m_StartDate set
   *  @return        Number of days risen to current date since Iterator started.
   */

  private long daysSinceStart(){
    long lTime;                              // time in milliseconds between m_StartDate and now.
    long lDaysSinceStart;
    Calendar toDay = Calendar.getInstance();

    lTime = toDay.getTime().getTime() - m_StartDate.getTime().getTime();
    lDaysSinceStart = lTime / 86400000;
    return lDaysSinceStart;
  }

  /**Get a parameter value*/
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  /**Construct the applet*/
  public QuotationOfTheDay() {
    // Iterator was initially started on position 0
    m_iStartPosi = 0;
    m_StartDate = new GregorianCalendar();
    // Iterator was initially started on 2000-12-25
    m_StartDate.set(2000, 11, 25);
    m_Quotations = new Vector();
  }

  /**Initialize the applet*/
  public void init() {
    fillContainer();
  }

  /**Get Applet information*/
  public String getAppletInfo() {
    return "This applet iterates over some Strings and showes one a day\n http://www.hinterseher.de";
  }

  /**Get parameter info*/
  public String[][] getParameterInfo() {
    return null;
  }
}