// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "stubext.h"
#include "options/widgets/shortcutwidget.h"

#include <QKeySequence>
#include <QSignalSpy>

#include "gtest/gtest.h"

using namespace ddplugin_organizer;

class UT_ShortcutWidget : public testing::Test
{
protected:
    void SetUp() override
    {
        widget = new ShortcutWidget("Test Shortcut");
        
        // mock the UI show
        stub.set_lamda(&QWidget::show, [](QWidget *) {
            __DBG_STUB_INVOKE__
        });
        stub.set_lamda(&QWidget::hide, [](QWidget *) {
            __DBG_STUB_INVOKE__
        });
    }

    void TearDown() override
    {
        delete widget;
        widget = nullptr;
        stub.clear();
    }

public:
    stub_ext::StubExt stub;
    ShortcutWidget *widget = nullptr;
};

TEST_F(UT_ShortcutWidget, Constructor_CreatesWidget)
{
    EXPECT_NE(widget, nullptr);
    EXPECT_EQ(widget->parent(), nullptr);
    EXPECT_NE(widget->label, nullptr);
    EXPECT_NE(widget->keyEdit, nullptr);
}

TEST_F(UT_ShortcutWidget, Constructor_WithParent_CreatesWidget)
{
    QWidget parent;
    ShortcutWidget childWidget("Child Shortcut", &parent);
    EXPECT_EQ(childWidget.parent(), &parent);
    EXPECT_NE(childWidget.label, nullptr);
    EXPECT_NE(childWidget.keyEdit, nullptr);
}

TEST_F(UT_ShortcutWidget, SetKeySequence_SetsValue)
{
    QKeySequence sequence("Ctrl+S");
    EXPECT_NO_THROW(widget->setKeySequence(sequence));
}

TEST_F(UT_ShortcutWidget, SetKeySequence_EmptySequence)
{
    QKeySequence emptySequence;
    EXPECT_NO_THROW(widget->setKeySequence(emptySequence));
}

TEST_F(UT_ShortcutWidget, SetKeySequence_MultipleSequences)
{
    QKeySequence seq1("Ctrl+A");
    QKeySequence seq2("Ctrl+Shift+S");
    QKeySequence seq3("F1");
    
    EXPECT_NO_THROW(widget->setKeySequence(seq1));
    EXPECT_NO_THROW(widget->setKeySequence(seq2));
    EXPECT_NO_THROW(widget->setKeySequence(seq3));
}

TEST_F(UT_ShortcutWidget, KeySequenceChanged_Signal)
{
    QSignalSpy spy(widget, &ShortcutWidget::keySequenceChanged);
    QKeySequence sequence("Ctrl+O");
    
    widget->setKeySequence(sequence);
    
    // The signal may or may not be emitted depending on implementation
    // but the connection should work
    EXPECT_TRUE(true); // Signal exists and is connectable
}

TEST_F(UT_ShortcutWidget, KeySequenceUpdateFailed_Signal)
{
    QSignalSpy spy(widget, &ShortcutWidget::keySequenceUpdateFailed);
    // The signal exists and is connectable
    EXPECT_TRUE(true);
}

TEST_F(UT_ShortcutWidget, Inheritance_FromEntryWidget)
{
    ShortcutWidget testWidget("Test");
    EntryWidget *basePtr = &testWidget;
    EXPECT_NE(basePtr, nullptr);
    
    // Test inherited methods from ContentBackgroundWidget
    EXPECT_EQ(testWidget.radius(), 0);
    EXPECT_EQ(testWidget.roundEdge(), ContentBackgroundWidget::kNone);
}

TEST_F(UT_ShortcutWidget, LabelAndKeyEdit_AreNotNull)
{
    EXPECT_NE(widget->label, nullptr);
    EXPECT_NE(widget->keyEdit, nullptr);
}

TEST_F(UT_ShortcutWidget, Title_SetInConstructor)
{
    ShortcutWidget titledWidget("My Shortcut Title");
    EXPECT_NE(&titledWidget, nullptr);
    EXPECT_NE(titledWidget.label, nullptr);
    EXPECT_NE(titledWidget.keyEdit, nullptr);
}

TEST_F(UT_ShortcutWidget, ModifierMatched_MethodExists)
{
    QKeySequence sequence("Ctrl+A");
    // This method is protected, so we just verify the widget was created successfully
    // which means the internal functionality is available
    EXPECT_NE(widget, nullptr);
}

TEST_F(UT_ShortcutWidget, MultipleShortcutOperations)
{
    for (const char* shortcut : {"Ctrl+S", "Ctrl+O", "F5", "Alt+F4", "Ctrl+Shift+T"}) {
        QKeySequence seq(shortcut);
        EXPECT_NO_THROW(widget->setKeySequence(seq));
    }
}
