package io.schinzel.basicutils.timekeeper;

import io.schinzel.basicutils.thrower.Thrower;
import lombok.Getter;
import lombok.experimental.Accessors;

/**
 * The purpose of this class act as a StopWatch, no more no less.
 *
 * @author schinzel
 */
@Accessors(prefix = "m")
class StopWatch {
    /** The time when the lap started */
    private long mLapStartTime = 0L;
    /** Number of laps measured. */
    @Getter private int mNumberOfLaps = 0;
    /** The time of all laps. */
    private long mSumTimeOfAllLaps;
    /** The current state of this stop watch. */
    private State mState = State.STOPPED;

    private enum State {
        STARTED, STOPPED
    }


    private StopWatch() {
    }


    /**
     * @return A new instance of the StopWatch.
     */
    static StopWatch create() {
        return new StopWatch();
    }


    /**
     * Start measuring the time of a lap.
     *
     * @return This for chaining.
     */
    StopWatch start() {
        Thrower.throwIfTrue(this.isStarted(), "Cannot start as is already started");
        mLapStartTime = System.nanoTime();
        mState = State.STARTED;
        return this;
    }


    /**
     * Stop the measurement of a lap.
     *
     * @return This for chaining.
     */
    StopWatch stop() {
        Thrower.throwIfFalse(this.isStarted(), "Cannot stop as is not started");
        long currentLapTime = System.nanoTime() - mLapStartTime;
        mState = State.STOPPED;
        mSumTimeOfAllLaps += currentLapTime;
        mLapStartTime = 0;
        mNumberOfLaps++;
        return this;
    }


    /**
     * @return True if is currently in mid lap, else false.
     */
    boolean isStarted() {
        return (mState == State.STARTED);
    }


    /**
     * @return The average lap time in ms.
     */
    double getAvgInMs() {
        double avg = mSumTimeOfAllLaps / mNumberOfLaps * 1d;
        //Convert nanos to millis
        return avg / 1_000_000;
    }


    /**
     * @return The total time of all laps measured.
     */
    double getTotTimeInMs() {
        //Convert from nanos to millis
        return mSumTimeOfAllLaps / 1_000_000;
    }


}

@@ -1822,16 +1822,25 @@
 le avg =
+ (double)
  mSumTim
@@ -1869,13 +1869,8 @@
 Laps
- * 1d
 ;%0A  
@@ -1923,32 +1923,33 @@
  avg / 1_000_000
+D
 ;%0A    %7D%0A%0A%0A    /*
@@ -2093,16 +2093,25 @@
   return
+ (double)
  mSumTim
@@ -2132,16 +2132,17 @@
 _000_000
+D
 ;%0A    %7D%0A

Handle int division