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

local GAP = 2

local function ShowSpeech(Frame, offsetY)
    Frame:ClearAllPoints()
    Frame:SetPoint("BOTTOMLEFT", addon.SpeechFrame, "BOTTOMLEFT", 0, offsetY)
    Frame:SetHeight(Frame.Message:GetStringHeight() + 10)
    return Frame
end

local function ReflowMessages(requery)
    local offsetY = 0

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

    if #messages == 1 then
        local Frame = messages[1]
        ShowSpeech(Frame, offsetY)
        Frame:Show()
        local height = Frame.Message:GetStringHeight()
        if height == 0 and not requery then
            Frame:Hide()
            C_Timer.After(0.5, function()
                ReflowMessages(true)
            end)
            return
        end
    end

    for i = #messages, 1, -1 do
        local Frame = messages[i]
        ShowSpeech(Frame, offsetY)
        offsetY = offsetY + Frame:GetHeight() + GAP
    end
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)
    addon.SpeechFrame:Show()

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

    table.insert(messages, MessageFrame)
    ReflowMessages()

    C_Timer.After(20, function()
        RemoveMessage(MessageFrame)
    end)
end
addon.API.AddSpeechMessage = AddMessage

local function ShowSpeechTranslation(sender, message)
    local text = CzechQuestsAddon:GetData("speech", sender .. "_" .. message)
    if text == nil and CzechQuestsAddon_Store.config.SPEECH_ORIGINAL_WHEN_MISSING then
        text = message
    end
    if text ~= nil then
        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