Quantcast
local HW = {
	["START"] = 1,
	["STOP"] = 2,
	["UPDATE"] = 3,
	["HEAL"] = 4,
	["VERSQUEST"] = 5,
	['VERSRESP'] = 6,
}
local _msg,_spellIndex
local _,_amt,_grp,_prio
if (not HealWatch) then HealWatch = {} end

function HealWatch.EnableComs()
	HealWatch.frame:RegisterEvent('CHAT_MSG_ADDON')
	HealWatch.enableComs = true
end

function HealWatch.DisableComs()
	HealWatch.frame:UnregisterEvent('CHAT_MSG_ADDON')
	HealWatch.enableComs = false
end

-- provided as the default message in HealWatch
function HealWatch.SendHWMessage(target,spell,status,time,rank)
	-- only send if we are enabled and stuff
	if (not (target and spell and HealWatch.enableComs)) then return end
	if (not (status == HW['VERSRESP'] or status == HW['VERSQUEST'])) then
	end
	if (HealWatch.customSend) then
		HealWatch.customSend(status or HW["START"],target,spell)
		return
	end
	_spellIndex = HealWatch.spells[spell]
	if (status == HW['VERSRESP']) then
		_msg = string.format("%d",status or 0)
	elseif (status == HW['VERSQUEST']) then
		_msg = string.format("%d%s",status or 0,HealWatch.version)
	else
		_msg = string.format("%d%d%s",status or 0,_spellIndex or 0,target or '-')
	end
	_grp = (HealWatch.isRaid and 'RAID') or 'PARTY'
	if (ChatThrottleLib) then
		_prio = 'ALERT'
		-- don't prioritize version stuff
		if (status == HW['VERSRESP'] or status == HW['VERSQUEST']) then _prio='BULK' end
		ChatThrottleLib:SendAddonMessage(_prio,"HW",_msg,_grp)
	else
		SendAddonMessage('HW',_msg,_grp)
	end
end

function HealWatch.CHAT_MSG_ADDON(prefix,msg,dest,player)
	-- ignore self messages
	if (player == HealWatch._playerName) then return end
	-- jump out if not in the right 'channel'
	if (not (dest == 'RAID' or dest == 'PARTY')) then return end
	-- see if we have a registered function for this prefix
	local module = HealWatch.comModules[(prefix or '')]
	-- if so, and it is valid, call it
	if (module and module.enabled) then
		local func = module['recv']
		if (func and type(func) == 'function') then func(player,msg) end
	end
end

-- here we provide a basic plugin module
local function ProcessHWMessage(nick,message)
	-- we checked to see if we are enabled
	if (HealWatch.debugComs) then
		HealWatch.dframe:AddMessage("Received HW message: ["..nick.."]: "..message)
	end
	-- Break message apart into spellname, target,etc.
	-- Assume for now that properly formatted message looks like this:
	-- e.g., [<Person>]: HW <status><spell><time><target>
	--_msg = string.format("%d%d%.3f%s",status or 0,_spellIndex or 0,time or 0,target or '-')
	local _,_,status,spellIndex,spellTarget = string.find(message,"^(%d)(.)(%a+)$")
	if (not status) then
		_,_,status,spellTarget = string.find(message,'^(%d)(.*)$')
	end
	status = tonumber(status)
	if (HealWatch.debugComs) then HealWatch.dframe:AddMessage(string.format("Sender: %s Spell: %s Status: %s Target: %s Type: %s",nick or 'nick',HealWatch.spells[spellIndex] or 'spell',status or 'status',spellTarget or 'spellTarget',type(timeRemain) or 'type(status)')) end
	if (status == HW["START"]) then
		local unit = HealWatch.GetUnitFromName(nick)
		local spell, rank, displayName, icon, startTime, endTime, isTradeSkill = UnitCastingInfo(unit)
		HealWatch.AddHeal(nick,spellTarget,spell,rank,startTime,endTime)
		HealWatch.hasComs[nick] = true
	elseif (status == HW["UPDATE"]) then
		local unit = HealWatch.GetUnitFromName(nick)
		local spell, rank, displayName, icon, startTime, endTime, isTradeSkill = UnitCastingInfo(unit)
		HealWatch.AddHeal(nick,spellTarget,spell,rank,startTime,endTime,true)
		HealWatch.AddHeal(nick,spellTarget,HealWatch.spells[spellIndex],timeRemain,true)
		HealWatch.hasComs[nick] = true
	elseif (status == HW["STOP"]) then
		local unit = HealWatch.GetUnitFromName(nick)
		local spell, rank, displayName, icon, startTime, endTime, isTradeSkill = UnitCastingInfo(unit)
		local spell = HealWatch.spells[spellIndex]
		HealWatch.RemHeal(nick,spellTarget,spell)
		HealWatch.hasComs[nick] = true
		-- disable to fix
	elseif (status == HW["HEAL"]) then
		local spell = HealWatch.spells[spellIndex]
		HealWatch.RemHeal(nick,spellTarget,spell)
		HealWatch.hasComs[nick] = true
	elseif (status == HW["VERSRESP"]) then
		DEFAULT_CHAT_FRAME:AddMessage("Nick: "..nick..' using version: '..spellTarget)
		HealWatch.hasComs[nick] = true
	elseif (status == HW["VERSQUEST"]) then
		HealWatch.SendHWMessage(HealWatch.version,0,HW["VERSRESP"],0)
		HealWatch.hasComs[nick] = true
	end
	return nil
end

local function SendHWMessage(status,target,spell,time,amt)
	if (not (target and spell and time and status)) then return end
	_spellIndex = HealWatch.spells[spell]
	if (status == HW['VERSRESP']) then
		_msg = string.format("%d",status or 0)
	elseif (status == HW['VERSQUEST']) then
		_msg = string.format("%d%s",status or 0,HealWatch.version)
	else
		_msg = string.format("%d%d%s",status or 0,_spellIndex or 0,target or '-')
	end
	_grp = 'RAID'
	if (ChatThrottleLib) then
		_prio = 'ALERT'
		-- don't prioritize version stuff
		if (status == HW['VERSRESP'] or status == HW['VERSQUEST']) then _prio='BULK' end
		ChatThrottleLib:SendAddonMessage(_prio,"HW",_msg,_grp)
	else
		SendAddonMessage('HW',_msg,_grp)
	end
end

local module = {
	['cmdLine'] = 'HealWatch',
	['menuLine'] = 'HealWatch',
	['prefix'] = 'HW',
	['recv'] = ProcessHWMessage,
	['send'] = SendHWMessage,
	['enabled'] = true,
	-- enable the HW module by default
}
if (not HealWatch.comModules) then HealWatch.comModules = {} end
table.insert(HealWatch.comModules,module)
HealWatch.comModules['HW'] = module
HealWatch.comModules['HW']['enabled'] = true
HealWatch.sendModule = module
HealWatch.customSend = module.send