--## Author: Rubber.Band (Original WA: https://wago.io/WQHasRep)  ## Version: 1.4
local function CreatePulse(obj)
    local g = obj:CreateAnimationGroup()
    local a1 = g:CreateAnimation("Scale")
    a1:SetScale(1.15, 1.15)
    a1:SetDuration(0.8)
    a1:SetOrder(1)
    a1:SetSmoothing("IN_OUT")
    local a2 = g:CreateAnimation("Scale")
    a2:SetScale(0.87, 0.87)
    a2:SetDuration(0.8)
    a2:SetOrder(2)
    a2:SetSmoothing("IN_OUT")
    g:SetLooping("REPEAT")
    return g
end

local function ApplyGlow(pin, timeLeft, isPhase)
    if not pin.RepGlow then
        pin.RepGlow = pin:CreateTexture(nil, "OVERLAY", nil, 7)
        pin.RepGlow:SetTexture("Interface\\Buttons\\CheckButtonGlow")
        pin.RepGlow:SetBlendMode("ADD")
        pin.RepGlow:SetPoint("CENTER", pin, "CENTER", 0, 0)
        pin.GlowAnim = CreatePulse(pin.RepGlow)
        pin.GlowAnim:Play()
    end

    if timeLeft and timeLeft <= 43200 then
        pin.RepGlow:SetVertexColor(1, 0.1, 0.1, 1) -- 강렬한 빨강
        pin.RepGlow:SetSize(60, 60)
    elseif timeLeft and timeLeft <= 86400 then
        pin.RepGlow:SetVertexColor(1, 0.8, 0, 1) -- 황금색
        pin.RepGlow:SetSize(52, 52)
    else
        pin.RepGlow:SetVertexColor(0, 1, 0.4, 0.8) -- 녹색
        pin.RepGlow:SetSize(42, 42)
    end

    if isPhase then
        pin.RepGlow:SetVertexColor(0.8, 0.3, 1, 1) -- 보라색
    end
    
    pin.RepGlow:Show()
end

local f = CreateFrame("Frame")
f.t = 0
f:SetScript("OnUpdate", function(self, e)
    if not WorldMapFrame:IsShown() then return end
    self.t = self.t + e
    if self.t < 0.2 then return end
    self.t = 0
    
    -- [수정] 핀을 찾는 방식을 더 확실한 방법으로 보강
    local canvas = WorldMapFrame.ScrollContainer.Child
    if not canvas then return end
    
    local children = {canvas:GetChildren()}
    for i = 1, #children do
        local p = children[i]
        if p.worldQuest and p.questID then
            -- 계정 평판 보너스 확인 로직
            local r = C_QuestLog.QuestContainsFirstTimeRepBonusForPlayer(p.questID)
            if r then
                local n = C_QuestLog.GetTitleForQuestID(p.questID) or ""
                local ph = n:find("Phase") ~= nil
                local tl = C_TaskQuest.GetQuestTimeLeftSeconds(p.questID)
                ApplyGlow(p, tl, ph)
            elseif p.RepGlow then
                p.RepGlow:Hide()
            end
        end
    end
end)