#ifndef ATB_TIME_H_INCLUDED
#define ATB_TIME_H_INCLUDED

#include <QDateTime>

class ATBTime {
    static QDateTime const m_end;
    mutable QDateTime m_time;

public:
    explicit ATBTime();
    explicit ATBTime(int h, int m, int s = 0, int ms = 0);
    explicit ATBTime(QString const &time);
    explicit ATBTime(QTime const &time);

    explicit ATBTime(ATBTime const &atbTime) {
        m_time = atbTime.m_time;
    }

    ATBTime &operator=(ATBTime && atbTime) {
        m_time = std::move(atbTime.m_time);
        return *this;
    }

    ATBTime &operator=(ATBTime const &atbTime) {
        m_time = atbTime.m_time;
        return *this;
    }


    int hour() const { return m_time.time().hour(); }
    int minute() const { return m_time.time().minute(); }
    int second() const { return m_time.time().second(); }
    int msec() const { return m_time.time().msec(); }

    int secsTo(QString const &t) const {
        if (t == "24:00:00") {
            return m_time.secsTo(m_end);
        }
        return m_time.time().secsTo(QTime::fromString(t, Qt::ISODate));
    }

    int msecsTo(QTime t) const { return m_time.time().msecsTo(t); }

    bool setHMS(int h, int m, int s, int ms = 0);

    bool isNull() const { return m_time.time().isNull(); }
    bool isValid() const { return m_time.time().isValid(); }


    QTime addMSecs(int ms) const;
    QTime addMSecs(int ms);

    QTime addSecs(int s) const;
    QTime addSecs(int s);

    int msecsSinceStartOfDay() const;
    QString toString(Qt::DateFormat format = Qt::TextDate) const;


    static bool isValid(int h, int m, int s, int ms = 0);

    static QTime currentTime() { return QDateTime::currentDateTime().time(); }

    static constexpr QTime fromMSecsSinceStartOfDay(int msecs);

    static QTime fromString(QString const &string, Qt::DateFormat format = Qt::TextDate);
    static QTime fromString(QString const &string, const QString &format);

    friend bool operator!=(const ATBTime &lhs, const ATBTime &rhs) noexcept;
    friend bool operator<(const ATBTime &lhs, const ATBTime &rhs) noexcept;
    friend bool operator<=(const ATBTime &lhs, const ATBTime &rhs) noexcept;
    friend bool operator>=(const ATBTime &lhs, const ATBTime &rhs) noexcept;
    friend bool operator<(const ATBTime &lhs, const ATBTime &rhs) noexcept;
    friend bool operator>(const ATBTime &lhs, const ATBTime &rhs) noexcept;
    friend bool operator==(const ATBTime &lhs, const ATBTime &rhs) noexcept;
    friend QDataStream &operator<<(QDataStream &out, ATBTime const &time);
    friend QDebug &operator<<(QDebug &out, ATBTime const &time);
    friend QDataStream &operator>>(QDataStream &in, ATBTime &time);
};


#endif // ATB_TIME_H_INCLUDED