---@class GW2
local GW = select(2, ...)
--/dump  C_UIWidgetManager.GetScenarioHeaderTimerWidgetVisualizationInfo(5865)
local function TerminateTimer(timerBlock)
    if timerBlock and timerBlock.ticker then
        timerBlock.ticker:Cancel()
        timerBlock.ticker = nil
    end
end
GW.TerminateScenarioWidgetTimer = TerminateTimer

local function TimerFunction(timerBlock, widgetId)
    local widget = C_UIWidgetManager.GetScenarioHeaderTimerWidgetVisualizationInfo(widgetId)
    if widget and widget.timerValue ~= widget.timerMax then
        timerBlock.timer:SetValue(ceil(widget.timerValue / widget.timerMax))
        timerBlock.timerString:SetText(SecondsToClock(widget.timerValue, false))
    else
        timerBlock.height = 1
        timerBlock.timer:Hide()
        TerminateTimer(timerBlock)
        GwQuesttrackerContainerScenario:UpdateLayout()
    end
end

local function addEventTimerBarByWidgetId(timerBlock, gwQuestTrackerTimerSavedHeight, showTimerAsBonus, widgetId, useRealTimer)
    local widget = C_UIWidgetManager.GetScenarioHeaderTimerWidgetVisualizationInfo(widgetId)
    if widget then
        if widget.shownState ~= Enum.WidgetShownState.Hidden and widget.timerMax > 0 and widget.timerValue < widget.timerMax and widget.timerValue > 1 then
            if useRealTimer then -- here we need to add a real timer
                if timerBlock.ticker then
                    timerBlock.ticker:Cancel()
                end
                timerBlock.ticker = C_Timer.NewTicker(0.25, function() TimerFunction(timerBlock, widgetId) end)
            else
                timerBlock.timer:SetValue(ceil(widget.timerValue / widget.timerMax))
                timerBlock.timerString:SetText(SecondsToClock(widget.timerValue, false))
            end
            timerBlock.timer:Show()
            gwQuestTrackerTimerSavedHeight = gwQuestTrackerTimerSavedHeight + 40
            showTimerAsBonus = true
        else
            gwQuestTrackerTimerSavedHeight = 1
            timerBlock.height = 1
            timerBlock.timer:Hide()
            showTimerAsBonus = false
            TerminateTimer(timerBlock)
        end
    else
        gwQuestTrackerTimerSavedHeight = 1
        timerBlock.timer:SetShown(timerBlock.needToShowTimer)
        if not timerBlock.needToShowTimer then
            timerBlock.height = 1
        end
        showTimerAsBonus = false
        TerminateTimer(timerBlock)
    end

    return gwQuestTrackerTimerSavedHeight, showTimerAsBonus
end
GW.addEventTimerBarByWidgetId = addEventTimerBarByWidgetId
