--[[
 Combat Caller
    By Alex Brazie
  
  Automates Low-HP and Out-Of-Mana calls

  This was written for testing of the shared
  configuration module I'm writing. 
  (But first, I need a module to configure!) 
  
  ]]--

-- These will the the values with checkboxes
CombatCaller_HealthCall = true;
CombatCaller_ManaCall = true;
CombatCaller_Cooldown = true;

-- These will be the variable variables. ;)
CombatCaller_HealthRatio = .4;
CombatCaller_HealthLimit = 100;
CombatCaller_ManaRatio = .3;
CombatCaller_ManaLimit = 150;
CombatCaller_CooldownTime = 30;

function CombatCaller_OnLoad() 
	this:RegisterEvent("UNIT_HEALTH");
	this:RegisterEvent("UNIT_MANA");
	this:RegisterEvent("PLAYER_ENTER_COMBAT");
	this:RegisterEvent("PLAYER_LEAVE_COMBAT");
	
	this.hp = -1;
	this.mana = -1;
	this.lasthp = -1;
	this.lastmana = -1;
	this.InCombat = 0;
	this.IsGhost = 0;
	this.LastHPShout = 0;
	this.LastManaShout = 0;

	if ( Cosmos_RegisterConfiguration ) then
	-- Register with the CosmosMaster
	Cosmos_RegisterConfiguration(
		"COS_COMCALLER",
		"SECTION",
		COMBATC_SEP,
		COMBATC_SEP_INFO
		);
	Cosmos_RegisterConfiguration(
		"COS_COMCALLER_HEADER",
		"SEPARATOR",
		COMBATC_SEP,
		COMBATC_SEP_INFO
		);
 	Cosmos_RegisterConfiguration(
 		"COS_COMCALLER_HEALTHSLIMIT", --CVar
 		"BOTH",									 --Things to use
 		COMBATC_HEALTH,			 --Simple String
 		COMBATC_HEALTH_INFO,
 												 --Description 
 		CombatCaller_OnHealthConfigUpdate,		 --Callback
 		0,										 --Default Checked/Unchecked
 		.2,										 --Default Value
 		0,										 --Min value
 		1,										 --Max value
 		COMBATC_HEALTH_LIMIT,							 --Slider Text
 		.01,									 --Slider Increment
 		1,										 --Slider state text on/off
 		"\%",									 --Slider state text append
 		100										 --Slider state text multiplier 
 		);
 	Cosmos_RegisterConfiguration(
 		"COS_COMCALLER_MANASLIMIT", 
 		"BOTH", 
 		COMBATC_MANA, 
 		COMBATC_MANA_INFO,
 												 --Description 
 		CombatCaller_OnManaConfigUpdate,
 		0, 
 		.2, 
 		0, 
 		1, 
 		COMBATC_MANA_LIMIT, 
 		.01, 
 		1, 
 		"\%",
 		100
 		);
 	Cosmos_RegisterConfiguration(
 		"COS_COMCALLER_COOLDOWN", 
 		"BOTH", 
 		COMBATC_COOL, 
 		COMBATC_COOL_INFO,
 												 --Description 
 		CombatCaller_OnCooldownUpdate,
 		0, 
 		30, 
 		0, 
 		60, 
 		COMBATC_COOL_LIMIT, 
 		1, 
 		1, 
 		COMBATC_COOL_SEC,
 		1	
 		);
 	end
 	-- Example update.
 	--Cosmos_Update("COS_COMBATCALLER_HEALTHSLIMIT", CSM_SLIDERVALUETEXTMULTIPLIER, 200 );
end

function CombatCaller_OnEvent(event) 
	if ( UnitIsDeadOrGhost("player") ) then
		this.IsGhost = 1;
		return;
	elseif ( this.IsGhost == 1 ) then
		this.hp = UnitHealth("player");
		this.lasthp = this.hp;
		this.mana = UnitMana("player");
		this.lastmana = this.mana;
		this.IsGhost = 0;
		this.InCombat = 0;
		return;
	end
	if (event == "PLAYER_ENTER_COMBAT") then
		this.InCombat = 1;
	elseif (event == "PLAYER_LEAVE_COMBAT") then
		this.InCombat = 0;
	end
	if( this.InCombat == 0 ) then
		return;
	end
	if ( event == "UNIT_HEALTH" ) then
		local ratio = UnitHealth("player")/UnitHealthMax("player");
		local oldratio = this.lasthp/UnitHealthMax("player");

		
		if ( (this.hp < this.lasthp) and 
		     (ratio < CombatCaller_HealthRatio) and 
		     (GetTime() - this.LastHPShout > CombatCaller_CooldownTime) and
		     (oldratio > CombatCaller_HealthRatio)) then
			CombatCaller_ShoutLowHealth();
			this.LastHPShout = GetTime();
		end
		
		this.lasthp = this.hp;
		this.hp = UnitHealth("player");
	end
	if ( event == "UNIT_MANA" ) then
		if(UnitPowerType("player") ~= 0) then
		  this.lastmana = 0;
		else 
		  local ratio = UnitMana("player")/UnitManaMax("player");
		  local oldratio = this.lastmana/UnitManaMax("player");

		  if ( (this.mana < this.lastmana) and 
		       (ratio < CombatCaller_ManaRatio) and 
		       (GetTime() - this.LastManaShout > CombatCaller_CooldownTime) and
		       (oldratio > CombatCaller_ManaRatio) ) then
			CombatCaller_ShoutLowMana();
			this.LastManaShout = GetTime();
		  end
	  
		  this.lastmana = this.mana;
		  this.mana = UnitMana("player");
		end
	end
end

function CombatCaller_OnHealthConfigUpdate(toggle, value) 
	if ( toggle == 0 ) then
		CombatCaller_HealthCall = false;
	else 
		CombatCaller_HealthCall = true;
		CombatCaller_HealthRatio = value;
	end
end

function CombatCaller_OnManaConfigUpdate(toggle, value) 
	if ( toggle == 0 ) then
		CombatCaller_ManaCall = false;
	else 
		CombatCaller_ManaCall = true;
		CombatCaller_ManaRatio = value;
	end
end
function CombatCaller_OnCooldownUpdate(toggle, value) 
	if ( toggle == 0 ) then
		CombatCaller_Cooldown = false;
		CombatCaller_CooldownTime = 0;
	else 
		CombatCaller_Cooldown = true; 
		CombatCaller_CooldownTime = value;
	end
end

function CombatCaller_ShoutLowHealth()
	if ( CombatCaller_HealthCall and this.InCombat == 1 ) then
		PlayVocalCategory("HEALME");
	end
end

function CombatCaller_ShoutLowMana()
	if ( CombatCaller_ManaCall and this.InCombat == 1 ) then
		PlayVocalCategory("OUTOFMANA");	
	end
end

function CombatCaller_TurnOff()
	CombatCaller_HealthCall = false;
	CombatCaller_ManaCall = false;
end

function CombatCaller_TurnOn()
	CombatCaller_HealthCall = true;
	CombatCaller_ManaCall = true;
end

	
