local _AddonName, _EchoRaidTools = ...;
local _PermaMarks = _EchoRaidTools:GetModule("perma-marks")


function _PermaMarks:CreateEventFrame()
    _PermaMarks.eventFrame = CreateFrame("FRAME")

    _PermaMarks.eventFrame:SetScript("OnEvent", function(self, event, ...)
        if event == "GROUP_ROSTER_UPDATE" then
                if  _PermaMarks.ConfigOpen and _PermaMarks.UpdateDropDowns then
                    _PermaMarks.UpdateDropDowns()
                end
                _PermaMarks:UpdatePlayerCanMarkState()
                if _PermaMarks.PlayerCanMark then
                    _PermaMarks:FindMarkableUnits()
                end

        elseif event == "GUILD_ROSTER_UPDATE" then
            _PermaMarks:BuildNicknameList()
            --recheck nicknames
        elseif event == "RAID_TARGET_UPDATE" then
            _PermaMarks:RefreshRaidMarks()
            --check markers




        elseif event == "PLAYER_TARGET_CHANGED" then
            _PermaMarks:FindMarkableUnits()
        elseif event == "PLAYER_FOCUS_CHANGED" then
            _PermaMarks:FindMarkableUnits()
        elseif event == "UPDATE_MOUSEOVER_UNIT" then
            _PermaMarks:FindMarkableUnits()
        elseif event =="UNIT_FLAGS" then
            _PermaMarks:FindMarkableUnits()
        end
    end)
    _PermaMarks.UpdateWatchedEvents()
end

function _PermaMarks.UpdateWatchedEvents()
    if  not _PermaMarks.ConfigOpen and not _PermaMarks:GetModuleEnabled() then
        if _PermaMarks.eventFrame:IsEventRegistered("GROUP_ROSTER_UPDATE") then
            _PermaMarks.eventFrame:UnregisterEvent("GROUP_ROSTER_UPDATE")
        end
    else
        if not _PermaMarks.eventFrame:IsEventRegistered("GROUP_ROSTER_UPDATE") then
            _PermaMarks.eventFrame:RegisterEvent("GROUP_ROSTER_UPDATE")
        end
    end

    if _PermaMarks:GetModuleEnabled() and _PermaMarks.PlayerCanMark then
        if not _PermaMarks.eventFrame:IsEventRegistered("RAID_TARGET_UPDATE") then
            _PermaMarks.eventFrame:RegisterEvent("RAID_TARGET_UPDATE")
        end
    else
        if _PermaMarks.eventFrame:IsEventRegistered("RAID_TARGET_UPDATE") then
            _PermaMarks.eventFrame:UnregisterEvent("RAID_TARGET_UPDATE")
        end
    end

    if _PermaMarks.registerTarget then
        if not _PermaMarks.eventFrame:IsEventRegistered("PLAYER_TARGET_CHANGED") then
            _PermaMarks.eventFrame:RegisterEvent("PLAYER_TARGET_CHANGED")
        end
    else
        if _PermaMarks.eventFrame:IsEventRegistered("PLAYER_TARGET_CHANGED") then
            _PermaMarks.eventFrame:UnregisterEvent("PLAYER_TARGET_CHANGED")
        end
    end

    if _PermaMarks.registerFocus then
        if not _PermaMarks.eventFrame:IsEventRegistered("PLAYER_FOCUS_CHANGED") then
            _PermaMarks.eventFrame:RegisterEvent("PLAYER_FOCUS_CHANGED")
        end
    else
        if _PermaMarks.eventFrame:IsEventRegistered("PLAYER_FOCUS_CHANGED") then
            _PermaMarks.eventFrame:UnregisterEvent("PLAYER_FOCUS_CHANGED")
        end
    end

    if _PermaMarks.registerMouseover then
        if not _PermaMarks.eventFrame:IsEventRegistered("UPDATE_MOUSEOVER_UNIT") then
            _PermaMarks.eventFrame:RegisterEvent("UPDATE_MOUSEOVER_UNIT")
        end
    else
        if _PermaMarks.eventFrame:IsEventRegistered("UPDATE_MOUSEOVER_UNIT") then
            _PermaMarks.eventFrame:UnregisterEvent("UPDATE_MOUSEOVER_UNIT")
        end
    end

    if _PermaMarks:GetIgnoreDead() then
        if not _PermaMarks.eventFrame:IsEventRegistered("UNIT_FLAGS") then
            _PermaMarks.eventFrame:RegisterEvent("UNIT_FLAGS")
        end
    else
        if _PermaMarks.eventFrame:IsEventRegistered("UNIT_FLAGS") then
            _PermaMarks.eventFrame:UnregisterEvent("UNIT_FLAGS")
        end
    end

    if _PermaMarks:GetGuildNames() then
        if not _PermaMarks.eventFrame:IsEventRegistered("GUILD_ROSTER_UPDATE") then
            _PermaMarks.eventFrame:RegisterEvent("GUILD_ROSTER_UPDATE")
        end
    else
        if _PermaMarks.eventFrame:IsEventRegistered("GUILD_ROSTER_UPDATE") then
            _PermaMarks.eventFrame:UnregisterEvent("GUILD_ROSTER_UPDATE")
        end
    end
end







