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

/**
 * This is an classic ... 
 * Change the CounterPanel array in Timex to a Counter array in CounterPanel. 
 * Use setLayout to line up entries. 
 * Move the Timex paint function down to CounterPanel. 
 * Make Counterpanel method that adds total work time from Counter array.
 * Only one counter counts at a time? (correct?) 
 */
public class Timex extends Applet {
  CounterPanel[] jobEntry = new CounterPanel[2];
  public static boolean DEBUG = false; 
  public void init() {
    setLayout(new GridLayout(2,1));
    jobEntry[0] = new CounterPanel("Work"); 
    jobEntry[1] = new CounterPanel("Spare");
    for (int i=0; i<jobEntry.length; i++) {
      add(jobEntry[i]);
    }
  }

  public void start() {
    repaint(1000l); 
  }

  public void paint(Graphics g) {
    
    for (int i=0; i<jobEntry.length; i++) {
      jobEntry[i].paint(g);
    }
    if (Timex.DEBUG) {
      System.err.println(new Date()); 
      repaint(1000l);  // arg is time in miliseconds ( 1,000l = 1 sec)
    }
    else {
      repaint(60000l); // arg is time in miliseconds (60,000l = 1 min)
    }
  }

  public String getAppletInfo() { 
    return "Timex time management tool. Written by Matthias Schwab.";
  }
}

/**
 * This organizes the panel for each job entry.
 * Should be an inner class 
 */
class CounterPanel extends Panel { 
  String   jobName; 
  Counter  counter;
  Label    clock;
  Button   togBut, addBut, subBut; 

  CounterPanel(String jobName) { 
    this.jobName = jobName;
    counter      = new Counter(); 
    setLayout(new GridLayout(1,4));
    togBut = new Button("start " + jobName)            ; add(togBut);
    addBut = new Button("+")                           ; add(addBut);  
    subBut = new Button("-")                           ; add(subBut);  
    clock  = new Label(counter.toString(),Label.CENTER); add(clock); 
  } 

  public void paint(Graphics g) {
    counter.count();
    clock.setText(counter.toString()); 
    clock.paint(g);
  }

  public boolean action (Event e, Object arg) {
    if (e.target == togBut) { 
      if (togBut.getLabel().startsWith("stop")) { 
	togBut.setLabel("start " + jobName); 
	counter.stop(); 
      }
      else {
	togBut.setLabel("stop " + jobName); 
	counter.start(); 
      }
      repaint();    // does not work! maybe listener? maybe single class? 
      return true; 
    }
    if (e.target == addBut) {
      counter.add( 300000l);
      repaint();    // does not work! maybe listener? maybe single class? 
      return true;
    }
    if (e.target == subBut) {
      counter.add(-300000l);
      repaint();
      return true;
    }
    return false; 
  }
}

/** 
 * This counts the time spent on each job entry.
 */ 
class Counter { 
  Date    todayDate, lastCountDate;
  long    workedTime;
  int     workedSecs; // for debugging //
  int     workedMins;
  int     workedHours;
  private boolean isAtWork; 

  Counter() { 
    workedTime  = 0l; 
    workedMins  = 0; 
    workedHours = 0;
    isAtWork    = false;
    lastCountDate = new Date();
    todayDate     = lastCountDate; 
  }

  void add(long addedTime) { 
    workedTime += addedTime; 
  }

  void start() {
    isAtWork = true; 
    count();
  }
  void stop() {
    isAtWork = false; 
  }

  long workedTime() {
    return workedTime; 
  }
  int workedMins() {
    return workedMins; 
  }
  int workedHours() {
    return workedHours; 
  }

  // count work time // 
  void count(){ 
    if (isAtWork) {
      Date nowDate   = new Date(); 
      workedTime    += nowDate.getTime() - lastCountDate.getTime(); 
      lastCountDate  = nowDate; 

      if (nowDate.getDay() != todayDate.getDay()) {
	reset(); 
      }

      // format workTime //
      Date  zeroDate    = new Date(0l);
      Date  workedDate  = new Date(workedTime);
      workedHours = workedDate.getHours()   - zeroDate.getHours();
      workedMins  = workedDate.getMinutes() - zeroDate.getMinutes();
      workedSecs  = workedDate.getSeconds() - zeroDate.getSeconds();
    }
  }

  // reset new day // 
  void reset() {
    // should write workTime out to file // 
    // when quit: just serialize object? //
    todayDate   = new Date();
    workedTime  = 0l; 
  }
  public String toString() {
    if (Timex.DEBUG) {
      System.err.println(" " + workedTime  +  
			 " " + workedHours +
			 " " + workedMins  +
			 " " + workedSecs );
    }
    if (Timex.DEBUG) 
      return new String(workedHours + ":" + workedMins + ":" + workedSecs);
    else 
      return new String(workedHours + ":" + workedMins                   );
  }
}


    
    
