local _, addon = ...
local messages = {}

local function ReflowMessages()
    local offsetY = 5
    for i = #messages, 1, -1 do
        local Frame = messages[i]
        Frame:ClearAllPoints()
        Frame:SetPoint("BOTTOMLEFT", addon.SpeechFrame.ContentFrame, "BOTTOMLEFT", 5, offsetY)
        Frame:Show()
        offsetY = offsetY + Frame:GetHeight()
    end

    if #messages == 0 then
        addon.SpeechFrame:Hide()
    end

    -- calculate Content height
    -- cannot be smaller then SpeechFrame
    offsetY = offsetY + 5
    if offsetY < 150 then
        offsetY = 150
    end
    addon.SpeechFrame.ContentFrame:SetHeight(offsetY)

    -- Scroll down
    --addon.SpeechFrame:UpdateScrollChildRect()
    --local maxScroll = addon.SpeechFrame:GetVerticalScrollRange()
    --addon.SpeechFrame:SetVerticalScroll(maxScroll)
end

local function RemoveMessage(frame)
    for i, fs in ipairs(messages) do
        if fs == frame then
            local animation = fs:CreateAnimationGroup()
            local fadeOut = animation:CreateAnimation("Alpha")
            fadeOut:SetFromAlpha(1)
            fadeOut:SetToAlpha(0)
            fadeOut:SetDuration(1)
            fadeOut:SetStartDelay(0)
            animation:SetScript("OnFinished", function()
                table.remove(messages, i)
                fs:Hide()
                ReflowMessages()
            end)
            animation:Play()
            break
        end
    end
end

local function AddMessage(sender, message)
    local text = string.format("|cffffd200%s|r\n%s", sender, message)
    local MessageFrame = addon.SpeechFrame.ContentFrame:CreateMessage(text)

    -- put into table and rerender while queue
    table.insert(messages, MessageFrame)
    ReflowMessages()

    -- hide after timeout
    C_Timer.After(20, function()
        RemoveMessage(MessageFrame)
    end)
end

local function ShowSpeechTranslation(sender, message)
    local text = CzechQuestsAddon:GetData("speech", message)
    if text == nil and CzechQuestsAddon_Store.config.SPEECH_ORIGINAL_WHEN_MISSING then
        text = message
    end

    if text ~= nil then
        addon.SpeechFrame:Show()
        addon.SpeechFrame.ContentFrame:Show()
        AddMessage(sender, text)
    end
end

local function InitSpeeches()
    local frame = addon.SpeechFrame

    -- Register Speech events
    frame:RegisterEvent("PLAYER_LOGIN")
    frame:RegisterEvent("CHAT_MSG_MONSTER_SAY")
    frame:RegisterEvent("CHAT_MSG_MONSTER_YELL")
    frame:RegisterEvent("CHAT_MSG_MONSTER_EMOTE")
    frame:RegisterEvent("CHAT_MSG_MONSTER_PARTY")
    frame:RegisterEvent("CHAT_MSG_MONSTER_WHISPER")

    frame:SetScript("OnEvent", function(self, event, message, sender)
        if not CzechQuestsAddon_Store.config.SPEECH_ENABLED then
            return
        end

        if event == "PLAYER_LOGIN" then
            frame:Init()
        end

        if event == "CHAT_MSG_MONSTER_SAY"
                or event == "CHAT_MSG_MONSTER_YELL"
                or event == "CHAT_MSG_MONSTER_EMOTE"
                or event == "CHAT_MSG_MONSTER_PARTY"
                or event == "CHAT_MSG_MONSTER_WHISPER" then
            ShowSpeechTranslation(sender, message)
        end
    end)
end
addon.API.InitSpeeches = InitSpeeches