#include <gtest/gtest.h>
#include <gtest/gtest-death-test.h>
#include <QDBusReply>
#include <QStringList>
#include "../../registeredSession/plugins/datetime/datetimeinterface.h"
#include "stubext.h"

using namespace stub_ext;

class DatetimeInterfaceTest : public testing::Test
{
protected:
    static void SetUpTestSuite()
    {
	m_pDatetimeDbus = new QDBusInterface("org.ukui.ukcc.session",
			"/Datetime",
			"org.ukui.ukcc.session.Datetime",
                        QDBusConnection::sessionBus());
    }

    static void TearDownTestSuite()
    {
	if (m_pDatetimeDbus) {
	    delete m_pDatetimeDbus;
	    m_pDatetimeDbus = nullptr;
	}
    }

    static QDBusInterface* m_pDatetimeDbus;
};

QDBusInterface *DatetimeInterfaceTest::m_pDatetimeDbus = nullptr;

TEST_F(DatetimeInterfaceTest, setTimezone)
{
    if (!m_pDatetimeDbus || !m_pDatetimeDbus->isValid()) {
       return;
    }
    std::string oldValue = m_pDatetimeDbus->property("timezone").toStringList().at(0).toStdString();
    m_pDatetimeDbus->call("setTimezone", "Asia/Shanghai");
    std::string newValue = m_pDatetimeDbus->property("timezone").toStringList().at(0).toStdString();
    ASSERT_STREQ(newValue.c_str(), "Asia/Shanghai");
    m_pDatetimeDbus->call("setTimeZone", oldValue.c_str());
}

TEST_F(DatetimeInterfaceTest, setTimeMode)
{
    if (!m_pDatetimeDbus || !m_pDatetimeDbus->isValid()) {
       return;
    }
    std::string oldValue = m_pDatetimeDbus->property("timeMode").toString().toStdString();
    m_pDatetimeDbus->call("setTimeMode", "automatic");
    std::string newValue = m_pDatetimeDbus->property("timeMode").toString().toStdString();
    ASSERT_STREQ(newValue.c_str(), "automatic");
    m_pDatetimeDbus->call("setTimeMode", oldValue.c_str());
}

TEST_F(DatetimeInterfaceTest, setNtpServer)
{
    if (!m_pDatetimeDbus || !m_pDatetimeDbus->isValid()) {
       return;
    }
    std::string oldValue = m_pDatetimeDbus->property("ntpServer").toString().toStdString();
    m_pDatetimeDbus->call("setNtpServer", "");
    std::string newValue = m_pDatetimeDbus->property("ntpServer").toString().toStdString();
    ASSERT_STREQ(newValue.c_str(), "");
    m_pDatetimeDbus->call("setNtpServer", oldValue.c_str());
}

TEST_F(DatetimeInterfaceTest, setOtherTimezones)
{
    if (!m_pDatetimeDbus || !m_pDatetimeDbus->isValid()) {
       return;
    }
    QStringList oldValue = m_pDatetimeDbus->property("otherTimezones").toStringList();
    QStringList otherTimezone = {"Asia/Urumqi"};
    m_pDatetimeDbus->call("setOtherTimezones", otherTimezone);
    QStringList newValue = m_pDatetimeDbus->property("otherTimezones").toStringList();
    ASSERT_STREQ(newValue.at(0).toStdString().c_str(), "Asia/Urumqi");
    m_pDatetimeDbus->call("setOtherTimezones", oldValue);
}

TEST_F(DatetimeInterfaceTest, timezoneName)
{
    if (!m_pDatetimeDbus || !m_pDatetimeDbus->isValid()) {
       return;
    }
    QDBusReply<QStringList> ret = m_pDatetimeDbus->call("timezoneName", "Asia/Urumqi");
    if (ret.isValid()) {
       std::string timezoneName = ret.value().at(0).toStdString();
       ASSERT_STREQ(timezoneName.c_str(), "Asia/Urumqi");
    }
}
