---@diagnostic disable: undefined-field, undefined-global
---@type string
local AddonName = ...
---@class Private
local Private = select(2, ...)
local wt = Private.Ace
---@class WTAPI
local WTAPI = WTAPI

local Media = LibStub("LibSharedMedia-3.0")

local ad_frame = nil
local ad_frame_rotateAndZoom_ticker = nil
local ad_frame_rotateAndZoom_Ticker_time = 0
local ad_frame_pulseTicker = nil
local ad_frame_pulseTicker_time = 0

local timers = {}
local ad_sounds = {
    ["Ad1"] = "Interface\\AddOns\\WildThings\\Media\\Sounds\\Misc\\ad1.ogg",
}

for k, v in pairs(ad_sounds) do
    Media:Register("sound", k, v)
end

local ad_images = {
    [1] = {
        ["path"] = "Interface\\AddOns\\%s\\Media\\Images\\Misc\\ad1",
        ["width"] = 348,
        ["height"] = 300,
    },
}

function wt:ActivateAd(duration)
    if ad_frame and ad_frame:IsShown() then return end

    PlaySoundFile("Interface\\AddOns\\WildThings\\Media\\Sounds\\Misc\\ad1.ogg", "Master")

    if not ad_frame then
        ad_frame = CreateFrame("Frame") -- parent frame
        ad_frame:SetSize(236, 400)
        ad_frame:SetPoint("CENTER", UIParent, "CENTER", 20, 60)
        ad_frame:SetFrameStrata("MEDIUM")
        ad_frame.Texture = ad_frame:CreateTexture() -- region to hold the texture
        ad_frame.Texture:SetAllPoints()
        ad_frame.Texture:SetTexture((ad_images[1].path):format(AddonName))
    else
        ad_frame:SetShown(true)
    end

    -- Rotate and Zoom in (Ease In-Out)
    if not ad_frame_rotateAndZoom_ticker then
        ad_frame_rotateAndZoom_ticker = C_Timer.NewTicker(0.01, function()
            ad_frame_rotateAndZoom_Ticker_time = ad_frame_rotateAndZoom_Ticker_time + 0.05  -- Geschwindigkeit der Welle
            -- Zoom in from very small to normal scale
            local scale = 0.1 + 0.9 * math.sin(ad_frame_rotateAndZoom_Ticker_time * 0.25)
            ad_frame:SetScale(scale)
            local angle = ad_frame_rotateAndZoom_Ticker_time * 360 / 2 -- 2 Sekunden für eine volle Umdrehung
            ad_frame.Texture:SetRotation(math.rad(angle-15))
        end)
    end

    -- Timer zum Stoppen
    table.insert(
        timers,
        C_Timer.NewTimer(tonumber(2), function()
            if ad_frame_rotateAndZoom_ticker then
                ad_frame_rotateAndZoom_ticker:Cancel()
                ad_frame_rotateAndZoom_ticker = nil
            end
            -- Zoom (Ease In-Out)
            if not ad_frame_pulseTicker then
                ad_frame_pulseTicker = C_Timer.NewTicker(0.01, function()
                    ad_frame_pulseTicker_time = ad_frame_pulseTicker_time + 0.05  -- Geschwindigkeit der Welle
                    local scale = 1 + 0.05 * math.sin(ad_frame_pulseTicker_time * 2 * math.pi / 2) -- 2 Sekunden für eine volle Umdrehung
                    ad_frame:SetScale(scale)
                end)
            end
        end)
    )

    table.insert(
        timers,
        C_Timer.NewTimer(tonumber(duration), function()
            wt:StopAd()
        end)
    )
end

function wt:StopAd()
    ad_frame:SetShown(false)
    ad_frame:SetScale(0.1)
    ad_frame_rotateAndZoom_Ticker_time = 0
    ad_frame_pulseTicker_time = 0
    if ad_frame_pulseTicker then
        ad_frame_pulseTicker:Cancel()
        ad_frame_pulseTicker = nil
    end
    if ad_frame_rotateAndZoom_ticker then
        ad_frame_rotateAndZoom_ticker:Cancel()
        ad_frame_rotateAndZoom_ticker = nil
    end
end













