Quantcast
local initFrames = true
local maxLabels = 40
local maxStatusBars = 40
local groupSetup
local isRaid
local _playerName
-- File containing all of the Event related stuff
function HealWatch.OnEvent(event,...)
	local fcn = HealWatch[event]
	if (type(fcn) == 'function') then
		fcn(...)
	else
		if (debugging) then dframe:AddMessage("Found an unknown event: "..event) end
	end
end

function HealWatch.VARIABLES_LOADED()
	if (HealWatchLabelHeader_Title) then HealWatchLabelHeader_Title:SetText("HealWatch v"..HealWatch.version) end
	if (HealWatchSave) then
		HealWatch.History = HealWatchSave.History
		HealWatch.enableComs = HealWatchSave.enableComs
		HealWatchSave.enableComs = HealWatch.enableComs
		HealWatch.savedComSendMod = HealWatchSave.savedComSendMod
	end
	HealWatch.History = HealWatch.History or {}
	if (HealWatchSave and HealWatchSave.Options) then
		HealWatch.Options = HealWatchSave.Options
	else
		-- default options
		HealWatch.Options = {
			colorHealthFull = { r=0,g=1,b=0 },
			colorHealthNone = { r=1,g=0,b=0 },
			maxCastBars		= 15,
			labelWidth		= 210,
			showBackground	= true,
			LabelGrowDirection = 2,
			showHealth		= true,
			FrameAttachPoint = 'TOP',
			showAllHeals	= false,
			colorMyHeals	= true,
			showMyHeals		= true,
			scale			= 1,
		}
		HealWatch.enableComs = true
		HealWatch.savedComSendMod = 'HealWatch'
	end
	if (not _playerName) then _playerName = UnitName('player') end
	if (not HealWatch.Options.scale) then HealWatch.Options.scale = 1 end
	HealWatchCastFrame:SetScale(HealWatch.Options.scale)
	--HealWatch.RestorePosition()
	if (HealWatch.enableComs) then
		HealWatch.EnableComs()
		for _,comMod in ipairs(HealWatch.comModules) do
			if (comMod.cmdLine == HealWatch.savedComSendMod) then
				HealWatch.EnableComsModule(comMod,nil,true)
			end
		end
	end
	if (not HealWatch.Options.colorHealthFull) then HealWatch.Options.colorHealthFull = { r=0,g=1,b=0 } end
	if (not HealWatch.Options.colorHealthNone) then HealWatch.Options.colorHealthNone = { r=1,g=0,b=0 } end
	if (not HealWatch.Options.maxCastBars) then HealWatch.Options.maxCastBars = 15 end
	if (not HealWatch.Options.labelWidth) then HealWatch.Options.labelWidth = 210 end
	if (not HealWatch.Options.showBackground) then
		local c = {HealWatchCastFrame:GetBackdropColor()}
		local b = {HealWatchCastFrame:GetBackdropBorderColor()}
		local a = 0
		if (HealWatch.Options.showBackground) then a = 1 end
		HealWatchCastFrame:SetBackdropColor(c[1],c[2],c[3],a)
		HealWatchCastFrame:SetBackdropBorderColor(b[1],b[2],b[3],a)
	end
	if (not HealWatch.Health) then HealWatch.Health = {} end
	if (not HealWatch.Health[_playerName]) then HealWatch.Health[_playerName] = {
			['hcur'] = UnitHealth('player'),
			['hmax'] = UnitHealthMax('player'),
			['def'] = UnitHealth('player') - UnitHealthMax('player'),
			['per'] = UnitHealth('player') / UnitHealthMax('player'),
		}
	end
	HealWatchCastFrame.rFrame = HealWatchCastFrame
	this:UnregisterEvent('VARIABLES_LOADED')
	HealWatchCastFrame:SetWidth(HealWatch.Options.labelWidth+6)
	HealWatchLabelHeader:SetWidth(HealWatch.Options.labelWidth)
end

function HealWatch.PLAYER_LOGIN()
	if (not _playerName) then _playerName = UnitName('player') end
	HealWatch._playerName = _playerName
	this:UnregisterEvent('PLAYER_LOGIN')
	if (HealWatch.Options.inRaidOnly) then
		if (UnitExists('party1') or UnitExists('raid1')) then
			HealWatch.EnableEvents()
		else
			HealWatch.DisableEvents()
		end
	end
end

function HealWatch.PLAYER_ENTERING_WORLD()
	if (not _playerName) then _playerName = UnitName('player') end
	HealWatch._playerName = _playerName
	HealWatch.UpdateRaidInfo()
	--this:UnregisterEvent('PLAYER_ENTERING_WORLD')
	if (HealWatch.comModules and HealWatch.comModules['HW']) then
		HealWatch.comModules['HW']['enabled'] = true
	end
	HealWatchCastFrame:ClearAllPoints()
	HealWatchCastFrame:SetPoint('TOP',HealWatchAnchorFrame,'TOP',0,0)
end

