#pragma once

namespace hr
{
  class real_time
  {
    public:
      real_time(): m_sec{ 0 }, m_nsec{ 0 } {}
      real_time(int s, int n);
      real_time(const real_time &r) : m_sec{r.m_sec }, m_nsec{ r.m_nsec } { }

    public:
      real_time operator+(const real_time &r) const { return real_time(m_sec + r.m_sec, m_nsec + r.m_nsec); }
      real_time operator-(const real_time &r) const { return real_time(m_sec - r.m_sec, m_nsec - r.m_nsec); }
      real_time operator-() const { return real_time(-m_sec, -m_nsec); }
      bool operator <(const real_time &r) const { if (m_sec == r.m_sec) return m_nsec < r.m_nsec; else return m_sec < r.m_sec; }

    public:
      static real_time frame2rt(long frame, unsigned int sample_rate);

    public:
      int sec() const { return m_sec; }
      int msec() const { return m_nsec / 1'000'000; }
      int nsec() const { return m_nsec; }

    public:
      static const int ONE_BILLION { 1'000'000'000 };
      static const real_time zero;

    private:
      int m_sec;
      int m_nsec;

    private:
      void normalize() {
        if (m_nsec >= ONE_BILLION) 
        {
          m_sec += m_nsec / ONE_BILLION;
          m_nsec %= ONE_BILLION;
        } 
        else if (m_nsec < 0) 
        {
          m_sec += (m_nsec / ONE_BILLION) - 1; // Учитываем округление
          m_nsec = (m_nsec % ONE_BILLION + ONE_BILLION) % ONE_BILLION; // Приводим к положительному значению
        }
      }
  };
}
