-- Global Initalizations
RR           = {}
RR.addonName = 'RangeReticle'
RR.version   = '0.6'

-- Addon Setup
function RR.Initialize(eventCode, addOnName)
    if (addOnName ~= RR.addonName) then return end

    RR.LoadSettings()
    RR.RegisterCommands()
    --RR.CreateSettingsUI()
    RR.RegisterEvents()

    EVENT_MANAGER:UnregisterForEvent(RR.addonName, EVENT_ADD_ON_LOADED)
end

-- Load Addon Settings
function RR.LoadSettings()
    RR.SavedVars = ZO_SavedVars:NewAccountWide('RRSavedVariables' , 1, nil, RR.SavedVarsDefaults, nil)

    RR.targetDebubInfo = false

    -- Convert colors to hex
    RR.hostileIRColorHex = RR.RGBPercToHex(RR.SavedVars.hostileOORColor.r,RR.SavedVars.hostileOORColor.g,RR.SavedVars.hostileOORColor.b)
    RR.hostileOORColorHex = RR.RGBPercToHex(RR.SavedVars.hostileIRColor.r,RR.SavedVars.hostileIRColor.g,RR.SavedVars.hostileIRColor.b)
    RR.hostileIMRColorHex = RR.RGBPercToHex(RR.SavedVars.hostileIMRColor.r,RR.SavedVars.hostileIMRColor.g,RR.SavedVars.hostileIMRColor.b)

    RR.alliesIRColorHex = RR.RGBPercToHex(RR.SavedVars.alliesOORColor.r,RR.SavedVars.alliesOORColor.g,RR.SavedVars.alliesOORColor.b)
    RR.alliesOORColorHex = RR.RGBPercToHex(RR.SavedVars.alliesIRColor.r,RR.SavedVars.alliesIRColor.g,RR.SavedVars.alliesIRColor.b)
    RR.alliesIMRColorHex = RR.RGBPercToHex(RR.SavedVars.alliesIMRColor.r,RR.SavedVars.alliesIMRColor.g,RR.SavedVars.alliesIMRColor.b)

    RR.neutralIRColorHex = RR.RGBPercToHex(RR.SavedVars.neutralOORColor.r,RR.SavedVars.neutralOORColor.g,RR.SavedVars.neutralOORColor.b)
    RR.neutralOORColorHex = RR.RGBPercToHex(RR.SavedVars.neutralIRColor.r,RR.SavedVars.neutralIRColor.g,RR.SavedVars.neutralIRColor.b)
    RR.neutralIMRColorHex = RR.RGBPercToHex(RR.SavedVars.neutralIMRColor.r,RR.SavedVars.neutralIMRColor.g,RR.SavedVars.neutralIMRColor.b)

    RR.npcsIRColorHex = RR.RGBPercToHex(RR.SavedVars.npcsOORColor.r,RR.SavedVars.npcsOORColor.g,RR.SavedVars.npcsOORColor.b)
    RR.npcsOORColorHex = RR.RGBPercToHex(RR.SavedVars.npcsIRColor.r,RR.SavedVars.npcsIRColor.g,RR.SavedVars.npcsIRColor.b)
    RR.npcsIMRColorHex = RR.RGBPercToHex(RR.SavedVars.npcsIMRColor.r,RR.SavedVars.npcsIMRColor.g,RR.SavedVars.npcsIMRColor.b)

    RR.previousDimensionsX = 10000
    RR.previousDimensionsY = 10000
end

-- Register slash commands
function RR.RegisterCommands()
  SLASH_COMMANDS['/rret'] = function(text)
    if text == nil or string.len(text) <= 0 or text == 'help' then
        d("RangeReticle Help:")
        d("/rret : display help.")
        d("/rret map : dump map debug information to chat")
        d("rret dbg : toggle target debug info mode")
    elseif text == 'map' then
        RR.Debug()
    elseif text == 'dbg' then
        RR.targetDebubInfo = not RR.targetDebubInfo
    end
  end
end

-- Register for events
function RR.RegisterEvents()
    EVENT_MANAGER:RegisterForEvent(RR.addonName, EVENT_RETICLE_HIDDEN_UPDATE, RR.OnReticleHiddenUpdate)
end

-- Reticle hidden update
function RR.OnReticleHiddenUpdate(event)
    local reticleHidden = IsReticleHidden()
    RangeReticleRange:SetHidden(reticleHidden)
    RangeReticleName:SetHidden(reticleHidden)
end

-- Convert an RGB color percentage 0 to 1 to Hex
function RR.RGBPercToHex(r, g, b)
    r = r <= 1 and r >= 0 and r or 0
    g = g <= 1 and g >= 0 and g or 0
    b = b <= 1 and b >= 0 and b or 0
    return string.format("%02x%02x%02x", r*255, g*255, b*255)
end

