-- RLGuiTests.lua
-- Purpose: Existence tests for GUI dialogs, frame, and input actions
-- Author: Ritter

RLGuiTests = {}

local Log = RmLogging.getLogger("RLRM")

--- Run all GUI existence tests
---@param assert function Assert closure from RLTestRunner
function RLGuiTests:runAll(assert)
    Log:info("--- GUI Existence Tests ---")

    -- =========================================================================
    -- Dialog module existence (10 assertions)
    -- =========================================================================

    assert("AnimalAIDialog module exists", AnimalAIDialog ~= nil, "not nil", tostring(AnimalAIDialog))
    assert("AnimalInfoDialog module exists", AnimalInfoDialog ~= nil, "not nil", tostring(AnimalInfoDialog))
    assert("DiseaseDialog module exists", DiseaseDialog ~= nil, "not nil", tostring(DiseaseDialog))
    assert("FileExplorerDialog module exists", FileExplorerDialog ~= nil, "not nil", tostring(FileExplorerDialog))
    assert("ProfileDialog module exists", ProfileDialog ~= nil, "not nil", tostring(ProfileDialog))
    assert("NameInputDialog module exists", NameInputDialog ~= nil, "not nil", tostring(NameInputDialog))
    assert("EarTagColourPickerDialog module exists", EarTagColourPickerDialog ~= nil, "not nil", tostring(EarTagColourPickerDialog))
    assert("AnimalFilterDialog module exists", AnimalFilterDialog ~= nil, "not nil", tostring(AnimalFilterDialog))
    assert("RmMigrationDialog module exists", RmMigrationDialog ~= nil, "not nil", tostring(RmMigrationDialog))
    assert("VisualAnimalsDialog module exists", VisualAnimalsDialog ~= nil, "not nil", tostring(VisualAnimalsDialog))

    -- =========================================================================
    -- Dialog INSTANCE set (10 assertions)
    -- =========================================================================

    assert("AnimalAIDialog.INSTANCE set", AnimalAIDialog.INSTANCE ~= nil, "not nil", tostring(AnimalAIDialog.INSTANCE))
    assert("AnimalInfoDialog.INSTANCE set", AnimalInfoDialog.INSTANCE ~= nil, "not nil", tostring(AnimalInfoDialog.INSTANCE))
    assert("DiseaseDialog.INSTANCE set", DiseaseDialog.INSTANCE ~= nil, "not nil", tostring(DiseaseDialog.INSTANCE))
    assert("FileExplorerDialog.INSTANCE set", FileExplorerDialog.INSTANCE ~= nil, "not nil", tostring(FileExplorerDialog.INSTANCE))
    assert("ProfileDialog.INSTANCE set", ProfileDialog.INSTANCE ~= nil, "not nil", tostring(ProfileDialog.INSTANCE))
    assert("NameInputDialog.INSTANCE set", NameInputDialog.INSTANCE ~= nil, "not nil", tostring(NameInputDialog.INSTANCE))
    assert("EarTagColourPickerDialog.INSTANCE set", EarTagColourPickerDialog.INSTANCE ~= nil, "not nil", tostring(EarTagColourPickerDialog.INSTANCE))
    assert("AnimalFilterDialog.INSTANCE set", AnimalFilterDialog.INSTANCE ~= nil, "not nil", tostring(AnimalFilterDialog.INSTANCE))
    assert("RmMigrationDialog.INSTANCE set", RmMigrationDialog.INSTANCE ~= nil, "not nil", tostring(RmMigrationDialog.INSTANCE))

    -- VisualAnimalsDialog: lazy registration, may not be registered yet
    local vadInstance = VisualAnimalsDialog ~= nil and VisualAnimalsDialog.INSTANCE ~= nil
    assert("VisualAnimalsDialog.INSTANCE set", vadInstance, "not nil",
        vadInstance and "set" or "nil (lazy registration timing)")

    -- =========================================================================
    -- Dialog g_gui registration (10 assertions)
    -- =========================================================================

    local registered

    registered = g_gui.guis ~= nil and g_gui.guis["AnimalAIDialog"] ~= nil
    assert("AnimalAIDialog in g_gui", registered, true, registered)

    registered = g_gui.guis ~= nil and g_gui.guis["AnimalInfoDialog"] ~= nil
    assert("AnimalInfoDialog in g_gui", registered, true, registered)

    registered = g_gui.guis ~= nil and g_gui.guis["DiseaseDialog"] ~= nil
    assert("DiseaseDialog in g_gui", registered, true, registered)

    registered = g_gui.guis ~= nil and g_gui.guis["FileExplorerDialog"] ~= nil
    assert("FileExplorerDialog in g_gui", registered, true, registered)

    registered = g_gui.guis ~= nil and g_gui.guis["ProfileDialog"] ~= nil
    assert("ProfileDialog in g_gui", registered, true, registered)

    registered = g_gui.guis ~= nil and g_gui.guis["NameInputDialog"] ~= nil
    assert("NameInputDialog in g_gui", registered, true, registered)

    registered = g_gui.guis ~= nil and g_gui.guis["EarTagColourPickerDialog"] ~= nil
    assert("EarTagColourPickerDialog in g_gui", registered, true, registered)

    registered = g_gui.guis ~= nil and g_gui.guis["AnimalFilterDialog"] ~= nil
    assert("AnimalFilterDialog in g_gui", registered, true, registered)

    registered = g_gui.guis ~= nil and g_gui.guis["RmMigrationDialog"] ~= nil
    assert("RmMigrationDialog in g_gui", registered, true, registered)

    -- VisualAnimalsDialog: conditional (lazy registration timing)
    local vadRegistered = g_gui.guis ~= nil and g_gui.guis["VisualAnimalsDialog"] ~= nil
    assert("VisualAnimalsDialog in g_gui", vadRegistered, true,
        vadRegistered or "nil (lazy registration timing)")

    -- =========================================================================
    -- RealisticLivestockFrame existence (1 assertion)
    -- Note: Frames loaded with isFrame=true don't appear in g_gui.guis —
    -- they attach to InGameMenu via fixInGameMenu(). Module existence is the proxy.
    -- =========================================================================

    assert("RealisticLivestockFrame module exists", RealisticLivestockFrame ~= nil, "not nil", tostring(RealisticLivestockFrame))

    -- =========================================================================
    -- Input actions (8 assertions)
    -- =========================================================================

    self:testInputActions(assert)
end

--- Test input action existence
---@param assert function Assert closure from RLTestRunner
function RLGuiTests:testInputActions(assert)
    Log:info("--- Input Action Tests ---")

    -- Event-registered action: VisualAnimalsDialog
    local found = false
    if g_inputBinding ~= nil and g_inputBinding.actionEvents ~= nil then
        for action, _ in pairs(g_inputBinding.actionEvents) do
            if action.name == "VisualAnimalsDialog" then found = true; break end
        end
    end
    assert("InputAction VisualAnimalsDialog event registered", found, true, found)

    -- modDesc-defined actions (7)
    local modDescActions = {"RL_OPEN_ANIMAL_SCREEN", "RL_MARK", "RL_MONITOR", "RL_AI", "RL_CASTRATE", "RL_DISEASES", "RL_SELECT"}
    for _, name in ipairs(modDescActions) do
        local exists = InputAction[name] ~= nil
        assert("InputAction." .. name .. " defined", exists, true, exists)
    end
end
