Quantcast
-- Global information

classDisplayName, class, classID = UnitClass("player");
PlayerClass = classID;
PlayerClassName = classDisplayName;
PlayerClassNameIndex = class;
PH_v = '3.0'; -- version number
limit1 = 0; -- limiter for sounds
executeSound = "Sound\\interface\\AuctionWindowClose.ogg";

-- check for sound enabled
if PH_sound == nil then
  PH_sound = 0;
end

-- check for display enabled
if PH_on == nil then
  PH_on = 1;
end


PH_p = {}			-- Class:			    ID:
PH_p[1] = 20		-- Warrior 			1
PH_p[2] = 20 		-- Paladin 			2
PH_p[3] = 20 		-- Hunter 			3
PH_p[4] = 35 		-- Rogue			  4
PH_p[5] = 20 		-- Priest			  5
PH_p[6] = 35 		-- Death Knight	6
PH_p[7] = 10 		-- Shaman			  7
PH_p[8] = 10 		-- Mage				  8
PH_p[9] = 25 		-- Warlock			9
PH_p[10] = 10 	-- Monk				  10
PH_p[11] = 25 	-- Druid			  11

-- Welcome message
function PH_OnLoad()
  SlashCmdList["PH"] = PH_SlashCommandHandler; -- Add our slash command handler to the list of slash commands
  SLASH_PH1 = "/ph"; -- Associate /ph with the slash command list entry for PORTRAITHEALTH
	if(IsAddOnLoaded("PortraitHealth")) then
		DEFAULT_CHAT_FRAME:AddMessage("|cffffff00Portrait Health "..PH_v.."|r loaded. Type |cff00aaff/ph|r for a list of commands.");
	else
	end
end

-- Slash Commands
function PH_SlashCommandHandler( msg, class )
	local class = PlayerClass;
	local className = PlayerClassName;
	local x = RAID_CLASS_COLORS[PlayerClassNameIndex];

-- show commands
if msg == '' then
  DEFAULT_CHAT_FRAME:AddMessage( "|cffffff00Portrait Health "..PH_v.."|r commands:");
  DEFAULT_CHAT_FRAME:AddMessage( "Usage: |cff00aaff/ph <command>|r.");
  DEFAULT_CHAT_FRAME:AddMessage(" - |cff00aaffshow|r to toggle health display.");
  DEFAULT_CHAT_FRAME:AddMessage(" - |cff00aaffsound|r to toggle sound at execute phase.");
  DEFAULT_CHAT_FRAME:AddMessage(" - |cff00aaffinfo|r to show current settings.");
end

-- health display toggle
if msg == 'show' then
  if PH_on == 1 then
    PortraitHealth:Hide();
    PH_on = 0;
    DEFAULT_CHAT_FRAME:AddMessage("Portrait Health |cff00aaffdisplay disabled|r.")
  elseif PH_on == 0 then
    PortraitHealth:Show();
    PH_on = 1;
    DEFAULT_CHAT_FRAME:AddMessage("Portrait Health |cff00aaffdisplay enabled|r.");
  end
end

-- sound toggle
if msg == 'sound' then
  if PH_sound == 1 then
    PH_sound = 0;
    DEFAULT_CHAT_FRAME:AddMessage("Portrait Health |cff00aaffsound disabled|r.");
  elseif PH_sound == 0 then
    PH_sound = 1;
    DEFAULT_CHAT_FRAME:AddMessage("Portrait Health |cff00aaffsound enabled|r.");
  end
end

-- show current settings
if msg == 'info' then
  DEFAULT_CHAT_FRAME:AddMessage("Addon version |cFFFFFF00" ..PH_v.. "|r");
  DEFAULT_CHAT_FRAME:AddMessage("Execute at |cFFFF0000"..PH_p[class].."%|r \(|c"..x.colorStr..""..PlayerClassName.."|r\)");

  if PH_on == 1 then
    DEFAULT_CHAT_FRAME:AddMessage("Health display is |cff00aaffon|r.");
  else
    DEFAULT_CHAT_FRAME:AddMessage("Health display is |cff00aaffoff|r.");
  end

  if PH_sound == 1 then
    DEFAULT_CHAT_FRAME:AddMessage("Sound is |cff00aaffon|r.");
  else
    DEFAULT_CHAT_FRAME:AddMessage("Sound is |cff00aaffoff|r.");
  end
end
end

