/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                         *
 *  Copyright (C) 2015 Simon Stürz <simon.stuerz@guh.io>                   *
 *                                                                         *
 *  This file is part of nymea.                                            *
 *                                                                         *
 *  This library is free software; you can redistribute it and/or          *
 *  modify it under the terms of the GNU Lesser General Public             *
 *  License as published by the Free Software Foundation; either           *
 *  version 2.1 of the License, or (at your option) any later version.     *
 *                                                                         *
 *  This library is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      *
 *  Lesser General Public License for more details.                        *
 *                                                                         *
 *  You should have received a copy of the GNU Lesser General Public       *
 *  License along with this library; If not, see                           *
 *  <http://www.gnu.org/licenses/>.                                        *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "countdown.h"
#include "extern-plugininfo.h"

Countdown::Countdown(const QString &name, const QTime &time, const bool &repeating, QObject *parent) :
    QObject(parent),
    m_name(name),
    m_time(time),
    m_currentTime(time),
    m_repeating(repeating)
{
    m_timer = new QTimer(this);
    m_timer->setInterval(1000);
    m_timer->setSingleShot(false);

    connect(m_timer, &QTimer::timeout, this, &Countdown::onTimeout);
}

void Countdown::start()
{
    qCDebug(dcDateTime) << name() << "start" << m_currentTime.toString();
    m_currentTime = m_time;
    m_timer->start();
    m_running = true;
    emit runningStateChanged(true);
}

void Countdown::stop()
{
    m_timer->stop();
    qCDebug(dcDateTime) << name() << "stop" << m_currentTime.toString();
    m_running = false;
    emit runningStateChanged(false);
}

void Countdown::restart()
{
    m_currentTime = m_time;
    qCDebug(dcDateTime) << name() << "restart" << m_currentTime.toString();
    m_timer->start();
    m_running = true;
    emit runningStateChanged(true);
}

QString Countdown::name() const
{
    return m_name;
}

bool Countdown::running() const
{
    return m_running;
}

bool Countdown::repeating() const
{
    return m_repeating;
}

QTime Countdown::time() const
{
    return m_time;
}

QTime Countdown::currentTime() const
{
    return m_currentTime;
}

void Countdown::onTimeout()
{
    m_currentTime = m_currentTime.addSecs(-1);
    //qCDebug(dcDateTime) << name() << m_currentTime.toString();
    if (m_currentTime == QTime(0,0)) {
        qCDebug(dcDateTime) << name() << "countdown timeout.";
        if (m_repeating) {
            m_currentTime = m_time;
        } else {
            m_timer->stop();
            m_running = false;
            emit runningStateChanged(false);
        }
        emit countdownTimeout();
    }
}
