#include <gtest/gtest.h>
#include <gtest/gtest-death-test.h>
#include <QDBusReply>
#include "../../registeredSession/plugins/screensaver/screensaverinterface.h"
#include "stubext.h"

using namespace stub_ext;

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

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

    static QDBusInterface* m_pScreensaverDbus;
};

QDBusInterface *ScreensaverInterfaceTest::m_pScreensaverDbus = nullptr;

TEST_F(ScreensaverInterfaceTest, setPreentryTime)
{
    if (!m_pScreensaverDbus || !m_pScreensaverDbus->isValid()) {
       return;
    }
    int oldValue = m_pScreensaverDbus->property("preentryTime").toInt();
    m_pScreensaverDbus->call("setPreentryTime", 10);
    int newValue = m_pScreensaverDbus->property("preentryTime").toInt();
    ASSERT_EQ(newValue, 10);
    m_pScreensaverDbus->call("setPreentryTime", oldValue);
}

TEST_F(ScreensaverInterfaceTest, setScreensaverType)
{
    if (!m_pScreensaverDbus || !m_pScreensaverDbus->isValid()) {
       return;
    }
    QGSettings gset("org.ukui.screensaver");
    std::string oldValue = m_pScreensaverDbus->property("screensaverType").toString().toStdString();
    m_pScreensaverDbus->call("setScreensaverType", "ukui");
    std::string newValue = m_pScreensaverDbus->property("screensaverType").toString().toStdString();

    ASSERT_STREQ(newValue.c_str(), "ukui");
    m_pScreensaverDbus->call("setScreensaverType", oldValue.c_str());
}

TEST_F(ScreensaverInterfaceTest, setShowBreakTimeUkuiCustom)
{
    if (!m_pScreensaverDbus || !m_pScreensaverDbus->isValid()) {
       return;
    }
    bool oldValue = m_pScreensaverDbus->property("showBreakTimeCustom").toBool();
    m_pScreensaverDbus->call("getShowBreakTimeCustom", true);
    bool newValue = m_pScreensaverDbus->property("showBreakTimeCustom").toBool();
    ASSERT_EQ(newValue, true);
    m_pScreensaverDbus->call("getShowBreakTimeCustom", oldValue);
}

TEST_F(ScreensaverInterfaceTest, setShowBreakTimeUkui)
{
    if (!m_pScreensaverDbus || !m_pScreensaverDbus->isValid()) {
       return;
    }
    bool oldValue = m_pScreensaverDbus->property("showBreakTimeUkui").toBool();
    m_pScreensaverDbus->call("setShowBreakTimeUkui", false);
    bool newValue = m_pScreensaverDbus->property("showBreakTimeUkui").toBool();
    ASSERT_EQ(newValue, false);
    m_pScreensaverDbus->call("setShowBreakTimeUkui", oldValue);
}

TEST_F(ScreensaverInterfaceTest, setScreenLockEnabled)
{
    if (!m_pScreensaverDbus || !m_pScreensaverDbus->isValid()) {
       return;
    }
    bool oldValue = m_pScreensaverDbus->property("screenLockEnabled").toBool();
    m_pScreensaverDbus->call("setScreenLockEnabled", false);
    bool newValue = m_pScreensaverDbus->property("screenLockEnabled").toBool();
    ASSERT_EQ(newValue, false);
    m_pScreensaverDbus->call("setScreenLockEnabled", oldValue);
}

TEST_F(ScreensaverInterfaceTest, setCustomPath)
{
    if (!m_pScreensaverDbus || !m_pScreensaverDbus->isValid()) {
       return;
    }
    std::string oldValue = m_pScreensaverDbus->property("customPath").toString().toStdString();
    m_pScreensaverDbus->call("setCustomPath", "/test/path");
    std::string newValue = m_pScreensaverDbus->property("customPath").toString().toStdString();
    ASSERT_STREQ(newValue.c_str(), "/test/path");
    m_pScreensaverDbus->call("setCustomPath", oldValue.c_str());
}

TEST_F(ScreensaverInterfaceTest, setSwitchRandom)
{
    if (!m_pScreensaverDbus || !m_pScreensaverDbus->isValid()) {
       return;
    }
    bool oldValue = m_pScreensaverDbus->property("switchRandom").toBool();
    m_pScreensaverDbus->call("setSwitchRandom", false);
    bool newValue = m_pScreensaverDbus->property("switchRandom").toBool();
    ASSERT_EQ(newValue, false);
    m_pScreensaverDbus->call("setSwitchRandom", oldValue);
}

TEST_F(ScreensaverInterfaceTest, setCycleTime)
{
    if (!m_pScreensaverDbus || !m_pScreensaverDbus->isValid()) {
       return;
    }
    int oldValue = m_pScreensaverDbus->property("cycleTime").toInt();
    m_pScreensaverDbus->call("setCycleTime", 60);
    int newValue = m_pScreensaverDbus->property("cycleTime").toInt();
    ASSERT_EQ(newValue, 60);
    m_pScreensaverDbus->call("setCycleTime", oldValue);
}


TEST_F(ScreensaverInterfaceTest, setCustomText)
{
    if (!m_pScreensaverDbus || !m_pScreensaverDbus->isValid()) {
       return;
    }
    std::string oldValue = m_pScreensaverDbus->property("customText").toString().toStdString();
    m_pScreensaverDbus->call("setCustomText", "test custom text");
    std::string newValue = m_pScreensaverDbus->property("customText").toString().toStdString();
    ASSERT_STREQ(newValue.c_str(), "test custom text");
    m_pScreensaverDbus->call("setCustomText", oldValue.c_str());
}

TEST_F(ScreensaverInterfaceTest, setCustomTextCentered)
{
    if (!m_pScreensaverDbus || !m_pScreensaverDbus->isValid()) {
       return;
    }
    bool oldValue = m_pScreensaverDbus->property("customTextCentered").toBool();
    m_pScreensaverDbus->call("setCustomTextCentered", false);
    bool newValue = m_pScreensaverDbus->property("customTextCentered").toBool();
    ASSERT_EQ(newValue, false);
    m_pScreensaverDbus->call("setCustomTextCentered", oldValue);
}
