// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc4388.utility;

/**
 * <p>Keeps track of Robot times like time passed, delta time, etc
 * <p>All times are in milliseconds
 */
public class RobotTime {
  private long m_currTime = System.currentTimeMillis();
  public long m_deltaTime = 0;

  private long m_startRobotTime = m_currTime;
  public long m_robotTime = 0;
  public long m_lastRobotTime = 0;

  private long m_startMatchTime = 0;
  public  long m_matchTime = 0;
  public long m_lastMatchTime = 0;

  public long m_frameNumber = 0;

  /**
   * Private constructor prevents other classes from instantiating
   */
  private RobotTime(){}

  private static RobotTime instance = null;

  /**
   * Gets the instance of Robot Time. If there is no instance running one will be created.
   * @return instance of Robot Time
   */
  public static RobotTime getInstance() {
    if (instance == null) {
      instance = new RobotTime();
    }
    return instance;
  }

  /**
   * Call this once per periodic loop.
   */
  public void updateTimes() {
    m_lastRobotTime = m_robotTime;
    m_lastMatchTime = m_matchTime;

    m_currTime = System.currentTimeMillis();
    m_robotTime = m_currTime - m_startRobotTime;
    m_deltaTime = m_robotTime - m_lastRobotTime;
    m_frameNumber++;

    if (m_startMatchTime != 0) {
      m_matchTime = m_currTime - m_startMatchTime;
    }
  }

  /**
   * Call this in both the auto and periodic inits
   */
  public void startMatchTime() {
    if (m_startMatchTime == 0) {
      m_startMatchTime = m_currTime;
    }
  }

  /**
   * Call this in disabled init
   */
  public void endMatchTime() {
    m_startMatchTime = 0;
    m_matchTime = 0;
  }
}
