#include <QtTest>
#include <QStandardItemModel>

#include "../../../src/notification/popup-notification-model.h"
#include "../../../src/notification/notification-model.h"
#include "stubs/global-settings.h"
#include "stubs/screen-monitor.h"

using namespace UkuiNotification;

class TestPopupNotificationModel : public PopupNotificationModel
{
    Q_OBJECT

public:
    using PopupNotificationModel::filterAcceptsRow;
};

class popupNotificationModel_test : public QObject
{
    Q_OBJECT

private slots:
    void init();
    void test_filterRejectsWhenTabletMode();
    void test_filterRejectsWhenSessionToolsVisible();
    void test_filterRejectsStoredNotification();
    void test_filterHonorsLockScreenVisibility();
    void test_valueChange_updatesFilter();
    void test_sessionToolsChange_updatesFilter();
};

static void setPopupRow(QStandardItemModel &model, int row, bool isStored, bool showOnLockScreen)
{
    model.insertRow(row);
    QModelIndex index = model.index(row, 0);
    model.setData(index, isStored, NotificationItem::IsStored);
    model.setData(index, showOnLockScreen, NotificationItem::ShowOnLockScreen);
}

void popupNotificationModel_test::init()
{
    Sidebar::GlobalSettings::globalInstance()->setValue(TABLET_MODE, false);
    Sidebar::ScreenMonitor::getInstance()->setScreenLockState(false);
    Sidebar::ScreenMonitor::getInstance()->setSessionToolsState(false);
}

void popupNotificationModel_test::test_filterRejectsWhenTabletMode()
{
    QStandardItemModel sourceModel(1, 1);
    setPopupRow(sourceModel, 0, false, true);

    TestPopupNotificationModel model;
    model.setSourceModel(&sourceModel);
    Sidebar::GlobalSettings::globalInstance()->setValue(TABLET_MODE, true);

    QCOMPARE(model.filterAcceptsRow(0, QModelIndex()), false);
}

void popupNotificationModel_test::test_filterRejectsWhenSessionToolsVisible()
{
    QStandardItemModel sourceModel(1, 1);
    setPopupRow(sourceModel, 0, false, true);

    Sidebar::ScreenMonitor::getInstance()->setSessionToolsState(true);
    TestPopupNotificationModel model;
    model.setSourceModel(&sourceModel);

    QCOMPARE(model.filterAcceptsRow(0, QModelIndex()), false);
}

void popupNotificationModel_test::test_filterRejectsStoredNotification()
{
    QStandardItemModel sourceModel(2, 1);
    setPopupRow(sourceModel, 0, true, true);
    setPopupRow(sourceModel, 1, false, true);

    TestPopupNotificationModel model;
    model.setSourceModel(&sourceModel);

    QCOMPARE(model.filterAcceptsRow(0, QModelIndex()), false);
    QCOMPARE(model.filterAcceptsRow(1, QModelIndex()), true);
}

void popupNotificationModel_test::test_filterHonorsLockScreenVisibility()
{
    QStandardItemModel sourceModel(2, 1);
    setPopupRow(sourceModel, 0, false, false);
    setPopupRow(sourceModel, 1, false, true);

    TestPopupNotificationModel model;
    model.setSourceModel(&sourceModel);
    Sidebar::ScreenMonitor::getInstance()->setScreenLockState(true);

    QCOMPARE(model.filterAcceptsRow(0, QModelIndex()), false);
    QCOMPARE(model.filterAcceptsRow(1, QModelIndex()), true);
}

void popupNotificationModel_test::test_valueChange_updatesFilter()
{
    QStandardItemModel sourceModel;
    setPopupRow(sourceModel, 0, false, true);

    PopupNotificationModel model;
    model.setSourceModel(&sourceModel);

    QCOMPARE(model.rowCount(), 1);
    Sidebar::GlobalSettings::globalInstance()->setValue(TABLET_MODE, true);
    QCOMPARE(model.rowCount(), 0);
}

void popupNotificationModel_test::test_sessionToolsChange_updatesFilter()
{
    QStandardItemModel sourceModel;
    setPopupRow(sourceModel, 0, false, true);

    PopupNotificationModel model;
    model.setSourceModel(&sourceModel);

    QCOMPARE(model.rowCount(), 1);
    Sidebar::ScreenMonitor::getInstance()->setSessionToolsState(true);
    QCOMPARE(model.rowCount(), 0);
    Sidebar::ScreenMonitor::getInstance()->setSessionToolsState(false);
    QCOMPARE(model.rowCount(), 1);
}

QTEST_MAIN(popupNotificationModel_test)

#include "popup-notification-model-test.moc"
