#include <QtTest>
#include <QSignalSpy>
#include <QGuiApplication>
#include "../../../src/model/widget-delegate.h"

class widgetDelegate_test : public QObject
{
    Q_OBJECT

private slots:
    void test_defaultQuickWidgetId();
    void test_defaultPreferredWidth();
    void test_defaultPreferredHeight();
    void test_quickWidgetId_returnsSet();
    void test_quickWidgetIdChanged_signalEmitted();
    void test_preferredWidthChanged_signalExists();
    void test_preferredHeightChanged_signalExists();
    void test_multipleInstances_independent();
    void test_defaultQuickWidgetId_isEmpty();
    void test_preferredWidth_nonNegative();
    void test_preferredHeight_nonNegative();
};

void widgetDelegate_test::test_defaultQuickWidgetId()
{
    UkuiShortcut::WidgetDelegate delegate;
    QCOMPARE(delegate.quickWidgetId(), QString());
}

void widgetDelegate_test::test_defaultPreferredWidth()
{
    UkuiShortcut::WidgetDelegate delegate;
    QCOMPARE(delegate.preferredWidth(), 0.0);
}

void widgetDelegate_test::test_defaultPreferredHeight()
{
    UkuiShortcut::WidgetDelegate delegate;
    QCOMPARE(delegate.preferredHeight(), 0.0);
}

void widgetDelegate_test::test_quickWidgetId_returnsSet()
{
    UkuiShortcut::WidgetDelegate delegate;
    // setQuickWidgetId triggers widget loading; test only getter after explicit set via property
    // We verify initial state is empty string
    QVERIFY(delegate.quickWidgetId().isEmpty());
}

void widgetDelegate_test::test_quickWidgetIdChanged_signalEmitted()
{
    UkuiShortcut::WidgetDelegate delegate;
    QSignalSpy spy(&delegate, &UkuiShortcut::WidgetDelegate::quickWidgetIdChanged);
    delegate.setQuickWidgetId(QStringLiteral("testWidget"));
    QVERIFY(spy.count() >= 1);
}

void widgetDelegate_test::test_preferredWidthChanged_signalExists()
{
    UkuiShortcut::WidgetDelegate delegate;
    // Verify the signal exists and can be connected
    QSignalSpy spy(&delegate, &UkuiShortcut::WidgetDelegate::preferredWidthChanged);
    QVERIFY(spy.isValid());
}

void widgetDelegate_test::test_preferredHeightChanged_signalExists()
{
    UkuiShortcut::WidgetDelegate delegate;
    QSignalSpy spy(&delegate, &UkuiShortcut::WidgetDelegate::preferredHeightChanged);
    QVERIFY(spy.isValid());
}

void widgetDelegate_test::test_multipleInstances_independent()
{
    UkuiShortcut::WidgetDelegate d1;
    UkuiShortcut::WidgetDelegate d2;
    QCOMPARE(d1.quickWidgetId(), d2.quickWidgetId());
    QCOMPARE(d1.preferredWidth(), d2.preferredWidth());
    QCOMPARE(d1.preferredHeight(), d2.preferredHeight());
}

void widgetDelegate_test::test_defaultQuickWidgetId_isEmpty()
{
    UkuiShortcut::WidgetDelegate delegate;
    QVERIFY(delegate.quickWidgetId().isEmpty());
}

void widgetDelegate_test::test_preferredWidth_nonNegative()
{
    UkuiShortcut::WidgetDelegate delegate;
    QVERIFY(delegate.preferredWidth() >= 0.0);
}

void widgetDelegate_test::test_preferredHeight_nonNegative()
{
    UkuiShortcut::WidgetDelegate delegate;
    QVERIFY(delegate.preferredHeight() >= 0.0);
}

QTEST_MAIN(widgetDelegate_test)
#include "widget-delegate-test.moc"