-- Create the settings in the control panel
function RR.CreateSettingsUI()
    RR.LAM = LibStub("LibAddonMenu-2.0")

    local panelData = {
        type = "panel",
        name = RR.addonName,
        displayName = RR.addonName .. " by Adein",
        author = "Adein",
        version = RR.version,
        registerForRefresh = true,	--boolean (optional) (will refresh all options controls when a setting is changed and when the panel is shown)
        registerForDefaults = true,	--boolean (optional) (will set all options controls back to default values)
    }

    RR.LAM:RegisterAddonPanel(RR.addonName, panelData)

    local optionsTable = {
        [1] = {
            type = "header",
            name = "Range Reticle Settings",
            width = "full",	--or "half" (optional)
        },
        [2] = {
            type = "submenu",
            name = "Hostile Targets",
            tooltip = "Settings for hostile targets",	--(optional)
            controls = {
                [1] = {
                    type = "checkbox",
                    name = "Show Name",
                    tooltip = "If enabled, the target name is shown above the reticle",
                    getFunc = function() return RR.SavedVars.hostileNameEnabled end,
                    setFunc = function() RR.SavedVars.hostileNameEnabled = not RR.SavedVars.hostileNameEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [2] = {
                    type = "checkbox",
                    name = "Show Level",
                    tooltip = "If enabled, the target level is shown above the reticle",
                    getFunc = function() return RR.SavedVars.hostileLevelEnabled end,
                    setFunc = function() RR.SavedVars.hostileLevelEnabled = not RR.SavedVars.hostileLevelEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [3] = {
                    type = "checkbox",
                    name = "Show Range",
                    tooltip = "If enabled, the target range is shown at the top-right of the reticle",
                    getFunc = function() return RR.SavedVars.hostileRangeEnabled end,
                    setFunc = function() RR.SavedVars.hostileRangeEnabled = not RR.SavedVars.hostileRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [4] = {
                    type = "checkbox",
                    name = "Show Target Difficulty",
                    tooltip = "If enabled, the target difficulty is shown as *'s above the reticle",
                    getFunc = function() return RR.SavedVars.hostileDifficultyEnabled end,
                    setFunc = function() RR.SavedVars.hostileDifficultyEnabled = not RR.SavedVars.hostileDifficultyEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [5] = {
                    type = "checkbox",
                    name = "Colorize Reticle",
                    tooltip = "If enabled, the color of the target reticle is colorized to reflect range status",
                    getFunc = function() return RR.SavedVars.hostileReticleColoringEnabled end,
                    setFunc = function() RR.SavedVars.hostileReticleColoringEnabled = not RR.SavedVars.hostileReticleColoringEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [6] = {
                    type = "checkbox",
                    name = "Colorize Range",
                    tooltip = "If enabled, the color of the target range is colorized to reflect range status",
                    getFunc = function() return RR.SavedVars.hostileRangeColoringEnabled end,
                    setFunc = function() RR.SavedVars.hostileRangeColoringEnabled = not RR.SavedVars.hostileRangeColoringEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [7] = {
                    type = "checkbox",
                    name = "Colorize Name and Level",
                    tooltip = "If enabled, the color of the name and level is colorized to reflect range status",
                    getFunc = function() return RR.SavedVars.hostileNameLevelColoringEnabled end,
                    setFunc = function() RR.SavedVars.hostileNameLevelColoringEnabled = not RR.SavedVars.hostileNameLevelColoringEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [8] = {
                    type = "checkbox",
                    name = "Out Of Range",
                    tooltip = "If enabled, the reticle color will change when the target is out of range",
                    getFunc = function() return RR.SavedVars.hostileOutOfRangeEnabled end,
                    setFunc = function() RR.SavedVars.hostileOutOfRangeEnabled = not RR.SavedVars.hostileOutOfRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [9] = {
                    type = "colorpicker",
                    name = "Out Of Range Color",
                    tooltip = "The reticle color when the target is out of range",
                    getFunc = function() return RR.SavedVars.hostileOORColor.r, RR.SavedVars.hostileOORColor.g, RR.SavedVars.hostileOORColor.b end,
                    setFunc = function(r,g,b,a)
                        RR.SavedVars.hostileOORColor.r = r
                        RR.SavedVars.hostileOORColor.g = g
                        RR.SavedVars.hostileOORColor.b = b
                        hostileOORColorHex = RR.RGBPercToHex(r,g,b)
				        end,
                    width = "half",	--or "half" (optional)
                },
                [10] = {
                    type = "checkbox",
                    name = "In Range",
                    tooltip = "If enabled, the reticle color will change when the target is in range",
                    getFunc = function() return RR.SavedVars.hostileInRangeEnabled end,
                    setFunc = function() RR.SavedVars.hostileInRangeEnabled = not RR.SavedVars.hostileInRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [11] = {
                    type = "slider",
                    name = "In Range Distance",
                    tooltip = "Set the distance that determines when a target is in range",
                    min = 0,
                    max = 60,
                    step = 1,	--(optional)
                    getFunc = function() return RR.SavedVars.hostileInRangeDistance end,
                    setFunc = function(value) RR.SavedVars.hostileInRangeDistance = value end,
                    width = "half",	--or "half" (optional)
                },
                [12] = {
                    type = "colorpicker",
                    name = "In Range Color",
                    tooltip = "The reticle color when the target is in range",
                    getFunc = function() return RR.SavedVars.hostileIRColor.r, RR.SavedVars.hostileIRColor.g, RR.SavedVars.hostileIRColor.b end,
                    setFunc = function(r,g,b,a)
                        RR.SavedVars.hostileIRColor.r = r
                        RR.SavedVars.hostileIRColor.g = g
                        RR.SavedVars.hostileIRColor.b = b
                        hostileIRColorHex = RR.RGBPercToHex(r,g,b)
                        end,
                    width = "half",	--or "half" (optional)
                },
                [13] = {
                    type = "checkbox",
                    name = "In Melee Range",
                    tooltip = "If enabled, the reticle color will change when the target is in melee range",
                    getFunc = function() return RR.SavedVars.hostileInMeleeRangeEnabled end,
                    setFunc = function() RR.SavedVars.hostileInMeleeRangeEnabled = not RR.SavedVars.hostileInMeleeRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [14] = {
                    type = "slider",
                    name = "In Melee Range Distance",
                    tooltip = "Set the distance that determines when a target is in melee range",
                    min = 0,
                    max = 60,
                    step = 1,	--(optional)
                    getFunc = function() return RR.SavedVars.hostileInMeleeRangeDistance end,
                    setFunc = function(value) RR.SavedVars.hostileInMeleeRangeDistance = value end,
                    width = "half",	--or "half" (optional)
                },
                [15] = {
                    type = "colorpicker",
                    name = "In Melee Range Color",
                    tooltip = "The reticle color when the target is in melee range",
                    getFunc = function() return RR.SavedVars.hostileIMRColor.r, RR.SavedVars.hostileIMRColor.g, RR.SavedVars.hostileIMRColor.b end,
                    setFunc = function(r,g,b,a)
                        RR.SavedVars.hostileIMRColor.r = r
                        RR.SavedVars.hostileIMRColor.g = g
                        RR.SavedVars.hostileIMRColor.b = b
                        hostileIMRColorHex = RR.RGBPercToHex(r,g,b)
                        end,
                    width = "half",	--or "half" (optional)
                },
            },
        },
        [3] = {
            type = "submenu",
            name = "Allies",
            tooltip = "Settings for allies",	--(optional)
            controls = {
                [1] = {
                    type = "checkbox",
                    name = "Show Name",
                    tooltip = "If enabled, the target name is shown above the reticle",
                    getFunc = function() return RR.SavedVars.alliesNameEnabled end,
                    setFunc = function() RR.SavedVars.alliesNameEnabled = not RR.SavedVars.alliesNameEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [2] = {
                    type = "checkbox",
                    name = "Show Level",
                    tooltip = "If enabled, the target level is shown above the reticle",
                    getFunc = function() return RR.SavedVars.alliesLevelEnabled end,
                    setFunc = function() RR.SavedVars.alliesLevelEnabled = not RR.SavedVars.alliesLevelEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [3] = {
                    type = "checkbox",
                    name = "Show Range",
                    tooltip = "If enabled, the target range is shown at the top-right of the reticle",
                    getFunc = function() return RR.SavedVars.alliesRangeEnabled end,
                    setFunc = function() RR.SavedVars.alliesRangeEnabled = not RR.SavedVars.alliesRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [4] = {
                    type = "checkbox",
                    name = "Show Target Difficulty",
                    tooltip = "If enabled, the target difficulty is shown as *'s above the reticle",
                    getFunc = function() return RR.SavedVars.alliesDifficultyEnabled end,
                    setFunc = function() RR.SavedVars.alliesDifficultyEnabled = not RR.SavedVars.alliesDifficultyEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [5] = {
                    type = "checkbox",
                    name = "Colorize Reticle",
                    tooltip = "If enabled, the color of the target reticle is colorized to reflect range status",
                    getFunc = function() return RR.SavedVars.alliesReticleColoringEnabled end,
                    setFunc = function() RR.SavedVars.alliesReticleColoringEnabled = not RR.SavedVars.alliesReticleColoringEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [6] = {
                    type = "checkbox",
                    name = "Colorize Range",
                    tooltip = "If enabled, the color of the target range is colorized to reflect range status",
                    getFunc = function() return RR.SavedVars.alliesRangeColoringEnabled end,
                    setFunc = function() RR.SavedVars.alliesRangeColoringEnabled = not RR.SavedVars.alliesRangeColoringEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [7] = {
                    type = "checkbox",
                    name = "Colorize Name and Level",
                    tooltip = "If enabled, the color of the name and level is colorized to reflect range status",
                    getFunc = function() return RR.SavedVars.alliesNameLevelColoringEnabled end,
                    setFunc = function() RR.SavedVars.alliesNameLevelColoringEnabled = not RR.SavedVars.alliesNameLevelColoringEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [8] = {
                    type = "checkbox",
                    name = "Out Of Range",
                    tooltip = "If enabled, the reticle color will change when the target is out of range",
                    getFunc = function() return RR.SavedVars.alliesOutOfRangeEnabled end,
                    setFunc = function() RR.SavedVars.alliesOutOfRangeEnabled = not RR.SavedVars.alliesOutOfRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [9] = {
                    type = "colorpicker",
                    name = "Out Of Range Color",
                    tooltip = "The reticle color when the target is out of range",
                    getFunc = function() return RR.SavedVars.alliesOORColor.r, RR.SavedVars.alliesOORColor.g, RR.SavedVars.alliesOORColor.b end,
                    setFunc = function(r,g,b,a)
                        RR.SavedVars.alliesOORColor.r = r
                        RR.SavedVars.alliesOORColor.g = g
                        RR.SavedVars.alliesOORColor.b = b
                        alliesOORColorHex = RR.RGBPercToHex(r,g,b)
				        end,
                    width = "half",	--or "half" (optional)
                },
                [10] = {
                    type = "checkbox",
                    name = "In Range",
                    tooltip = "If enabled, the reticle color will change when the target is in range",
                    getFunc = function() return RR.SavedVars.alliesInRangeEnabled end,
                    setFunc = function() RR.SavedVars.alliesInRangeEnabled = not RR.SavedVars.alliesInRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [11] = {
                    type = "slider",
                    name = "In Range Distance",
                    tooltip = "Set the distance that determines when a target is in range",
                    min = 0,
                    max = 60,
                    step = 1,	--(optional)
                    getFunc = function() return RR.SavedVars.alliesInRangeDistance end,
                    setFunc = function(value) RR.SavedVars.alliesInRangeDistance = value end,
                    width = "half",	--or "half" (optional)
                },
                [12] = {
                    type = "colorpicker",
                    name = "In Range Color",
                    tooltip = "The reticle color when the target is in range",
                    getFunc = function() return RR.SavedVars.alliesIRColor.r, RR.SavedVars.alliesIRColor.g, RR.SavedVars.alliesIRColor.b end,
                    setFunc = function(r,g,b,a)
                        RR.SavedVars.alliesIRColor.r = r
                        RR.SavedVars.alliesIRColor.g = g
                        RR.SavedVars.alliesIRColor.b = b
                        alliesIRColorHex = RR.RGBPercToHex(r,g,b)
                        end,
                    width = "half",	--or "half" (optional)
                },
                [13] = {
                    type = "checkbox",
                    name = "In Melee Range",
                    tooltip = "If enabled, the reticle color will change when the target is in melee range",
                    getFunc = function() return RR.SavedVars.alliesInMeleeRangeEnabled end,
                    setFunc = function() RR.SavedVars.alliesInMeleeRangeEnabled = not RR.SavedVars.alliesInMeleeRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [14] = {
                    type = "slider",
                    name = "In Melee Range Distance",
                    tooltip = "Set the distance that determines when a target is in melee range",
                    min = 0,
                    max = 60,
                    step = 1,	--(optional)
                    getFunc = function() return RR.SavedVars.alliesInMeleeRangeDistance end,
                    setFunc = function(value) RR.SavedVars.alliesInMeleeRangeDistance = value end,
                    width = "half",	--or "half" (optional)
                },
                [15] = {
                    type = "colorpicker",
                    name = "In Melee Range Color",
                    tooltip = "The reticle color when the target is in melee range",
                    getFunc = function() return RR.SavedVars.alliesIMRColor.r, RR.SavedVars.alliesIMRColor.g, RR.SavedVars.alliesIMRColor.b end,
                    setFunc = function(r,g,b,a)
                        RR.SavedVars.alliesIMRColor.r = r
                        RR.SavedVars.alliesIMRColor.g = g
                        RR.SavedVars.alliesIMRColor.b = b
                        alliesIMRColorHex = RR.RGBPercToHex(r,g,b)
                        end,
                    width = "half",	--or "half" (optional)
                },
            },
        },
        [4] = {
            type = "submenu",
            name = "Neutral Targets",
            tooltip = "Settings for neutral targets",	--(optional)
            controls = {
                [1] = {
                    type = "checkbox",
                    name = "Show Name",
                    tooltip = "If enabled, the target name is shown above the reticle",
                    getFunc = function() return RR.SavedVars.neutralNameEnabled end,
                    setFunc = function() RR.SavedVars.neutralNameEnabled = not RR.SavedVars.neutralNameEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [2] = {
                    type = "checkbox",
                    name = "Show Level",
                    tooltip = "If enabled, the target level is shown above the reticle",
                    getFunc = function() return RR.SavedVars.neutralLevelEnabled end,
                    setFunc = function() RR.SavedVars.neutralLevelEnabled = not RR.SavedVars.neutralLevelEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [3] = {
                    type = "checkbox",
                    name = "Show Range",
                    tooltip = "If enabled, the target range is shown at the top-right of the reticle",
                    getFunc = function() return RR.SavedVars.neutralRangeEnabled end,
                    setFunc = function() RR.SavedVars.neutralRangeEnabled = not RR.SavedVars.neutralRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [4] = {
                    type = "checkbox",
                    name = "Show Target Difficulty",
                    tooltip = "If enabled, the target difficulty is shown as *'s above the reticle",
                    getFunc = function() return RR.SavedVars.neutralDifficultyEnabled end,
                    setFunc = function() RR.SavedVars.neutralDifficultyEnabled = not RR.SavedVars.neutralDifficultyEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [5] = {
                    type = "checkbox",
                    name = "Colorize Reticle",
                    tooltip = "If enabled, the color of the target reticle is colorized to reflect range status",
                    getFunc = function() return RR.SavedVars.neutralReticleColoringEnabled end,
                    setFunc = function() RR.SavedVars.neutralReticleColoringEnabled = not RR.SavedVars.neutralReticleColoringEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [6] = {
                    type = "checkbox",
                    name = "Colorize Range",
                    tooltip = "If enabled, the color of the target range is colorized to reflect range status",
                    getFunc = function() return RR.SavedVars.neutralRangeColoringEnabled end,
                    setFunc = function() RR.SavedVars.neutralRangeColoringEnabled = not RR.SavedVars.neutralRangeColoringEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [7] = {
                    type = "checkbox",
                    name = "Colorize Name and Level",
                    tooltip = "If enabled, the color of the name and level is colorized to reflect range status",
                    getFunc = function() return RR.SavedVars.neutralNameLevelColoringEnabled end,
                    setFunc = function() RR.SavedVars.neutralNameLevelColoringEnabled = not RR.SavedVars.neutralNameLevelColoringEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [8] = {
                    type = "checkbox",
                    name = "Out Of Range",
                    tooltip = "If enabled, the reticle color will change when the target is out of range",
                    getFunc = function() return RR.SavedVars.neutralOutOfRangeEnabled end,
                    setFunc = function() RR.SavedVars.neutralOutOfRangeEnabled = not RR.SavedVars.neutralOutOfRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [9] = {
                    type = "colorpicker",
                    name = "Out Of Range Color",
                    tooltip = "The reticle color when the target is out of range",
                    getFunc = function() return RR.SavedVars.neutralOORColor.r, RR.SavedVars.neutralOORColor.g, RR.SavedVars.neutralOORColor.b end,
                    setFunc = function(r,g,b,a)
                        RR.SavedVars.neutralOORColor.r = r
                        RR.SavedVars.neutralOORColor.g = g
                        RR.SavedVars.neutralOORColor.b = b
                        neutralOORColorHex = RR.RGBPercToHex(r,g,b)
				        end,
                    width = "half",	--or "half" (optional)
                },
                [10] = {
                    type = "checkbox",
                    name = "In Range",
                    tooltip = "If enabled, the reticle color will change when the target is in range",
                    getFunc = function() return RR.SavedVars.neutralInRangeEnabled end,
                    setFunc = function() RR.SavedVars.neutralInRangeEnabled = not RR.SavedVars.neutralInRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [11] = {
                    type = "slider",
                    name = "In Range Distance",
                    tooltip = "Set the distance that determines when a target is in range",
                    min = 0,
                    max = 60,
                    step = 1,	--(optional)
                    getFunc = function() return RR.SavedVars.neutralInRangeDistance end,
                    setFunc = function(value) RR.SavedVars.neutralInRangeDistance = value end,
                    width = "half",	--or "half" (optional)
                },
                [12] = {
                    type = "colorpicker",
                    name = "In Range Color",
                    tooltip = "The reticle color when the target is in range",
                    getFunc = function() return RR.SavedVars.neutralIRColor.r, RR.SavedVars.neutralIRColor.g, RR.SavedVars.neutralIRColor.b end,
                    setFunc = function(r,g,b,a)
                        RR.SavedVars.neutralIRColor.r = r
                        RR.SavedVars.neutralIRColor.g = g
                        RR.SavedVars.neutralIRColor.b = b
                        neutralIRColorHex = RR.RGBPercToHex(r,g,b)
                        end,
                    width = "half",	--or "half" (optional)
                },
                [13] = {
                    type = "checkbox",
                    name = "In Melee Range",
                    tooltip = "If enabled, the reticle color will change when the target is in melee range",
                    getFunc = function() return RR.SavedVars.neutralInMeleeRangeEnabled end,
                    setFunc = function() RR.SavedVars.neutralInMeleeRangeEnabled = not RR.SavedVars.neutralInMeleeRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [14] = {
                    type = "slider",
                    name = "In Melee Range Distance",
                    tooltip = "Set the distance that determines when a target is in melee range",
                    min = 0,
                    max = 60,
                    step = 1,	--(optional)
                    getFunc = function() return RR.SavedVars.neutralInMeleeRangeDistance end,
                    setFunc = function(value) RR.SavedVars.neutralInMeleeRangeDistance = value end,
                    width = "half",	--or "half" (optional)
                },
                [15] = {
                    type = "colorpicker",
                    name = "In Melee Range Color",
                    tooltip = "The reticle color when the target is in melee range",
                    getFunc = function() return RR.SavedVars.neutralIMRColor.r, RR.SavedVars.neutralIMRColor.g, RR.SavedVars.neutralIMRColor.b end,
                    setFunc = function(r,g,b,a)
                        RR.SavedVars.neutralIMRColor.r = r
                        RR.SavedVars.neutralIMRColor.g = g
                        RR.SavedVars.neutralIMRColor.b = b
                        neutralIMRColorHex = RR.RGBPercToHex(r,g,b)
                        end,
                    width = "half",	--or "half" (optional)
                },
            },
        },
        [5] = {
            type = "submenu",
            name = "NPC Targets",
            tooltip = "Settings for NPC targets",	--(optional)
            controls = {
                [1] = {
                    type = "checkbox",
                    name = "Show Name",
                    tooltip = "If enabled, the target name is shown above the reticle",
                    getFunc = function() return RR.SavedVars.npcsNameEnabled end,
                    setFunc = function() RR.SavedVars.npcsNameEnabled = not RR.SavedVars.npcsNameEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [2] = {
                    type = "checkbox",
                    name = "Show Level",
                    tooltip = "If enabled, the target level is shown above the reticle",
                    getFunc = function() return RR.SavedVars.npcsLevelEnabled end,
                    setFunc = function() RR.SavedVars.npcsLevelEnabled = not RR.SavedVars.npcsLevelEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [3] = {
                    type = "checkbox",
                    name = "Show Range",
                    tooltip = "If enabled, the target range is shown at the top-right of the reticle",
                    getFunc = function() return RR.SavedVars.npcsRangeEnabled end,
                    setFunc = function() RR.SavedVars.npcsRangeEnabled = not RR.SavedVars.npcsRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [4] = {
                    type = "checkbox",
                    name = "Show Target Difficulty",
                    tooltip = "If enabled, the target difficulty is shown as *'s above the reticle",
                    getFunc = function() return RR.SavedVars.npcsDifficultyEnabled end,
                    setFunc = function() RR.SavedVars.npcsDifficultyEnabled = not RR.SavedVars.npcsDifficultyEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [5] = {
                    type = "checkbox",
                    name = "Colorize Reticle",
                    tooltip = "If enabled, the color of the target reticle is colorized to reflect range status",
                    getFunc = function() return RR.SavedVars.npcsReticleColoringEnabled end,
                    setFunc = function() RR.SavedVars.npcsReticleColoringEnabled = not RR.SavedVars.npcsReticleColoringEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [6] = {
                    type = "checkbox",
                    name = "Colorize Range",
                    tooltip = "If enabled, the color of the target range is colorized to reflect range status",
                    getFunc = function() return RR.SavedVars.npcsRangeColoringEnabled end,
                    setFunc = function() RR.SavedVars.npcsRangeColoringEnabled = not RR.SavedVars.npcsRangeColoringEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [7] = {
                    type = "checkbox",
                    name = "Colorize Name and Level",
                    tooltip = "If enabled, the color of the name and level is colorized to reflect range status",
                    getFunc = function() return RR.SavedVars.npcsNameLevelColoringEnabled end,
                    setFunc = function() RR.SavedVars.npcsNameLevelColoringEnabled = not RR.SavedVars.npcsNameLevelColoringEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [8] = {
                    type = "checkbox",
                    name = "Out Of Range",
                    tooltip = "If enabled, the reticle color will change when the target is out of range",
                    getFunc = function() return RR.SavedVars.npcsOutOfRangeEnabled end,
                    setFunc = function() RR.SavedVars.npcsOutOfRangeEnabled = not RR.SavedVars.npcsOutOfRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [9] = {
                    type = "colorpicker",
                    name = "Out Of Range Color",
                    tooltip = "The reticle color when the target is out of range",
                    getFunc = function() return RR.SavedVars.npcsOORColor.r, RR.SavedVars.npcsOORColor.g, RR.SavedVars.npcsOORColor.b end,
                    setFunc = function(r,g,b,a)
                        RR.SavedVars.npcsOORColor.r = r
                        RR.SavedVars.npcsOORColor.g = g
                        RR.SavedVars.npcsOORColor.b = b
                        npcsOORColorHex = RR.RGBPercToHex(r,g,b)
				        end,
                    width = "half",	--or "half" (optional)
                },
                [10] = {
                    type = "checkbox",
                    name = "In Range",
                    tooltip = "If enabled, the reticle color will change when the target is in range",
                    getFunc = function() return RR.SavedVars.npcsInRangeEnabled end,
                    setFunc = function() RR.SavedVars.npcsInRangeEnabled = not RR.SavedVars.npcsInRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [11] = {
                    type = "slider",
                    name = "In Range Distance",
                    tooltip = "Set the distance that determines when a target is in range",
                    min = 0,
                    max = 60,
                    step = 1,	--(optional)
                    getFunc = function() return RR.SavedVars.npcsInRangeDistance end,
                    setFunc = function(value) RR.SavedVars.npcsInRangeDistance = value end,
                    width = "half",	--or "half" (optional)
                },
                [12] = {
                    type = "colorpicker",
                    name = "In Range Color",
                    tooltip = "The reticle color when the target is in range",
                    getFunc = function() return RR.SavedVars.npcsIRColor.r, RR.SavedVars.npcsIRColor.g, RR.SavedVars.npcsIRColor.b end,
                    setFunc = function(r,g,b,a)
                        RR.SavedVars.npcsIRColor.r = r
                        RR.SavedVars.npcsIRColor.g = g
                        RR.SavedVars.npcsIRColor.b = b
                        npcsIRColorHex = RR.RGBPercToHex(r,g,b)
                        end,
                    width = "half",	--or "half" (optional)
                },
                [13] = {
                    type = "checkbox",
                    name = "In Melee Range",
                    tooltip = "If enabled, the reticle color will change when the target is in melee range",
                    getFunc = function() return RR.SavedVars.npcsInMeleeRangeEnabled end,
                    setFunc = function() RR.SavedVars.npcsInMeleeRangeEnabled = not RR.SavedVars.npcsInMeleeRangeEnabled end,
                    width = "half",	--or "half" (optional)
                },
                [14] = {
                    type = "slider",
                    name = "In Melee Range Distance",
                    tooltip = "Set the distance that determines when a target is in melee range",
                    min = 0,
                    max = 60,
                    step = 1,	--(optional)
                    getFunc = function() return RR.SavedVars.npcsInMeleeRangeDistance end,
                    setFunc = function(value) RR.SavedVars.npcsInMeleeRangeDistance = value end,
                    width = "half",	--or "half" (optional)
                },
                [15] = {
                    type = "colorpicker",
                    name = "In Melee Range Color",
                    tooltip = "The reticle color when the target is in melee range",
                    getFunc = function() return RR.SavedVars.npcsIMRColor.r, RR.SavedVars.npcsIMRColor.g, RR.SavedVars.npcsIMRColor.b end,
                    setFunc = function(r,g,b,a)
                        RR.SavedVars.npcsIMRColor.r = r
                        RR.SavedVars.npcsIMRColor.g = g
                        RR.SavedVars.npcsIMRColor.b = b
                        npcsIMRColorHex = RR.RGBPercToHex(r,g,b)
                        end,
                    width = "half",	--or "half" (optional)
                },
            },
        },
    }

    LAM:RegisterOptionControls(RR.addonName, optionsTable)
end

function RR.GetColor(imrColor, irColor, oorColor, inMeleeRangeEnabled, inRangeEnabled, outOfRangeEnabled, targetInMeleeRange, targetInRange)
    local colorRed = 1
    local colorGreen = 1
    local colorBlue = 1
    -- Check colorize settings
    if inMeleeRangeEnabled and targetInMeleeRange then
        colorRed = imrColor.r
        colorGreen = imrColor.g
        colorBlue = imrColor.b
    elseif inRangeEnabled and targetInRange then
        colorRed = irColor.r
        colorGreen = irColor.g
        colorBlue = irColor.b
    elseif outOfRangeEnabled then
        colorRed = oorColor.r
        colorGreen = oorColor.g
        colorBlue = oorColor.b
    end
    return colorRed, colorGreen, colorBlue
end

function RR.OnRangeUpdate()
    -- Determine if reticle is hidden
    local reticleHidden = IsReticleHidden()
    if reticleHidden then
        return
    end

    -- Get target name and reaction
	local targetReaction = GetUnitReaction('reticleover')
	local targetName = GetUnitName('reticleover')
    local targetClass = GetUnitClass('reticleover') -- TODO
    local targetRank, targetSubRank = GetUnitAvARank('reticleover') -- TODO
    local targetGrouped = IsUnitGrouped('reticleover') -- TODO
    local targetX, targetY, targetH = GetMapPlayerPosition('reticleover')
    local selfX, selfY, selfH = GetMapPlayerPosition('player')

    -- Local variables to store what needs to be done
    local nameEnabled = false
    local levelEnabled = false
    local rangeEnabled = false
	local difficultyEnabled = false
    local reticleColoringEnabled = false
    local rangeColoringEnabled = false
    local nameLevelColoringEnabled = false
    local outOfRangeEnabled = false
    local inRangeEnabled = false
    local inMeleeRangeEnabled = false

    -- Shared information
    local distance = -999
    local oorColor
    local inRangeDistance
    local irColor
    local inMeleeRangeDistance
    local imrColor
    local targetInMeleeRange = false
    local targetInRange = false

    -- Determine target type and if we need to do anything
    if targetReaction and targetName and string.len(targetName) > 0 then

        if RR.targetDebubInfo then
            d("Target X/Y/H: " .. tostring(targetX) .. ', ' .. tostring(targetY) .. ', ' .. tostring(targetH))
            d("Self X/Y/H: " .. tostring(selfX) .. ', ' .. tostring(selfY) .. ', ' .. tostring(selfH))

            d("Target Name: " .. tostring(targetName))
            d("Target Reaction: " .. tostring(targetReaction))
            d("Target Class: " .. tostring(targetClass))
            d("Rank R/SR: " .. tostring(targetRank) .. ', ' .. tostring(targetSubRank))
            d("Target Grouped: " .. tostring(targetGrouped))
        end

		if targetReaction == UNIT_REACTION_HOSTILE then
            nameEnabled = RR.SavedVars.hostileNameEnabled
            levelEnabled = RR.SavedVars.hostileLevelEnabled
            rangeEnabled = RR.SavedVars.hostileRangeEnabled
			difficultyEnabled = RR.SavedVars.hostileDifficultyEnabled
            reticleColoringEnabled = RR.SavedVars.hostileReticleColoringEnabled
            rangeColoringEnabled = RR.SavedVars.hostileRangeColoringEnabled
            nameLevelColoringEnabled = RR.SavedVars.hostileNameLevelColoringEnabled
            outOfRangeEnabled = RR.SavedVars.hostileOutOfRangeEnabled
            inRangeEnabled = RR.SavedVars.hostileInRangeEnabled
            inMeleeRangeEnabled = RR.SavedVars.hostileInMeleeRangeEnabled
            oorColor = RR.SavedVars.hostileOORColor
            inRangeDistance = RR.SavedVars.hostileInRangeDistance
            irColor = RR.SavedVars.hostileIRColor
            inMeleeRangeDistance = RR.SavedVars.hostileInMeleeRangeDistance
            imrColor = RR.SavedVars.hostileIMRColor
        elseif targetReaction == UNIT_REACTION_PLAYER_ALLY then
            nameEnabled = RR.SavedVars.alliesNameEnabled
            levelEnabled = RR.SavedVars.alliesLevelEnabled
            rangeEnabled = RR.SavedVars.alliesRangeEnabled
            reticleColoringEnabled = RR.SavedVars.alliesReticleColoringEnabled
            rangeColoringEnabled = RR.SavedVars.alliesRangeColoringEnabled
            nameLevelColoringEnabled = RR.SavedVars.alliesNameLevelColoringEnabled
            outOfRangeEnabled = RR.SavedVars.alliesOutOfRangeEnabled
            inRangeEnabled = RR.SavedVars.alliesInRangeEnabled
            inMeleeRangeEnabled = RR.SavedVars.alliesInMeleeRangeEnabled
            oorColor = RR.SavedVars.alliesOORColor
            inRangeDistance = RR.SavedVars.alliesInRangeDistance
            irColor = RR.SavedVars.alliesIRColor
            inMeleeRangeDistance = RR.SavedVars.alliesInMeleeRangeDistance
            imrColor = RR.SavedVars.alliesIMRColor
        elseif targetReaction == UNIT_REACTION_NEUTRAL then
            nameEnabled = RR.SavedVars.neutralNameEnabled
            levelEnabled = RR.SavedVars.neutralLevelEnabled
            rangeEnabled = RR.SavedVars.neutralRangeEnabled
			difficultyEnabled = RR.SavedVars.neutralDifficultyEnabled
            reticleColoringEnabled = RR.SavedVars.neutralReticleColoringEnabled
            rangeColoringEnabled = RR.SavedVars.neutralRangeColoringEnabled
            nameLevelColoringEnabled = RR.SavedVars.neutralNameLevelColoringEnabled
            outOfRangeEnabled = RR.SavedVars.neutralOutOfRangeEnabled
            inRangeEnabled = RR.SavedVars.neutralInRangeEnabled
            inMeleeRangeEnabled = RR.SavedVars.neutralInMeleeRangeEnabled
            oorColor = RR.SavedVars.neutralOORColor
            inRangeDistance = RR.SavedVars.neutralInRangeDistance
            irColor = RR.SavedVars.neutralIRColor
            inMeleeRangeDistance = RR.SavedVars.neutralInMeleeRangeDistance
            imrColor = RR.SavedVars.neutralIMRColor
        elseif targetReaction == UNIT_REACTION_NPC_ALLY or UNIT_REACTION_FRIENDLY then
            nameEnabled = RR.SavedVars.npcsNameEnabled
            levelEnabled = RR.SavedVars.npcsLevelEnabled
            rangeEnabled = RR.SavedVars.npcsRangeEnabled
            reticleColoringEnabled = RR.SavedVars.npcsReticleColoringEnabled
            rangeColoringEnabled = RR.SavedVars.npcsRangeColoringEnabled
            nameLevelColoringEnabled = RR.SavedVars.npcsNameLevelColoringEnabled
            outOfRangeEnabled = RR.SavedVars.npcsOutOfRangeEnabled
            inRangeEnabled = RR.SavedVars.npcsInRangeEnabled
            inMeleeRangeEnabled = RR.SavedVars.npcsInMeleeRangeEnabled
            oorColor = RR.SavedVars.npcsOORColor
            inRangeDistance = RR.SavedVars.npcsInRangeDistance
            irColor = RR.SavedVars.npcsIRColor
            inMeleeRangeDistance = RR.SavedVars.npcsInMeleeRangeDistance
            imrColor = RR.SavedVars.npcsIMRColor
        end
    end

    RangeReticleRange:SetText('')
    if rangeEnabled or nameLevelColoringEnabled or reticleColoringEnabled then
        -- Get map details
        local mapType = GetMapType()
        local mapContentType = GetMapContentType()

        -- Calculate and display target range
        if targetX and targetY then
        	local dimensionsX, dimensionsY = ZO_WorldMapContainer:GetDimensions()
        	local multiplier = 5.0

            if mapContentType == MAP_CONTENT_NONE then
                if mapType == MAPTYPE_SUBZONE then
                elseif mapType == MAPTYPE_ZONE then
                end
            elseif mapContentType == MAP_CONTENT_AVA then
                if mapType == MAPTYPE_SUBZONE then
                elseif mapType == MAPTYPE_ZONE then
                	multiplier = 12.0
                end
            elseif mapContentType == MAP_CONTENT_DUNGEON then
                if mapType == MAPTYPE_SUBZONE then
                	dimensionsX, dimensionsY = ZO_WorldMapContainer1:GetDimensions()
                	multiplier = 1.0
                elseif mapType == MAPTYPE_ZONE then
                end
            end

        	if dimensionsX <= 2000 then
        		previousDimensionsX = dimensionsX
        	elseif previousDimensionsX <= 2000 then
        		dimensionsX = previousDimensionsX
        	end
        	if dimensionsY <= 2000 then
        		previousDimensionsY = dimensionsY
        	elseif previousDimensionsY <= 2000 then
        		dimensionsY = previousDimensionsY
        	end

        	if dimensionsX > 2000 or dimensionsY > 2000 then
	       		local X, Y = ZO_WorldMapContainer:GetDimensions()
        		local X1, Y1 = ZO_WorldMapContainer1:GetDimensions()
        		dimensionsX = math.min(X, X1)
        		dimensionsY = math.min(Y, Y1)
        	end

   			local actualSelfX = selfX * dimensionsX
			local actualSelfY = selfY * dimensionsY
			local actualTargetX = targetX * dimensionsX
			local actualTargetY = targetY * dimensionsY
			local dist = math.sqrt(math.pow((actualSelfX - actualTargetX), 2) + math.pow((actualSelfY - actualTargetY), 2)) * multiplier
			distance = math.floor(dist + 0.5)

			-- Disable range functionality when a zone isn't working with range calculation
			if distance > 100 then
				distance = -999
			end

            if distance <= inRangeDistance then
                targetInRange = true
            end
            if distance <= inMeleeRangeDistance then
                targetInMeleeRange = true
            end
            if rangeEnabled and distance >= 0 then
                RangeReticleRange:SetText(tostring(distance) .. 'm')
            elseif rangeEnabled then
            	RangeReticleRange:SetText('e')
            	--d("Dimensions: " .. tostring(dimensionsX) .. ', ' .. tostring(dimensionsY))
            	--d("Multiplier: " .. tostring(multiplier))
            end
        end
    end

    -- Display the name and level/rank
    RangeReticleName:SetText('')
    if nameEnabled or levelEnabled or difficultyEnabled then
        -- Get target name and level/rank
        local nameString = ''
        local levelString = ''
		local difficultyString = ''
        if levelEnabled then
            local targetLevel = GetUnitLevel('reticleover')
            local targetVRank = GetUnitVeteranRank('reticleover')
            if targetVRank and string.len(targetVRank) > 0 and targetVRank ~= 0 then
                levelString = ' (v' .. tostring(targetVRank) .. ')'
            elseif targetLevel and string.len(targetLevel) > 0 then
                levelString = ' (' .. tostring(targetLevel) .. ')'
            end
        end
        if nameEnabled then
            nameString = targetName
        end
		if difficultyEnabled then
			local targetDifficulty = GetUnitDifficulty('reticleover')
			if targetDifficulty > 1 then
				difficultyString = " " .. string.rep("*", (targetDifficulty - 1))
			end
		end
        RangeReticleName:SetText(nameString .. levelString .. difficultyString)
    end

    -- Colorize the various items
    if distance >= 0 then
        local colorRed = 1
        local colorGreen = 1
        local colorBlue = 1
        if nameLevelColoringEnabled then
            colorRed, colorGreen, colorBlue = RR.GetColor(imrColor, irColor, oorColor, inMeleeRangeEnabled, inRangeEnabled, outOfRangeEnabled, targetInMeleeRange, targetInRange)
        end
	    RangeReticleName:SetColor(colorRed, colorGreen, colorBlue)

        colorRed = 1
        colorGreen = 1
        colorBlue = 1
        if reticleColoringEnabled then
            colorRed, colorGreen, colorBlue = RR.GetColor(imrColor, irColor, oorColor, inMeleeRangeEnabled, inRangeEnabled, outOfRangeEnabled, targetInMeleeRange, targetInRange)
        end
        -- Set reticle color
        ZO_ReticleContainerReticle:SetColor(colorRed, colorGreen, colorBlue)
        ZO_ReticleContainerStealthEye:SetColor(colorRed, colorGreen, colorBlue)
        ZO_ReticleContainerStealthText:SetColor(colorRed, colorGreen, colorBlue)

        colorRed = 1
        colorGreen = 1
        colorBlue = 1
        if rangeColoringEnabled then
            colorRed, colorGreen, colorBlue = RR.GetColor(imrColor, irColor, oorColor, inMeleeRangeEnabled, inRangeEnabled, outOfRangeEnabled, targetInMeleeRange, targetInRange)
        end
        RangeReticleRange:SetColor(colorRed, colorGreen, colorBlue)
    else
        -- Set everything to white when distance not available
	    RangeReticleName:SetColor(1, 1, 1)
        RangeReticleRange:SetColor(1, 1, 1)
        ZO_ReticleContainerReticle:SetColor(1, 1, 1)
        ZO_ReticleContainerStealthEye:SetColor(1, 1, 1)
        ZO_ReticleContainerStealthText:SetColor(1, 1, 1)
    end
end

-- Debug info to chat window
function RR.Debug()
    -- Get details of what's under the reticle
    local targetX, targetY, targetH = GetMapPlayerPosition('reticleover')

    -- Get details of the player
    local selfX, selfY, selfH = GetMapPlayerPosition('player')

    -- Get map details
    local mapIndex = GetCurrentMapIndex()
    local mapZoneIndex = GetCurrentMapZoneIndex()
    local mapWidth, mapHeight = GetMapNumTiles()
    local mapType = GetMapType()
    local mapContentType = GetMapContentType()
    local mapCurrentFloor, mapNumFloors =  GetMapFloorInfo()
    local mapName = GetMapName()

    -- Get target details
    local targetReaction = GetUnitReaction('reticleover')
    local targetName = GetUnitName('reticleover')
    local targetClass = GetUnitClass('reticleover') -- TODO
    local targetRank, targetSubRank = GetUnitAvARank('reticleover') -- TODO
    local targetGrouped = IsUnitGrouped('reticleover') -- TODO

    --MAPTYPE_NONE: 0
    --MAPTYPE_SUBZONE: 1
    --MAPTYPE_ZONE: 2
    --MAPTYPE_WORLD: 3
    --MAPTYPE_ALLIANCE: 4
    --MAPTYPE_COSMIC: 5

    --MAP_CONTENT_NONE: 0
    --MAP_CONTENT_AVA: 1
    --MAP_CONTENT_DUNGEON: 2

    d("Map Index: " .. tostring(mapIndex))
    d("Map Zone Index: " .. tostring(mapZoneIndex))
    d("Map W/H: " .. tostring(mapWidth) .. ', ' .. tostring(mapHeight))
    d("Map Type: " .. tostring(mapType))
    d("Map Content Type: " .. tostring(mapContentType))
    d("Floor Cur/Num: " .. tostring(mapCurrentFloor) .. ', ' .. tostring(mapNumFloors))
    d("Map Name: " .. tostring(mapName))
    local dimensionsX, dimensionsY = ZO_WorldMapContainer:GetDimensions()
    d("WMDimensions: " .. tostring(dimensionsX) .. ', ' .. tostring(dimensionsY))
    local dimensionsX1, dimensionsY1 = ZO_WorldMapContainer1:GetDimensions()
    d("1Dimensions: " .. tostring(dimensionsX1) .. ', ' .. tostring(dimensionsY1))
end


EVENT_MANAGER:RegisterForEvent(RR.addonName, EVENT_ADD_ON_LOADED , RR.Initialize)