-- http://gaming.thoughtbludgeon.com
PetLeash_VERSION = "v1.01";

-- Config Options
PetLeash_Config = {};

-- General Variables
PetLeash_Vars = {};
PetLeash_Vars.MainAction = "";
PetLeash_Vars.InCombat = false;

-- Slash Commands
PetLeash_ENABLE			= "enable";
PetLeash_DISABLE		= "disable";
PetLeash_ON			= "on";
PetLeash_OFF			= "off";
PetLeash_CHANNEL		= "channel";
PetLeash_NOCHASE		= "nochase";


-- Misc
PetLeash_FOCUSREGEN = 5;
COLOR_RED = "|cffff0000";
COLOR_YELLOW = "|cffffff00";
COLOR_GREEN = "|cff00ff00";
COLOR_GREY = "|caaaaaaaa";
COLOR_WHITE = "|cffffffff";
COLOR_BLUE = "|cff3366ff";
COLOR_CLOSE = "|r";

-- Load Handler
function PetLeash_OnLoad()
	this:RegisterEvent("PLAYER_ENTER_COMBAT");
	this:RegisterEvent("PLAYER_LEAVE_COMBAT");
	this:RegisterEvent("PLAYER_TARGET_CHANGED");
	this:RegisterEvent("START_AUTOREPEAT_SPELL");
	this:RegisterEvent("STOP_AUTOREPEAT_SPELL");
	this:RegisterEvent("PLAYER_CONTROL_LOST");
	this:RegisterEvent("UI_ERROR_MESSAGE");
	this:RegisterEvent("SPELLCAST_START");
	this:RegisterEvent("SPELLCAST_STOP");
	this:RegisterEvent("PLAYER_REGEN_DISABLED");
	this:RegisterEvent("PLAYER_REGEN_ENABLED");
	this:RegisterEvent("CHAT_MSG_MONSTER_EMOTE");
	this:RegisterEvent("PET_ATTACK_START");
	this:RegisterEvent("PET_ATTACK_STOP");
	this:RegisterEvent("VARIABLES_LOADED");
	PetLeash_ResetConfig();

	SLASH_PetLeash1 = "/PetLeash";
	SLASH_PetLeash2 = "/pl";
	SlashCmdList["PetLeash"] = function(msg)
		PetLeash_OnSlashCommand(msg);
	end

	if ( DEFAULT_CHAT_FRAME ) then 
		DEFAULT_CHAT_FRAME:AddMessage("PetLeash "..PetLeash_VERSION.." Loaded: Type /PetLeash or /pl for more information");
	end	
end

-- Slash Command Handler
function PetLeash_OnSlashCommand(msg)

	if (not msg) then
		return;
	end

	local command = string.lower(msg);
	
	-- Enable/Disable
	if (command == PetLeash_ENABLE or command == PetLeash_ON) then
		PetLeash_Config.Enabled = true;
		PetLeash_AddInfoMessage("PetLeash enabled");
	elseif (command == PetLeash_DISABLE or command == PetLeash_OFF  ) then
		PetLeash_Config.Enabled = false;
		PetLeash_AddInfoMessage("PetLeash disabled");
	-- No Chase
	elseif (strsub(command, 1, strlen(PetLeash_NOCHASE)) == PetLeash_NOCHASE ) then
		if (PetLeash_Config.NoChase) then
			PetLeash_Config.NoChase = false;
			PetLeash_AddInfoMessage("PetLeash: Your pet will chase fleeing targets");
		else
			PetLeash_Config.NoChase = true;
			PetLeash_AddInfoMessage("PetLeash: Your pet will stop attacking if your target flees");
		end		
	-- Typos and Default
	else
		DEFAULT_CHAT_FRAME:AddMessage(" ");
		PetLeash_AddInfoMessage("PetLeash Status:");
		DEFAULT_CHAT_FRAME:AddMessage(COLOR_GREEN.."Use "..COLOR_CLOSE..COLOR_WHITE.."/PetLeash <command> "..COLOR_CLOSE..COLOR_GREEN.."or "..COLOR_CLOSE..COLOR_WHITE.."/pl <command> "..COLOR_CLOSE..COLOR_GREEN.."to perform the following commands:"..COLOR_CLOSE);
		PetLeash_AddHelpMessage(PetLeash_ENABLE.." | "..PetLeash_DISABLE, "enables or disables PetLeash.  ", nil, PetLeash_Config.Enabled);
		PetLeash_AddHelpMessage(PetLeash_NOCHASE, "pet will stop combat if your target flees or is feared.  ", nil, PetLeash_Config.NoChase);
    	end
end

