local time = time

local function IsSolo()
    if IsInRaid() then
        return false
    end

    if IsInGroup() then
        return false
    end

    return true
end

local WA_ClassColorName = function(unit)
    if unit and UnitExists(unit) then
      local name = UnitName(unit)
      local _, class = UnitClass(unit)
      if not class then
        return name
      else
        local classData = (CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)[class]
        local coloredName = ("|c%s%s|r"):format(classData.colorStr, name)
        return coloredName
      end
    else
      return "" -- ¯\_(ツ)_/¯
    end
end

local SendChatMessage = function(text)
    print('|cffffff80MerfinPlus:|r ', text)
end

-- Register the addon message prefix
C_ChatInfo.RegisterAddonMessagePrefix("MERFIN_PT")

local function PullCommandHandler(msg, editbox)
    local duration = tonumber(msg)
    if duration then
        if IsSolo() then
            SendChatMessage("You need to be in a party or raid group to start a timer")
            return
        end
        C_ChatInfo.SendAddonMessage("MERFIN_PT", "TIMER:PULL:".. duration, "RAID")
        SendChatMessage("Pull timer set for " .. duration .. " seconds.")
    else
        SendChatMessage("Invalid command format. Use '/pull 10'.")
    end
end

local function BreakCommandHandler(msg, editbox)
    local duration = tonumber(msg)
    if duration then
        if IsSolo() then
            SendChatMessage("You need to be in a party or raid group to start a timer")
            return
        end
        C_ChatInfo.SendAddonMessage("MERFIN_PT", "TIMER:BREAK:".. duration, "RAID")
        SendChatMessage("Break timer set for " .. duration .. " seconds.")
    else
        SendChatMessage("Invalid command format. Use '/break 10'.")
    end
end

SLASH_PULL1 = '/pull'
SlashCmdList["PULL"] = PullCommandHandler

SLASH_BREAK1 = '/break'
SlashCmdList["BREAK"] = BreakCommandHandler

-- Overwrite Blizz countdown
local OverWriteBlizzCountdown = function()
    C_PartyInfo.DoCountdown = function(duration)
        if duration then
            C_ChatInfo.SendAddonMessage("MERFIN_PT", "TIMER:PULL:".. duration, "RAID")
            SendChatMessage("Pull timer set for " .. duration .. " seconds.")
        end
    end 
end

-- Disable default blizz countdown
if C_EventUtils.IsEventValid("START_PLAYER_COUNTDOWN") then
    TimerTracker:UnregisterEvent("START_PLAYER_COUNTDOWN")
    TimerTracker:UnregisterEvent("CANCEL_PLAYER_COUNTDOWN")
end

MerfinPlusSaved = MerfinPlusSaved or {}
MerfinPlusSaved.pullStartTime = MerfinPlusSaved.pullStartTime or 0
MerfinPlusSaved.pullExpTime = MerfinPlusSaved.pullExpTime or 0
MerfinPlusSaved.breakStartTime = MerfinPlusSaved.breakStartTime or 0
MerfinPlusSaved.breakExpTime = MerfinPlusSaved.breakExpTime or 0

local StarTimer = function(duration, senderName, type)
    -- If Sender is Leader of Assist
    local isLeader = UnitIsGroupLeader(senderName)
    local isAssistant = UnitIsGroupAssistant(senderName)

    if isLeader or isAssistant and not IsSolo() then
        if type == 'PULL' then
            SendChatMessage(WA_ClassColorName(senderName) .. ' sent a ' .. duration .. 's pull timer.')
            MerfinPlusSaved.pullStartTime = time()
            MerfinPlusSaved.pullExpTime = duration + MerfinPlusSaved.pullStartTime
            WeakAuras.ScanEvents("MERFIN_PULL_TIMER", duration, senderName)
        elseif type == 'BREAK' then

        end
    end
end

local PullTimer = CreateFrame("Frame")
PullTimer:RegisterEvent("ADDON_LOADED")
PullTimer:RegisterEvent("CHAT_MSG_ADDON")
PullTimer:RegisterEvent("GROUP_ROSTER_UPDATE")

PullTimer:SetScript("OnEvent", function(self, event, ...)
    if event == "ADDON_LOADED" then 
        local addOnName = ...
        if addOnName == "Blizzard_RaidUI" then
            OverWriteBlizzCountdown()
        end
    elseif event == 'CHAT_MSG_ADDON' then
        local prefix, msg, channel, sender = ...
        if prefix == "MERFIN_PT" and msg:find("^TIMER:") then
            local _, timerType, duration = strsplit(":", msg)
            duration = tonumber(duration)
            -- Get unit name of sender without server name
            local senderName = Ambiguate(sender, "none")
            StarTimer(duration, senderName, timerType)
        elseif prefix == 'BigWigs' then
            local _, timerType, duration = strsplit('^', msg)
            duration = tonumber(duration)
            -- Get unit name of sender without server name
            local senderName = Ambiguate(sender, "none")
            StarTimer(duration, senderName, string.upper(timerType))
        end
    elseif event == 'GROUP_ROSTER_UPDATE' then
        if IsSolo() then
            MerfinPlusSaved.pullStartTime = 0
            MerfinPlusSaved.pullExpTime = 0
            MerfinPlusSaved.breakStartTime = 0
            MerfinPlusSaved.breakExpTime = 0
        end
    end
end)

-- Get Saved Pull Timer
Merfin.GetPullTimer = function()
    local curTime = time()
    if MerfinPlusSaved.pullExpTime > curTime then
        local timeRemaining = MerfinPlusSaved.pullExpTime - curTime
        local totalTime = MerfinPlusSaved.pullExpTime - MerfinPlusSaved.pullStartTime
        return timeRemaining, totalTime
    end
end

-- Get Saved Break Timer
Merfin.GetBreakTimer = function()
    local curTime = time()
    if MerfinPlusSaved.breakExpTime > curTime then
        local timeRemaining = MerfinPlusSaved.breakExpTime - curTime
        local totalTime = MerfinPlusSaved.breakExpTime - MerfinPlusSaved.breakStartTime
        return timeRemaining, totalTime
    end
end