function HealWatch.PLAYER_LOGOUT()
	HealWatchSave = {}
	if (HealWatch.Options.saveHistory) then
		HealWatchSave.History = HealWatch.History
	end
	if (debugging) then
		HealWatchSave.RaidSetup = groupSetup
		HealWatchSave.Heals = Heals
	end
	HealWatchSave.Options = HealWatch.Options
	HealWatchSave.scale = HealWatch.Options.scale
	HealWatchSave.enableComs = HealWatch.enableComs
	HealWatchSave.savedComSendMod = (HealWatch.sendModule and HealWatch.sendModule['cmdLine']) or nil
end

function HealWatch.RAID_ROSTER_UPDATE()
	HealWatch.UpdateRaidSetup = true
end

function HealWatch.PARTY_MEMBERS_CHANGED()
	HealWatch.UpdateRaidSetup = true
end

local _raidMembers
local _partyMembers
function HealWatch.UpdateRaidInfo()
	HealWatch.UpdateRaidSetup = false
	local setup = HealWatch.groupSetup or {}
	-- reset the group compositions
	for k,v in pairs(setup) do
		if (type(v) == 'table') then
			for i,val in ipairs(v) do v[i] = nil end
		end
		v.inGroup = nil
		v.index = -1
	end
	_raidMembers = GetNumRaidMembers()
	_partyMembers = GetNumPartyMembers()
	if (not setup['group IDs']) then setup['group IDs'] = {} end
	if (_raidMembers > 0) then
		if (not HealWatch.Options.enabled) then HealWatch.Enable() end
		for i = 1,GetNumRaidMembers() do
			local name,_,group = GetRaidRosterInfo(i)
			if (name) then
				if (not setup[name]) then setup[name] = {} end
				setup[name]["index"] = i
				setup[name]['inGroup'] = true
				setup['group IDs']['raid'..i] = name
				if (group) then
					setup[name]["group"] = group
					if (not setup[group]) then setup[group] = {} end
					table.insert(setup[group],name)
				end
			end
		end
		if (not isRaid) then
			isRaid = true
		end
	elseif (_partyMembers > 0) then
		for i=1,GetNumPartyMembers() do
			local name = UnitName("party"..i)
			if (name) then
				if (not setup[name]) then setup[name] = {} end
				setup[name]["index"] = i
				setup[name]['inGroup'] = true
				setup[name]["group"] = nil
				setup['group IDs']['party'..i] = name
			end
		end
		isRaid = nil
	else
		isRaid = nil
	end
	-- clean the old units out of the roster
	for k,v in pairs(setup) do
		if (not v.inGroup) then
			setup[k] = nil
		end
	end
	if (not (UnitExists('party1') or UnitExists('raid1'))) then
		-- only hide when not in a group
		HealWatchCastFrame:Hide()
	else
		HealWatchCastFrame:Show()
	end
	HealWatch.groupSetup = setup
	groupSetup = HealWatch.groupSetup
	HealWatch.isRaid = isRaid
	if (HealWatch.Options.inRaidOnly) then
		if (not HealWatch.Options.enabled and HealWatch.isRaid) then
			HealWatch.EnableEvents()
		elseif (HealWatch.Options.enabled and not HealWatch.isRaid) then
			HealWatch.DisableEvents()
			HealWatchCastFrame:Hide()
		end
	end
end

-- arg1 = Spell Name
-- arg2 = cast time in milliseconds
function HealWatch.UNIT_HEALTH(unit)
	if (not unit) then return end
	local hlth,hmax,hcur,name
	if (HealWatch.groupSetup and HealWatch.groupSetup['group IDs']) then
		name = HealWatch.groupSetup['group IDs'][unit]
		if (unit == 'player') then name = _playerName end
	end
	-- don't know who this is, don't care
	if (not name) then name = UnitName(unit) end
	if (not name) then return end
	if (not HealWatch.Health) then HealWatch.Health = {} end
	hlth = HealWatch.Health[name]
	hlth = HealWatch.Health[name] or {}
	HealWatch.Health[name] = hlth
	hmax = hlth.hmax or UnitHealthMax(unit)
	hlth.hmax = hmax
	hcur = UnitHealth(unit)
	hlth.hcur = hcur
	hlth.def = hcur - hmax
	hlth.per = hcur/hmax
	if (HealWatch.UpdateHealth) then HealWatch.UpdateHealth(name,hcur,hmax,hcur-hmax) end
end

function HealWatch.UNIT_MAXHEALTH(unit)
	if (not unit) then return end
	local name,hlth,hmax,hcur
	if (HealWatch.groupSetup and HealWatch.groupSetup['group IDs']) then
		name = HealWatch.groupSetup['group IDs'][unit]
		if (unit == 'player') then name = _playerName end
	end
	if (not HealWatch.Health) then HealWatch.Health = {} end
	if (not name) then name = UnitName(unit) end
	hlth = HealWatch.Health[name] or {}
	HealWatch.Health[name] = hlth
	-- make sure we have the health info about <name>
	hlth.hmax = UnitHealthMax(unit)
	hmax = hlth.hmax
	hcur = hlth.hcur or UnitHealth(unit)
	hlth.def = hcur - hmax
	hlth.per = hcur/hmax
	if (HealWatch.UpdateHealth) then HealWatch.UpdateHealth(name,hcur,hmax,hcur-hmax) end
end