local _, NSI = ... -- Internal namespace

local encID = 3179
-- /run NSAPI:DebugEncounter(3179)
NSI.EncounterAlertStart[encID] = function(self, id) -- on ENCOUNTER_START
    if not NSRT.EncounterAlerts[encID] then
        NSRT.EncounterAlerts[encID] = {enabled = false}
    end
    id = id or self:DifficultyCheck(14) or 0
    if NSRT.EncounterAlerts[encID].enabled then -- text, Type, spellID, dur, phase, encID
        local Alert = self:CreateDefaultAlert("Beams", "Text", nil, 8, 1, encID)


        -- using same timers for all difficulties atm
        local timers = {
            [0] = {},
            [14] = {102.6, 224.2, 346},
            [15] = {102.6, 224.2, 346},
            [16] = {102.6, 224.2, 346},
        }
        for i, v in ipairs(timers[id] or {}) do -- Entropic Unraveling
            Alert.time = v
            self:AddToReminder(Alert)
        end

        Alert.text, Alert.TTS, Alert.dur = "Orbs", "Orbs", 5
        timers = {
            [0] = {},
            [14] = {14.1, 59.1, 135, 180.7, 256.5, 301.6},
            [15] = {14.1, 59.1, 135, 180.7, 256.5, 301.6},
            [16] = {18.1, 63.1, 141, 186.7, 262.5, 307.6},
        }
        for i, v in ipairs(timers[id] or {}) do -- Void Convergence (Adds)
            Alert.time = v
            self:AddToReminder(Alert)
        end

        Alert.text, Alert.TTS = "CC Adds", "CC Adds"
        timers = {
            [0] = {},
            [14] = {20, 65, 141, 187, 263, 308},
            [15] = {20, 65, 141, 187, 263, 308},
            [16] = {27.6, 73, 150.8, 196.9, 272.4, 317.5},
        }
        for i, v in ipairs(timers[id] or {}) do -- Fractured Projection (CC Adds)
            Alert.time = v
            self:AddToReminder(Alert)
        end
    end
    if NSRT.EncounterAlerts[encID].CCAddsDisplay and id == 16 then
        self.platetexts = self.platetexts or {}
        local plateref = {}
        local function DisplayNameplateText(aura1, aura2, u)
            local plate = C_NamePlate.GetNamePlateForUnit(u)
            if plate then
                for i=1, #self.platetexts+1 do
                    if self.platetexts[i] and not self.platetexts[i]:IsShown() then
                        if aura2 then
                            self.platetexts[i]:SetText("WAIT")
                            self.platetexts[i].bgTexture:SetColorTexture(1, 0, 0, 0.8)
                        else
                            self.platetexts[i]:SetText("CC")
                            self.platetexts[i].bgTexture:SetColorTexture(0, 1, 0, 0.8)
                        end
                        self.platetexts[i]:ClearAllPoints()
                        self.platetexts[i]:SetPoint("BOTTOM", plate, "TOP", 0, 0)

                        self.platetexts[i]:Show()
                        self.platetexts[i].bgFrame:Show()
                        self.platetexts[i].unit = u
                        plateref[u] = i
                        return
                    elseif not self.platetexts[i] then

                        self.platetexts[i] = self.plateframe:CreateFontString(nil, "OVERLAY", "GameFontNormal")
                        self.platetexts[i]:SetFont(self.LSM:Fetch("font", NSRT.Settings.GlobalFont), 18, "OUTLINE")
                        self.platetexts[i]:SetPoint("BOTTOM", plate, "TOP", 0, 0)
                        self.platetexts[i]:SetShadowColor(0, 0, 0, 1)
                        self.platetexts[i]:SetTextColor(1, 1, 1, 1)

                        self.platetexts[i].bgFrame = CreateFrame("Frame", nil, self.plateframe)
                        self.platetexts[i].bgFrame:SetFrameStrata("BACKGROUND")
                        self.platetexts[i].bgTexture = self.platetexts[i].bgFrame:CreateTexture(nil, "BACKGROUND")
                        self.platetexts[i].bgTexture:SetColorTexture(1, 1, 1, 0.8)
                        self.platetexts[i].bgTexture:SetAllPoints(self.platetexts[i].bgFrame)
                        self.platetexts[i].bgFrame:SetSize(25, 25)
                        self.platetexts[i].bgFrame:SetPoint("CENTER", self.platetexts[i], "CENTER", 0, 0)

                        if aura2 then
                            self.platetexts[i]:SetText("WAIT")
                            self.platetexts[i].bgTexture:SetColorTexture(1, 0, 0, 0.8)
                        else
                            self.platetexts[i]:SetText("CC")
                            self.platetexts[i].bgTexture:SetColorTexture(0, 1, 0, 0.8)
                        end

                        self.platetexts[i]:Show()
                        self.platetexts[i].bgFrame:Show()
                        self.platetexts[i].unit = u
                        plateref[u] = i
                        return
                    end
                end
            end
        end
        local function UpdateNameplateTexts(e, u)
            if e == "NAME_PLATE_UNIT_REMOVED" then
                if plateref[u] then
                    if self.platetexts[plateref[u]] then
                        self.platetexts[plateref[u]]:Hide()
                        self.platetexts[plateref[u]].bgFrame:Hide()
                        self.platetexts[plateref[u]].unit = nil
                        plateref[u] = nil
                        return
                    end
                end
                -- fallback if plateref somehow doesn't exist
                for i, v in ipairs(self.platetexts) do
                    if v.unit == u then
                        v:Hide()
                        v.bgFrame:Hide()
                        v.unit = nil
                    end
                end
                return
            elseif e == "NAME_PLATE_UNIT_ADDED" or e == "UNIT_AURA" then
                if e == "UNIT_AURA" and not u:find("^nameplate%d") then return end -- only allow nameplate units for UNIT_AURA
                if UnitLevel(u) ~= -1 then
                    local aura1 = C_UnitAuras.GetAuraDataByIndex(u, 1, "HELPFUL")
                    if aura1 then
                        local aura2 = C_UnitAuras.GetAuraDataByIndex(u, 2, "HELPFUL")
                        if plateref[u] then
                            if self.platetexts[plateref[u]] then
                                self.platetexts[plateref[u]]:Hide()
                                self.platetexts[plateref[u]].bgFrame:Hide()
                                self.platetexts[plateref[u]].unit = nil
                                plateref[u] = nil
                            end
                        end
                        DisplayNameplateText(aura1, aura2, u)
                    end
                end
            end
        end

        if not self.plateframe then
            self.plateframe = CreateFrame("Frame")
            self.plateframe:SetScript("OnEvent", function(_, e, u)
                if e == "NAME_PLATE_UNIT_ADDED" then
                    UpdateNameplateTexts(e, u)
                elseif e == "NAME_PLATE_UNIT_REMOVED" then
                    UpdateNameplateTexts(e, u)
                elseif e == "UNIT_AURA" then
                    UpdateNameplateTexts(e, u)
                end
            end)
        end
        self.plateframe:RegisterEvent("NAME_PLATE_UNIT_ADDED")
        self.plateframe:RegisterEvent("NAME_PLATE_UNIT_REMOVED")
        self.plateframe:RegisterEvent("UNIT_AURA")
        self.plateframe:Show()
    end
end

NSI.EncounterAlertStop[encID] = function(self) -- on ENCOUNTER_END
    if NSRT.EncounterAlerts[encID].CCAddsDisplay then
        if self.plateframe then
            for i, v in ipairs(self.platetexts) do
                v:Hide()
                v.bgFrame:Hide()
            end
            self.plateframe:UnregisterEvent("NAME_PLATE_UNIT_ADDED")
            self.plateframe:UnregisterEvent("NAME_PLATE_UNIT_REMOVED")
            self.plateframe:UnregisterEvent("UNIT_AURA")
            self.plateframe:Hide()
        end
    end
end