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

using namespace stub_ext;

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

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

    static QDBusInterface* m_pAreaDbus;
};

QDBusInterface *AreaInterfaceTest::m_pAreaDbus = nullptr;

TEST_F(AreaInterfaceTest, setFormatCountry)
{
    if (!m_pAreaDbus || !m_pAreaDbus->isValid()) {
       return;
    }
    std::string oldValue = m_pAreaDbus->property("formatCountry").toStringList().at(0).toStdString();
    m_pAreaDbus->call("setFormatCountry", "zh_CN.UTF-8");
    std::string newValue = m_pAreaDbus->property("formatCountry").toStringList().at(0).toStdString();
    ASSERT_STREQ(newValue.c_str(), "zh_CN.UTF-8");
    m_pAreaDbus->call("setFormatCountry", oldValue.c_str());
}

TEST_F(AreaInterfaceTest, setCalendar)
{
    if (!m_pAreaDbus || !m_pAreaDbus->isValid()) {
       return;
    }
    std::string oldValue = m_pAreaDbus->property("calendar").toString().toStdString();
    m_pAreaDbus->call("setCalendar", "lunar");
    std::string newValue = m_pAreaDbus->property("calendar").toString().toStdString();
    ASSERT_STREQ(newValue.c_str(), "lunar");
    m_pAreaDbus->call("setCalendar", oldValue.c_str());
}

TEST_F(AreaInterfaceTest, setFirstDay)
{
    if (!m_pAreaDbus || !m_pAreaDbus->isValid()) {
       return;
    }
    std::string oldValue = m_pAreaDbus->property("firstDay").toString().toStdString();
    m_pAreaDbus->call("setFirstDay", "monday");
    std::string newValue = m_pAreaDbus->property("firstDay").toString().toStdString();
    ASSERT_STREQ(newValue.c_str(), "monday");
    m_pAreaDbus->call("setFirstDay", oldValue.c_str());
}

TEST_F(AreaInterfaceTest, setDateFormat)
{
    if (!m_pAreaDbus || !m_pAreaDbus->isValid()) {
       return;
    }
    std::string oldValue = m_pAreaDbus->property("dateFormat").toString().toStdString();
    m_pAreaDbus->call("setDateFormat", "cn");
    std::string newValue = m_pAreaDbus->property("dateFormat").toString().toStdString();
    ASSERT_STREQ(newValue.c_str(), "cn");
    m_pAreaDbus->call("setDateFormat", oldValue.c_str());
}

TEST_F(AreaInterfaceTest, setTimeFormat)
{
    if (!m_pAreaDbus || !m_pAreaDbus->isValid()) {
       return;
    }
    std::string oldValue = m_pAreaDbus->property("timeFormat").toString().toStdString();
    m_pAreaDbus->call("setTimeFormat", "24");
    std::string newValue = m_pAreaDbus->property("timeFormat").toString().toStdString();
    ASSERT_STREQ(newValue.c_str(), "24");
    m_pAreaDbus->call("setTimeFormat", oldValue.c_str());
}

TEST_F(AreaInterfaceTest, setLanguage)
{
    if (!m_pAreaDbus || !m_pAreaDbus->isValid()) {
       return;
    }
    std::string oldValue = m_pAreaDbus->property("language").toString().toStdString();
    m_pAreaDbus->call("setLanguage", "zh_CN");
    std::string newValue = m_pAreaDbus->property("language").toString().toStdString();
    ASSERT_STREQ(newValue.c_str(), "zh_CN");
    m_pAreaDbus->call("setLanguage", oldValue.c_str());
}

TEST_F(AreaInterfaceTest, setShowLanguageList)
{
    if (!m_pAreaDbus || !m_pAreaDbus->isValid()) {
       return;
    }
    QStringList oldValue = m_pAreaDbus->property("showLanguageList").toStringList();
    QStringList languageList = {"zh_CN", "en"};
    m_pAreaDbus->call("setShowLanguageList", languageList);
    std::string newValue = m_pAreaDbus->property("showLanguageList").toStringList().at(0).toStdString();
    ASSERT_STREQ(newValue.c_str(), "zh_CN");
    m_pAreaDbus->call("setShowLanguageList", oldValue);
}
