export class Timer {
  private startTime: number;
  private elapsedTime: number;
  private timerId: number | null;
  private isRunning: boolean;

  constructor() {
    this.startTime = 0;
    this.elapsedTime = 0;
    this.timerId = null;
    this.isRunning = false;
  }

  start(): void {
    if (!this.isRunning) {
      this.isRunning = true;
      this.startTime = Date.now() - this.elapsedTime;
      this.timerId = window.setInterval(() => {
        this.elapsedTime = Date.now() - this.startTime;
      }, 10); // Update every 10 milliseconds
    }
  }

  pause(): void {
    if (this.isRunning) {
      this.isRunning = false;
      if (this.timerId !== null) {
        clearInterval(this.timerId);
      }
    }
  }

  resume(): void {
    if (!this.isRunning) {
      this.start();
    }
  }

  resetTime(): void {
    this.elapsedTime = 0;
    this.startTime = Date.now() - this.elapsedTime
  }

  reset(): void {
    this.isRunning = false;
    if (this.timerId !== null) {
      clearInterval(this.timerId);
    }
    this.elapsedTime = 0;
  }

  getTime(): Number {
    return this.elapsedTime / 1000
  }



  private formatTime(ms: number): Object {
    const milliseconds = ms % 1000;
    const seconds = Math.floor(ms / 1000) % 60;
    const minutes = Math.floor(ms / 1000 / 60) % 60;
    const hours = Math.floor(ms / 1000 / 60 / 60);

    // console.log(`${this.pad(hours)}:${this.pad(minutes)}:${this.pad(seconds)}.${this.pad(milliseconds, 3)}`)
    return ms / 1000
  }

  private pad(number: number, digits: number = 2): string {
    return number.toString().padStart(digits, '0');
  }
}
