-- ----------------------------------------------------------------------------
-- GridStatusRaidIcons by kunda
-- ----------------------------------------------------------------------------
local RL = AceLibrary("Roster-2.1")

GridStatusRaidIcons = GridStatus:NewModule("GridStatusRaidIcons")
GridStatusRaidIcons.menuName = RAID_TARGET_ICON

local LOC_PLAYER       = RAID_TARGET_ICON..": "..PLAYER
local LOC_PLAYERTARGET = RAID_TARGET_ICON..": "..PLAYER.." "..TARGET
local icontext = {
	[1] = RAID_TARGET_1, -- Star
	[2] = RAID_TARGET_2, -- Circle
	[3] = RAID_TARGET_3, -- Diamond
	[4] = RAID_TARGET_4, -- Triangle
	[5] = RAID_TARGET_5, -- Moon
	[6] = RAID_TARGET_6, -- Square
	[7] = RAID_TARGET_7, -- Cross
	[8] = RAID_TARGET_8  -- Skull
}
local icontextcolor = {
	[1] = {r = 1.0, g = 0.92, b = 0},     -- Yellow 4-point Star
	[2] = {r = 0.98, g = 0.57, b = 0},    -- Orange Circle
	[3] = {r = 0.83, g = 0.22, b = 0.9},  -- Purple Diamond
	[4] = {r = 0.04, g = 0.95, b = 0},    -- Green Triangle
	[5] = {r = 0.7, g = 0.82, b = 0.875}, -- White Crescent Moon
	[6] = {r = 0, g = 0.71, b = 1},       -- Blue Square
	[7] = {r = 1.0, g = 0.24, b = 0.168}, -- Red 'X' Cross
	[8] = {r = 0.98, g = 0.98, b = 0.98}  -- White Skull
}

GridStatusRaidIcons.defaultDB = {
	debug = false,
	alert_raidicons_player = {
		text =  LOC_PLAYER,
		enable = true,
		color = { r = 1, g = 1, b = 1, a = 0.4 },
		priority = 50,
		range = false,
	},
	alert_raidicons_playertarget = {
		text =  LOC_PLAYERTARGET,
		enable = true,
		color = { r = 1, g = 1, b = 1, a = 0.4 },
		priority = 50,
		range = false,
	}
}

GridStatusRaidIcons.options = false

function GridStatusRaidIcons:OnInitialize()
	self.super.OnInitialize(self)
	self:RegisterStatus("alert_raidicons_player", LOC_PLAYER, nil, true)
	self:RegisterStatus("alert_raidicons_playertarget", LOC_PLAYERTARGET, nil, true)
end

function GridStatusRaidIcons:OnEnable()
	self:RegisterEvent("RAID_TARGET_UPDATE", "Update")
	self:RegisterEvent("UNIT_TARGET")
	self:RegisterEvent("PARTY_MEMBERS_CHANGED", "Update")
end

function GridStatusRaidIcons:Update()
	for u in RL:IterateRoster(true) do
		self:UNIT_TARGET(u.unitid)
	end
end

function GridStatusRaidIcons:UNIT_TARGET(e_unitid)
	local settings1 = self.db.profile.alert_raidicons_player
	local settings2 = self.db.profile.alert_raidicons_playertarget
	local unitname = UnitName(e_unitid)
	local raidicon = GetRaidTargetIndex(e_unitid)
	local targetid

	-- group-/raidmember
	if settings1.enable and type(raidicon) == "number" then
		self.core:SendStatusGained(unitname, "alert_raidicons_player",
			settings1.priority,
			(settings1.range and 40),
			{ r = icontextcolor[raidicon].r, g = icontextcolor[raidicon].g, b = icontextcolor[raidicon].b, a = settings1.color.a },
			icontext[raidicon],
			nil,
			nil,
			"Interface\\AddOns\\GridStatusRaidIcons\\icons\\icon".. raidicon ..".tga")
	else
		self.core:SendStatusLost(unitname, "alert_raidicons_player")
	end

	-- group-/raidmember target
	if settings2.enable and UnitExists(e_unitid.."target") then
		targetid = e_unitid.."target"
		raidicon = GetRaidTargetIndex(targetid)
		if type(raidicon) == "number" then
			self.core:SendStatusGained(unitname, "alert_raidicons_playertarget",
				settings2.priority,
				(settings2.range and 40),
				{ r = icontextcolor[raidicon].r, g = icontextcolor[raidicon].g, b = icontextcolor[raidicon].b, a = settings2.color.a },
				icontext[raidicon],
				nil,
				nil,
				"Interface\\AddOns\\GridStatusRaidIcons\\icons\\icon".. raidicon ..".tga")
		else
			self.core:SendStatusLost(unitname, "alert_raidicons_playertarget")
		end
	else
		self.core:SendStatusLost(unitname, "alert_raidicons_playertarget")
	end
end