Quantcast
--[[-------------------------------------------------------------------------
  *
  * IncomingHeals module for PerfectRaid addon.
  *
  * Written by: Panoramix
  * Version: 1.0
  *
---------------------------------------------------------------------------]]


local IncomingHeals = PerfectRaid:NewModule("PerfectRaid-IncomingHeals")

local L = PerfectRaidLocals
local utils, frames

function IncomingHeals:Initialize()

	frames = PerfectRaid.frames
	utils = PerfectRaid.utils

	self:RegisterMessage("DONGLE_PROFILE_CHANGED")
	self:RegisterMessage("PERFECTRAID_CONFIG_CHANGED")
end


function IncomingHeals:DONGLE_PROFILE_CHANGED(event, addon, svname, name)
	if svname == "PerfectRaidDB" then
		IncomingHeals:EnableIncomingHeals(PerfectRaid.db.profile.showincomingheals)
	end
end

function IncomingHeals:PERFECTRAID_CONFIG_CHANGED(event, addon, svname, name)
	IncomingHeals:EnableIncomingHeals(PerfectRaid.db.profile.showincomingheals)
end

function IncomingHeals:Enable()
	IncomingHeals:EnableIncomingHeals(PerfectRaid.db.profile.showincomingheals)
end

function IncomingHeals:EnableIncomingHeals(value)
	if value then
		self:RegisterEvent("UNIT_HEAL_PREDICTION", "UpdateIncomingHeals")
	else
		self:UnregisterEvent("UNIT_HEAL_PREDICTION", "UpdateIncomingHeals")
	end
end


function IncomingHeals:ConfigureButton( button )

	local bar = CreateFrame("StatusBar", nil, button)
	button.incominghealsbar = bar

end

function IncomingHeals:UpdateButtonLayout( button )

	button.incominghealsbar:ClearAllPoints()
	button.incominghealsbar:SetPoint("TOPLEFT", button.leftbox, "TOPRIGHT", 0, -1)
	button.incominghealsbar:SetPoint("BOTTOMRIGHT", button.rightbox, "BOTTOMLEFT", 0, 1)
	button.incominghealsbar:SetStatusBarTexture("Interface\\AddOns\\PerfectRaid\\images\\smooth")
	button.incominghealsbar:SetFrameLevel( button.healthbar:GetFrameLevel()-1 )
	button.incominghealsbar:SetStatusBarColor( 0.3, 0.5, 0.3 )
	button.incominghealsbar:Hide()

end

function IncomingHeals:UpdateIncomingHeals( event, target )

	-- not the right unit
	if target == "target" then return end

	local health = UnitHealth(target)
	local maxhealth = UnitHealthMax(target)
	local healinc = UnitGetIncomingHeals(target)

	-- not correct healinc or health
	if health == null or healinc == null then return end

	local healthsum = health + healinc

	-- adjust healthsum to maxhealth
	if healthsum > maxhealth then healthsum = maxhealth end

	for unit, tbl in pairs(frames) do

		if UnitIsUnit( target, unit ) then

			for frame in pairs(frames[unit]) do

				if healinc == 0 or health == maxhealth then
					frame.incominghealsbar:Hide()
				else
					frame.incominghealsbar:SetMinMaxValues(0, maxhealth)
					frame.incominghealsbar:SetValue(healthsum)
					frame.incominghealsbar:Show()
				end
			end

			break
		end
	end


end