local L = AceLibrary("AceLocale-2.2"):new("GridStatusChainWho")
GridStatusChainWho = GridStatus:NewModule("GridStatusChainWho")

local GridStatusChainWho = GridStatusChainWho

local GSCWChainHealName = GetSpellInfo( 1064 )
local GSCWCoHName = GetSpellInfo( 34861 )
local GSCWHolyNovaName = GetSpellInfo( 15237 )

GridStatusChainWho.menuName = L["Chain Who?"]
GridStatusChainWho.Timers = {}

-- Defaults
GridStatusChainWho.defaultDB = {
	debug = false,
	alert_chainwho = {
		enable = true,
		color = { r = 1.0, g = 1.0, b = 0.6, a = 1 },
		crit_color = { r = 0.4, g = 1.0, b = 0.6, a = 1 },
		duration = 5,
		priority = 90,
	}
}

local settings
GridStatusChainWho.options = false

local gscwOptions = {
    ["crit_color"] = {
		type = "color",
		name = L["Critical color"],
		desc = L["Color for a critical strike"],
        order = 90,
        hasAlpha = true,
        get = function ()
                local color = settings.alert_chainwho.crit_color
                return color.r, color.g, color.b, color.a
        		end,
        set = function (r, g, b, a)
                local color = settings.alert_chainwho.crit_color
                color.r = r
                color.g = g
                color.b = b
                color.a = a or 1
        		end,
	},
    ["duration"] = {
		type = "range",
		name = L["Duration"],
		desc = L["How long to highlight a unit, given in number of quarter second increments (4 = at least 1 second)"],
        max = 20,
        min = 1,
        step = 1,
		get = function()
			return settings.alert_chainwho.duration
		end,
		set = function(v)
			settings.alert_chainwho.duration = v
		end,
	},
}

function GridStatusChainWho:OnInitialize()
	self.super.OnInitialize(self)
	self:Debug("OnInitialize")
    settings = GridStatusChainWho.db.profile
	self:RegisterStatus( "alert_chainwho", L["Chain Who?"], gscwOptions, true )
end

function GridStatusChainWho:OnStatusEnable( status )
	self:Debug( "OnStatusEnable" )
	if status == "alert_chainwho" then
		self:ScheduleRepeatingEvent("GSCW_Refresh", self.RemoveMark, 0.25, self )
		self:RegisterEvent( "COMBAT_LOG_EVENT_UNFILTERED", "CombatLogEvent" )
	end
end

function GridStatusChainWho:OnStatusDisable( status )
	self:Debug( "OnStatusDisable" )
	if status == "alert_chainwho" then
		self:CancelScheduledEvent( "GSCW_Refresh" )
		self:UnregisterAllEvents( )
		self.core:SendStatusLostAllUnits( "alert_chainwho" )
	end
end

function GridStatusChainWho:Reset()
	self:Debug("Reset")
	self.super.Reset(self)
	self:OnDisable()
	self:OnEnable()
end

function GridStatusChainWho:RemoveMark( )
	local index = 0
	for guid,time in pairs(self.Timers) do
		time = time - 1
		index = index + 1
		if time < 0 then
			self.core:SendStatusLost( guid, "alert_chainwho" )
			self.Timers[ guid ] = nil
			self:Debug( "Removing timer: "..guid )
		else
			self.Timers[ guid ] = time
		end
	end 
end

function GridStatusChainWho:CombatLogEvent(timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, spellId, spellName, spellSchool, amount, overheal, absorbed, critical)
	if bit.band( srcFlags, COMBATLOG_OBJECT_TYPE_MASK ) ~= COMBATLOG_OBJECT_TYPE_PLAYER then
		return
	end

	if bit.band( srcFlags, COMBATLOG_OBJECT_AFFILIATION_MASK ) ~= COMBATLOG_OBJECT_AFFILIATION_MINE then
		return
	end

	if eventtype == "SPELL_HEAL" then
		if spellName == GSCWChainHealName or spellName == GSCWCoHName or spellName == GSCWHolyNovaName then
			if critical == nil then
				self:Debug( spellName.." hit on "..dstName.." for "..amount )
			    self.core:SendStatusGained( dstGUID, "alert_chainwho",
			                               settings.alert_chainwho.priority,
			                               nil,
			                               settings.alert_chainwho.color,
			                               L["Chain Who?"],
			                               nil, nil,
			                               false )
			else
				self:Debug( spellName.." crit on "..dstName.." for "..amount )
			    self.core:SendStatusGained( dstGUID, "alert_chainwho",
			                               settings.alert_chainwho.priority,
			                               nil,
			                               settings.alert_chainwho.crit_color,
			                               L["Chain Who?"],
			                               nil, nil,
			                               false )
			end
			self:Debug( "Starting timer: "..dstGUID.." of "..settings.alert_chainwho.duration.." ticks." )
			self.Timers[ dstGUID ] = settings.alert_chainwho.duration
		end
	end
end
