local LAM = LibStub:GetLibrary("LibAddonMenu-2.0")


RaidNotifier = {}

RaidNotifier.name		= 'RaidNotifier'
RaidNotifier.slash		= '/rn'
RaidNotifier.version	= '1.0.0'
RaidNotifier.versionDB	= 1
RaidNotifier.loaded	    = false
RaidNotifier.author     = 'silentgecko'
RaidNotifier.savedVarsName  = 'RNVars'
RaidNotifier.variables = {
    sanctum_magicka_deto = true,
    sanctum_poison       = true,
    last_notify          = 0,
}

local function CreateSettingsMenu()
    local self = RaidNotifier
    ------Creating the menu with LibAddonMenu-2.0 (thank you Seerah)-------
    self.panelData = {
        type = "panel",
        name = self.name,
        author = self.author,
        version = self.version,
        registerForRefresh = false,
    }

    self.optionsData = {
        {
            type = "description",
            text = GetString(RAIDNOTIFIER_DESCRIPTION),
        },
        {
            type = "checkbox",
            name = GetString(RAIDNOTIFIER_SANCTUM_MAGICKA_DETONATION),
            tooltip = GetString(RAIDNOTIFIER_SANCTUM_MAGICKA_DETONATION_TT),
            getFunc = function() return RaidNotifier.savedVariables.sanctum_magicka_deto end,
            setFunc = function(value)
                RaidNotifier.savedVariables.sanctum_magicka_deto = value
            end,
        },
        {
            type = "checkbox",
            name = GetString(RAIDNOTIFIER_SANCTUM_POISON),
            tooltip = GetString(RAIDNOTIFIER_SANCTUM_POISON_TT),
            getFunc = function() return RaidNotifier.savedVariables.sanctum_poison end,
            setFunc = function(value)
                RaidNotifier.savedVariables.sanctum_poison = value
            end,
        },
    }

    LAM:RegisterAddonPanel("RaidNotifierPanel", self.panelData)
    LAM:RegisterOptionControls("RaidNotifierPanel", self.optionsData)
end


---------Passing saved variables to the labels at initialize-------
function RaidNotifier.Initialize(event, addonName)
    local self = RaidNotifier

    if addonName ~= self.name then return end

    EVENT_MANAGER:UnregisterForEvent(self.name, EVENT_ADD_ON_LOADED)

    self.savedVariables = ZO_SavedVars:NewAccountWide(self.savedVarsName, self.versionDB, nil, self.variables)

    CreateSettingsMenu()

    EVENT_MANAGER:RegisterForEvent(self.name, EVENT_RAID_TRIAL_STARTED,  RaidNotifier.addEventListeners)
    EVENT_MANAGER:RegisterForEvent(self.name, EVENT_RAID_TRIAL_COMPLETE, RaidNotifier.removeEventListeners)
    EVENT_MANAGER:RegisterForEvent(self.name, EVENT_RAID_TRIAL_FAILED,   RaidNotifier.removeEventListeners)

    if IsRaidInProgress() then
        self.addEventListeners()
    else
        self.removeEventListeners()
    end
end

-- Add Events
function RaidNotifier.addEventListeners()
    local self   = RaidNotifier
    local raidId = GetCurrentParticipatingRaidId()
    -- add sanctum
    if raidId == 3 then
        EVENT_MANAGER:RegisterForEvent(self.name, EVENT_EFFECT_CHANGED, self.sanctumDebuffs)
    end
end

-- Remove all Events
function RaidNotifier.removeEventListeners()
    local self   = RaidNotifier
    local raidId = GetCurrentParticipatingRaidId()

    EVENT_MANAGER:UnregisterForEvent(self.name, EVENT_EFFECT_CHANGED)
end

function RaidNotifier.sanctumDebuffs(_, change, buff, name, unit, start, finish, stack, icon, _, effectType, abilityType, statusEffectType, unitName, unitId, abilityId)
    -- only take care of player
    if (unit ~= 'player') then return end

    local self = RaidNotifier

    -- only notice on effect added
    if (change == EFFECT_RESULT_GAINED) then
        if self.savedVariables.sanctum_poison then
            -- i don't know which of these debuffs is the correct, so we add all ;-)
            local poison = {}
            poison[38965] = true
            poison[38968] = true
            poison[38969] = true
            poison[41822] = true
            poison[41823] = true
            poison[41824] = true
            poison[41826] = true
            poison[41827] = true
            poison[41829] = true
            poison[41830] = true
            poison[41832] = true
            poison[41833] = true
            poison[53782] = true
            poison[53786] = true
            poison[53792] = true
            poison[53799] = true
            poison[53815] = true
            poison[54476] = true
            poison[54692] = true
            poison[55085] = true
            poison[55086] = true
            poison[55089] = true
            poison[55148] = true
            poison[57989] = true
            poison[65779] = true
            poison[65780] = true
            poison[65781] = true

            if (poison[abilityId]) then
--                d('abilityId', abilityId)
--                d('abilityName', name)
--                d('abilityType', abilityType)
--                d('buff', buff)
--                d('effectType', effectType)
--                d('start', start)
--                d('finish', finish)
                CENTER_SCREEN_ANNOUNCE:AddMessage(2, CSA_EVENT_COMBINED_TEXT, SOUNDS.CHAMPION_POINTS_COMMITTED, GetString(RAIDNOTIFIER_SANCTUM_POISON_ALERT), nil, nil, nil, nil, nil, 2000)
            end
        end

        if self.savedVariables.sanctum_magicka_deto then
            if (abilityId == 59036) then
--                d('abilityId', abilityId)
--                d('abilityName', name)
--                d('abilityType', abilityType)
--                d('buff', buff)
--                d('effectType', effectType)
--                d('start', start)
--                d('finish', finish)
                CENTER_SCREEN_ANNOUNCE:AddMessage(1, CSA_EVENT_COMBINED_TEXT, SOUNDS.CHAMPION_POINTS_COMMITTED, GetString(RAIDNOTIFIER_SANCTUM_MAGICKA_DETONATION_ALERT), nil, nil, nil, nil, nil, 5000)
            end
        end
    end
end

---------Events-------
EVENT_MANAGER:RegisterForEvent(RaidNotifier.name, EVENT_ADD_ON_LOADED, RaidNotifier.Initialize)