package stopwatch;


import java.lang.*;
import javafx.stage.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Timer;


/**
 * @author Jasper
 */
public class StopwatchModel {

    public var handAngle:Number = 180;
    public var tenthsHandAngle:Number = 180;
    public var minutesHandAngle:Number = 180;
    public var timeString:String = "00:00.00";

    /**
     * The number of milliseconds which have elapsed while the stopwatch has
     * been running. That is, it is the total time kept on the stopwatch.
     */
     var elapsedMillis:Integer = 0;
    /**
     * Keeps track of the amount of the clock time (CPU clock) when the
     * stopwatch run plunger was pressed, or when the last tick even occurred.
     * This is used to calculate the elapsed time delta.
     */
     var lastClockTime:Integer = 0;
     var timerListener:ActionListener = ActionListener {
        override public function actionPerformed(evt:ActionEvent): Void {
            if (lastClockTime == 0) lastClockTime = System.currentTimeMillis() as Integer;
            var now:Integer = System.currentTimeMillis() as Integer;
            var delta = now - lastClockTime;
            elapsedMillis += delta;
            var elapsedHundredthsSecond:Integer = elapsedMillis/10;

            var hundredthsExact:Number = (elapsedMillis/10.0) mod 10;
            var tenthsExact:Number = (elapsedMillis/100.0) mod 100;

            var tenths:Integer = (elapsedHundredthsSecond/10) mod 10;
            var seconds:Integer = (elapsedHundredthsSecond/100) mod 60;

            handAngle = 180 + ((360/60.0)*seconds);
            tenthsHandAngle = 180 + ((360/10.0)*tenthsExact);
            minutesHandAngle = 180 + ((360/600.0)*seconds);

            var decimalSeconds:Number = (elapsedHundredthsSecond/100.0) mod 60.0;
            var mins:Integer = elapsedHundredthsSecond/6000;

            timeString = "{%02d mins}:{%05.2f decimalSeconds}";
            
            lastClockTime = now;
        }
    }
     var timer:Timer = new Timer(47, timerListener);

    public function startStop(){
        // if started
        if (timer.isRunning()){
            timer.stop();
            lastClockTime = 0;
        } else {
            lastClockTime = System.currentTimeMillis() as Integer;
            timer.start();
        }
    }

    public function reset(){
        if (timer.isRunning()){
            // if started, stop it
            timer.stop();
        } else {
            // if stopped, reset it
            lastClockTime = 0;
            elapsedMillis = 0;
        }
        // update the model
        timerListener.actionPerformed(null);
    }
}