-- Main Event Handler
function PetLeash_OnEvent()
	if (event == "VARIABLES_LOADED") then
		if (not PetLeash_Config.Version or PetLeash_Config.Version ~= PetLeash_VERSION) then
			PetLeash_AddInfoMessage("PetLeash: Version changed, resetting all variables to default.");
			PetLeash_ResetConfig();
		end
		RegisterForSave("PetLeash_Config");
				
	end

	if (not PetLeash_Config.Enabled) then
		return;
	end

	if (not UnitExists("pet") or UnitIsDead("pet")) then
		PetLeash_Vars.inCombat = false;
		return;
	end

	if ( event == "PET_ATTACK_START") then
		PetLeash_Vars.inCombat = true;
	end

	if (event == "PET_ATTACK_STOP") then
		PetLeash_Vars.inCombat = false;
	end

	if (event == "PLAYER_REGEN_ENABLED") then
		PetLeash_Vars.inCombat = false;
	end

	if (event == "CHAT_MSG_MONSTER_EMOTE") then
		PetLeash_OnMonsterEmote(arg1, arg2);
	end

	if ( event == "PLAYER_REGEN_DISABLED") then
		PetLeash_CheckFear();
	end

	if ( event == "CHAT_MSG_SPELL_SELF_DAMAGE" or event == "CHAT_MSG_COMBAT_SELF_MISSES" or event == "CHAT_MSG_COMBAT_SELF_HITS") then
		PetLeash_CheckFear();
	elseif( event == "PLAYER_ENTER_COMBAT") then
		PetLeash_CheckFear();
	elseif( event == "PLAYER_LEAVE_COMBAT" ) then
		PetLeash_CheckFear();
	elseif( event == "PLAYER_REGEN_ENABLED") then
		PetLeash_CheckFear();
	elseif( event == "PLAYER_REGEN_DISABLED") then
		PetLeash_CheckFear();
	elseif( event == "SPELLCAST_START") then
		PetLeash_CheckFear();
	elseif( event == "SPELLCAST_STOP") then
		PetLeash_CheckFear();
	elseif( event == "PLAYER_TARGET_CHANGED") then
		PetLeash_CheckFear();
	elseif( event == "START_AUTOREPEAT_SPELL") then
		PetLeash_CheckFear();
	elseif( event == "STOP_AUTOREPEAT_SPELL") then
		PetLeash_CheckFear();
	elseif( event == "PLAYER_CONTROL_LOST") then
		PetLeash_CheckFear();
	elseif( event == "PLAYER_CONTRL_GAINED") then
		PetLeash_CheckFear();
	elseif( event == "UI_ERROR_MESSAGE") then
		PetLeash_CheckFear();
	end

end

function PetLeash_OnMonsterEmote(arg1)
	if (not PetLeash_Config.NoChase) then
		return
	end
	
	if (arg2 == UnitName("target") and strfind (arg1, "attempts to run away in fear")) then
		--PetLeash_AddInfoMessage(UnitName("pet").." breaking off from combat!");
		PetFollow();
	end
end

function PetLeash_CheckFear()
	for i = 1, 10 do
		if (UnitDebuff("target",i)) then
			currentdebuff=UnitDebuff("target",i)
			if (UnitDebuff("target",i) == "Interface\\Icons\\Spell_Shadow_Possession") then
				--PetLeash_AddInfoMessage(UnitName("target").." is feared!  "..UnitName("pet").." breaking off from combat!");
				PetFollow();
				break;
			end
			if (UnitDebuff("target",i) == "Interface\\Icons\\Spell_Shadow_Charm") then
				--PetLeash_AddInfoMessage(UnitName("target").." is charmed!  "..UnitName("pet").." breaking off from combat!");
				PetFollow();
				break;
			end
			if (UnitDebuff("target",i) == "Interface\\Icons\\Spell_Nature_Polymorph") then
				--PetLeash_AddInfoMessage(UnitName("target").." is sheeped!  "..UnitName("pet").." breaking off from combat!");
				PetFollow();
				break;
			end
		elseif (not UnitDebuff("target",i)) then
			break;
			end
		end
end

-- 
-- General Functions
--

-- Decide to toggle a variable based on its current state, or the command string
function PetLeash_ToggleOption(command, enabled)
	if (strfind(command, "on") or strfind(command, "enable")) then
		PetLeash_Config.Enabled = false;
		PetLeash_AddInfoMessage("PetLeash disabled");
		return true;
	elseif (strfind(command, "off") or strfind(command, "disable")) then
		PetLeash_Config.Enabled = true;
		PetLeash_AddInfoMessage("PetLeash enabled");
		return false;
	else
		if (enabled) then
			return false;
		else
			return true;
		end
	end
end

function PetLeash_BuildTooltip(id)
	if (id == 2) then	
		GameTooltip:SetOwner(this, "ANCHOR_RIGHT");
		GameTooltip:SetText(COLOR_WHITE.."PetLeash Toggle"..COLOR_CLOSE);
		GameTooltip:Show();
	end
end

function PetLeash_turnon()
		PetLeash_Config.Enabled = true;
		PetLeash_AddInfoMessage("PetLeash enabled");
end
function PetLeash_turnoff()
		PetLeash_Config.Enabled = false;
		PetLeash_AddInfoMessage("PetLeash disabled");
end

-- Adds a formatted informational message
function PetLeash_AddInfoMessage(message)
	DEFAULT_CHAT_FRAME:AddMessage(COLOR_YELLOW..message..COLOR_CLOSE);
end

-- Adds a formatted help message
function PetLeash_AddHelpMessage(command, detail, status, enabled)
	message = COLOR_WHITE..command..": "..COLOR_CLOSE..COLOR_GREEN..detail..COLOR_CLOSE;

	if (enabled == nil) then
		DEFAULT_CHAT_FRAME:AddMessage(message);
		return;
	end
	
	if (status == "" or status == nil) then
		if (enabled) then
			DEFAULT_CHAT_FRAME:AddMessage(message..COLOR_WHITE.." (enabled)"..COLOR_CLOSE);
		else
			DEFAULT_CHAT_FRAME:AddMessage(message..COLOR_GREY.." (disabled)"..COLOR_CLOSE);
		end
	else
		if (enabled) then
			DEFAULT_CHAT_FRAME:AddMessage(message..COLOR_WHITE..status..COLOR_CLOSE);
		else
			DEFAULT_CHAT_FRAME:AddMessage(message..COLOR_GREY..status..COLOR_CLOSE);
		end		
	end
end

function PetLeash_ResetConfig()
	PetLeash_Config.Enabled = true;
	PetLeash_Config.Channel = "say";
	PetLeash_Config.NoChase = true;
	PetLeash_Config.Version = PetLeash_VERSION;
end
