print("[AA Damage] Integrated ChainsawFuel loaded")

local installed = false

local function installIfNeeded()
    if installed then return end
    if g_currentMission == nil then return end

    installed = true

    local mod = ChainsawFuel.new()
    g_currentMission.chainsawFuelMod = mod

    addModEventListener(mod)
    print("[ChainsawFuel] ModEventListener registered (mission ready)")

end

Mission00.loadMapFinished = Utils.appendedFunction(Mission00.loadMapFinished, installIfNeeded)
Mission00.update = Utils.appendedFunction(Mission00.update, installIfNeeded)



ChainsawFuelInputListener = {}

function ChainsawFuelInputListener:mouseEvent(posX, posY, isDown, isUp, button)
    local mod = g_currentMission and g_currentMission.chainsawFuelMod
    if mod ~= nil and mod.mouseEvent ~= nil then
        local ok, handled = pcall(mod.mouseEvent, mod, posX, posY, isDown, isUp, button)
        if ok and handled then
            return true
        end
    end
    return false
end

function ChainsawFuelInputListener:keyEvent(unicode, sym, modifier, isDown)
    return false
end

function ChainsawFuelInputListener:update(dt)
    local mod = g_currentMission and g_currentMission.chainsawFuelMod
    if mod == nil then
        return
    end

    local alt = false
    local h = false

    if Input ~= nil and Input.isKeyPressed ~= nil then
        if Input.KEY_lalt ~= nil and Input.isKeyPressed(Input.KEY_lalt) == true then alt = true end
        if Input.KEY_ralt ~= nil and Input.isKeyPressed(Input.KEY_ralt) == true then alt = true end
        if Input.KEY_h ~= nil and Input.isKeyPressed(Input.KEY_h) == true then h = true end
        if Input.KEY_H ~= nil and Input.isKeyPressed(Input.KEY_H) == true then h = true end
    end

    local combo = alt and h
    if combo and not mod._altHToggle then
        mod.hudMoveEnabled = not (mod.hudMoveEnabled == true)
        mod.draggingHud = false

        if g_inputBinding ~= nil and g_inputBinding.setShowMouseCursor ~= nil then
            pcall(g_inputBinding.setShowMouseCursor, g_inputBinding, mod.hudMoveEnabled)
        end

        if mod.hudMoveEnabled then
            if mod._showInfo ~= nil then
                mod:_showInfo("HUD Move ON")
            end
        else
            if g_currentMission ~= nil then
                g_currentMission.isPlayerFrozen = false
                g_currentMission.isPlayerInputDisabled = false
            end
            if g_localPlayer ~= nil and g_localPlayer.setIsPlayerFrozen ~= nil then
                pcall(g_localPlayer.setIsPlayerFrozen, g_localPlayer, false)
            end
            if mod.saveToSavegame ~= nil then
                pcall(mod.saveToSavegame, mod)
            end
            if mod._showInfo ~= nil then
                mod:_showInfo("HUD Move OFF")
            end
        end
    end
    mod._altHToggle = combo

    if mod.hudMoveEnabled then
        if g_currentMission ~= nil then
            g_currentMission.isPlayerFrozen = true
            g_currentMission.isPlayerInputDisabled = true
        end
        if g_localPlayer ~= nil and g_localPlayer.setIsPlayerFrozen ~= nil then
            pcall(g_localPlayer.setIsPlayerFrozen, g_localPlayer, true)
        end
        if g_inputBinding ~= nil and g_inputBinding.setShowMouseCursor ~= nil then
            pcall(g_inputBinding.setShowMouseCursor, g_inputBinding, true)
        end
    end
end

if g_dedicatedServer == nil then
    addModEventListener(ChainsawFuelInputListener)
end
