local L = LibStub("AceLocale-3.0"):GetLocale("CritMatic")
local MESSAGE_SPACING = 3
local MAX_MESSAGES = 4
local activeMessages = {}
--/run CritMatic.ShowNewCritMessage("Killing Spree", 300)CritMatic.ShowNewNormalMessage("Killing Spree",435)

-- Utility function to adjust message positions
local function AdjustMessagePositions()
  -- Position the first message at the top
  activeMessages[1]:SetPoint("CENTER", UIParent, "CENTER", 0, 350)

  -- Position subsequent messages relative to the previous one
  for i = 2, #activeMessages do
    activeMessages[i]:SetPoint("TOP", activeMessages[i - 1], "BOTTOM", 0, -MESSAGE_SPACING)
  end
end




-- Utility function to remove the oldest message and adjust the rest
local function RemoveOldestMessage()
  local oldestMessage = table.remove(activeMessages)  -- Remove the oldest message (first in the table)

  -- Hide the message frame and perform any necessary cleanup
  if oldestMessage then
    oldestMessage:Hide()
    -- Any additional cleanup logic, if needed
  end
end

Critmatic.MessageFrame = {}

function Critmatic.MessageFrame:CreateMessage(text, r, g, b)
  local delayInSeconds = 0.45
  local function delayedExecution()
    -- Replace frame creation with a call to CreateNewMessageFrame()
    local f = Critmatic.CreateNewMessageFrame()

    -- Set the text and color
    f.text:SetText(text)
    f.text:SetTextColor(r, g, b)

    -- Create or update the fade-out animation (if it's not already in CreateNewMessageFrame)
    f.fadeOut = f:CreateAnimationGroup()
    local fade = f.fadeOut:CreateAnimation("Alpha")
    fade:SetFromAlpha(1)
    fade:SetToAlpha(0)
    fade:SetDuration(0.5)
    fade:SetStartDelay(7.5)
    f.fadeOut:SetScript("OnFinished", function()
      f:Hide()
    end)

    -- Play the animations
    f.bounce:Play()
    f.fadeOut:Play()

    -- Insert the new message at the beginning
    table.insert(activeMessages, 1, f)

    -- Adjust positions of all messages
    AdjustMessagePositions()

    -- Remove the oldest message if there are too many
    if #activeMessages > MAX_MESSAGES then
      RemoveOldestMessage()
    end

    return f
  end

  -- Delay the execution using C_Timer
  C_Timer.After(delayInSeconds, delayedExecution)
end

function Critmatic.ShowNewHealCritMessage(spellName, amount)
  local messageType = L["message_log_crit"] .." "..L["message_log_heal"]
  if string.find(spellName, L["message_log_heal"].."$") then
    messageType = "Crit" -- Remove "Heal" from the message type if the spell name ends with "Heal"
  end
  local message = string.upper(string.format(L["message_log_new"].."%s %s: %d!", spellName, messageType, amount))
  local r, g, b = unpack(Critmatic.db.profile.fontSettings.fontColorCrit)
  Critmatic.MessageFrame:CreateMessage(message, r, g, b)  -- Gold color
end

function Critmatic.ShowNewHealMessage(spellName, amount)
  local messageType = L["message_log_heal"]
  if string.find(spellName, L["message_log_heal"].."$") then
    messageType = "" -- Remove "Heal" from the message type if the spell name ends with "Heal"
  end
  local message = string.upper(string.format(L["message_log_new"].."%s %s: %d!", spellName, messageType, amount))
  local r, g, b = unpack(Critmatic.db.profile.fontSettings.fontColor)
  Critmatic.MessageFrame:CreateMessage(message, r, g, b)
end



function Critmatic.ShowNewCritMessage(spellName, amount)

  local message = string.upper(string.format(L["message_log_new"] .."%s "..L["message_log_crit"]..": %d!", spellName,
          amount))
  local r, g, b = unpack(Critmatic.db.profile.fontSettings.fontColorCrit)
  Critmatic.MessageFrame:CreateMessage(message, r, g, b)
end

function Critmatic.ShowNewNormalMessage(spellName, amount)

  local message = string.upper(string.format(L["message_log_new"].."%s "..L["message_log_hit"]..": %d!", spellName,
          amount))
  local r, g, b = unpack(Critmatic.db.profile.fontSettings.fontColor)
  Critmatic.MessageFrame:CreateMessage(message, r, g, b)

end


