/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2024 KylinSoft  Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#include <gtest/gtest.h>
#include <gtest/gtest-death-test.h>
#include <QGSettings>
#include "../../registeredSession/plugins/wallpaper/wallpaperinterface.h"

class WallpaperInterfaceTest : public testing::Test
{
protected:
    static void SetUpTestSuite()
    {
        m_pWallpaperInterface = new WallpaperInterface();
    }

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

    static WallpaperInterface* m_pWallpaperInterface;
};

WallpaperInterface *WallpaperInterfaceTest::m_pWallpaperInterface = nullptr;

TEST_F(WallpaperInterfaceTest, getWallpaperMode)
{
    QGSettings gset("org.mate.background");
    QString oldValue = gset.get("picture-filename").toString();
    m_pWallpaperInterface->getWallpaper();
    std::string curValue = m_pWallpaperInterface->getWallpaperMode().toStdString();
    if (oldValue.isEmpty()) {
        ASSERT_STREQ(curValue.c_str(), "color");
    } else {
        ASSERT_STREQ(curValue.c_str(), "picture");
    }
}

TEST_F(WallpaperInterfaceTest, setWallpaperPicture)
{
    QGSettings gset("org.mate.background");
    QString oldValue = gset.get("picture-filename").toString();
    QString mode = "picture";
    QString colorData = "";
    if (oldValue.isEmpty()) {
	mode = "color";
    	colorData = gset.get("primary-color").toString();
    }
    std::string wallpaperMode = m_pWallpaperInterface->getWallpaperMode().toStdString();
    m_pWallpaperInterface->setWallpaper("picture", "/usr/share/backgrounds/city.jpg");
    std::string newValue = gset.get("picture-filename").toString().toStdString();
    ASSERT_STREQ(newValue.c_str(), "/usr/share/backgrounds/city.jpg");
    if (mode == "color") {
        m_pWallpaperInterface->setWallpaper("color", colorData.toStdString().c_str());
    } else {
        m_pWallpaperInterface->setWallpaper("picture", oldValue.toStdString().c_str());
    }
}

TEST_F(WallpaperInterfaceTest, setWallpaperColor)
{
    QGSettings gset("org.mate.background");
    QString oldValue = gset.get("picture-filename").toString();
    QString mode = "picture";
    QString colorData = "";
    if (oldValue.isEmpty()) {
	mode = "color";
    	colorData = gset.get("primary-color").toString();
    }
    std::string wallpaperMode = m_pWallpaperInterface->getWallpaperMode().toStdString();
    m_pWallpaperInterface->setWallpaper("color", "#2d7d9a");
    std::string newValue = gset.get("primary-color").toString().toStdString();
    ASSERT_STREQ(newValue.c_str(), "#2d7d9a");
    if (mode == "color") {
        m_pWallpaperInterface->setWallpaper("color", colorData.toStdString().c_str());
    } else {
        m_pWallpaperInterface->setWallpaper("picture", oldValue.toStdString().c_str());
    }
}

TEST_F(WallpaperInterfaceTest, setDisplayType)
{
    std::string oldType = m_pWallpaperInterface->getDisplayType().toStdString();
    m_pWallpaperInterface->setDisplayType("centered");
    QGSettings gset("org.mate.background");
    std::string newType = gset.get("picture-options").toString().toStdString();
    ASSERT_STREQ(newType.c_str(), "centered");
    m_pWallpaperInterface->setDisplayType(oldType.c_str());
}

TEST_F(WallpaperInterfaceTest, resetDefault)
{
    QString oldType = m_pWallpaperInterface->getDisplayType();
    QGSettings gset("org.mate.background");
    std::string oldPicName = gset.get("picture-filename").toString().toStdString();

    m_pWallpaperInterface->resetDefault();

    std::string newType = m_pWallpaperInterface->getDisplayType().toStdString();
    ASSERT_STREQ(newType.c_str(), "scaled");

    std::string newPicName = gset.get("picture-filename").toString().toStdString();
    ASSERT_STREQ(newPicName.c_str(), "/usr/share/backgrounds/1-openkylin.jpg");

    m_pWallpaperInterface->setDisplayType(oldType);
    gset.set("picture-filename", oldPicName.c_str());
}

TEST_F(WallpaperInterfaceTest, getPreviewWallpapers)
{
    int size = m_pWallpaperInterface->property("previewWallpapers").toStringList().size();
    ASSERT_GE(size, 0);
}

TEST_F(WallpaperInterfaceTest, getSourceWallpapers)
{
    int size = m_pWallpaperInterface->property("sourceWallpapers").toStringList().size();
    ASSERT_GE(size, 0);
}