-- health display set by user prefs
function PH_display()
  if PH_on == 0 then
    PortraitHealth:Hide();
  else
    PortraitHealth:Show();
  end
end

-- Set the text color and update health output
function HealthPercentage(event)

	-- Health value to display. Whole numbers only.
	TargetPercentHealth = (UnitHealth("target") / UnitHealthMax("target") * 100);

  -- Reset notifier when target changes or the target's health increases beyond the execute range
  if event == "PLAYER_TARGET_CHANGED" then
    limit1 = 0;
  end

  if TargetPercentHealth > PH_p[classID] then
    limit1 = 0;
  end


	-- Class detection

	if PlayerClass == 1 and TargetPercentHealth < 20 then 			-- Warrior
		PortraitHealth_Health:SetTextColor(1,0,0);
    if not UnitIsDead("target") and limit1 == 0 and PH_sound == 1 then
      PlaySoundFile(executeSound);
      limit1 = 1;
    end

		elseif PlayerClass == 2 and TargetPercentHealth <= 20 then 	-- Paladin
				PortraitHealth_Health:SetTextColor(1,0,0);
        if not UnitIsDead("target") and limit1 == 0 and PH_sound == 1 then
          PlaySoundFile(executeSound);
          limit1 = 1;
        end

		elseif PlayerClass == 3 and TargetPercentHealth <= 20 then 	-- Hunter
				PortraitHealth_Health:SetTextColor(1,0,0);
        if not UnitIsDead("target") and limit1 == 0 and PH_sound == 1 then
          PlaySoundFile(executeSound);
          limit1 = 1;
        end

		elseif PlayerClass == 4 and TargetPercentHealth < 35 then 	-- Rogue
				PortraitHealth_Health:SetTextColor(1,0,0);
        if not UnitIsDead("target") and limit1 == 0 and PH_sound == 1 then
          PlaySoundFile(executeSound);
          limit1 = 1;
        end

		elseif PlayerClass == 5 and TargetPercentHealth < 20 then 	-- Priest
				PortraitHealth_Health:SetTextColor(1,0,0);
        if not UnitIsDead("target") and limit1 == 0 and PH_sound == 1 then
          PlaySoundFile(executeSound);
          limit1 = 1;
        end

		elseif PlayerClass == 6 and TargetPercentHealth < 35 then 	-- Death Knight
				PortraitHealth_Health:SetTextColor(1,0,0);
        if not UnitIsDead("target") and limit1 == 0 and PH_sound == 1 then
          PlaySoundFile(executeSound);
          limit1 = 1;
        end

		elseif PlayerClass == 9 and TargetPercentHealth < 20 then 	-- Warlock
				PortraitHealth_Health:SetTextColor(1,0,0);
        if not UnitIsDead("target") and limit1 == 0 and PH_sound == 1 then
          PlaySoundFile(executeSound);
          limit1 = 1;
        end

		elseif PlayerClass == 10 and TargetPercentHealth <= 10 then -- Monk
				PortraitHealth_Health:SetTextColor(1,0,0);
        if not UnitIsDead("target") and limit1 == 0 and PH_sound == 1 then
          PlaySoundFile(executeSound);
          limit1 = 1;
        end

		elseif PlayerClass == 11 and TargetPercentHealth <= 25 then -- Druid
				PortraitHealth_Health:SetTextColor(1,0,0);
        if not UnitIsDead("target") and limit1 == 0 and PH_sound == 1 then
          PlaySoundFile(executeSound);
          limit1 = 1;
        end

		elseif TargetPercentHealth <= 10 then						-- Other classes
				PortraitHealth_Health:SetTextColor(1,0,0);
        if not UnitIsDead("target") and limit1 == 0 and PH_sound == 1 then
          PlaySoundFile(executeSound);
          limit1 = 1;
        end

	else
		PortraitHealth_Health:SetTextColor(1,1,0);
	end

	-- reformat health without decimals
	local h = string.format("%.0f",TargetPercentHealth);
	local i = string.format("%.1f",TargetPercentHealth);

	-- change the portrait to show current health
	if UnitIsDead("target") then
		PortraitHealth_Health:SetText('Dead');
	elseif
		TargetPercentHealth >= 1 then PortraitHealth_Health:SetText(h .. '%');
	elseif
		TargetPercentHealth < 1 then PortraitHealth_Health:SetText(i .. '%');

	end

end