-- EzDismount : A quick and dirty dismounting mod, useful for PVP or herb/ore collecting
-- By Gaddur of the Eonar Server

-- Based on the mod "Acuzod's Dismount"
--

local EzDClass
local EzDPlayer

EzDismount_ver = "v1.5";
EzDismount_fullver = ("EzDismount " .. EzDismount_ver);

BINDING_HEADER_EZDISMOUNT = "EzDismount";
BINDING_NAME_EZDISMOUNT = "Dismount";


---------------------------------
-- Stuff to do when Mod is loaded
---------------------------------
function EzDismount_onload()
	
	EzDClass = UnitClass("player");
	EzDPlayer = (UnitName("player").." of "..GetCVar("realmName"))
	EZDismount_DetPlayer:SetText(EzDPlayer.." Server");
	
	--Create user table if it doesnt exist
	if (EzDismount_Config == nil) then
		EzDismount_Config = {};
	end
	
	if (EzDismount_Config[EzDPlayer] == nil) then
		EzDismount_Config[EzDPlayer] = {
			["Dismount"] = "ON";
			["Druid"] = "ON";
			["Wolf"] = "ON";
			["MoonKin"] = "ON";
		}
	end
	
  	DEFAULT_CHAT_FRAME:AddMessage("## " .. EzDismount_fullver .. " Loaded ##  Use /ezd or /ezdismount for menu.", 0.0, 1.0, 0.0);

  	SlashCmdList["EZDISMOUNT"] = EzDismount_options;
   	SLASH_EZDISMOUNT1 = "/ezd";
   	SLASH_EZDISMOUNT2 = "/ezdismount";
	  
	-- Set Default Colors
	EZDismount_ShamanTitle:SetTextColor(255,255,255,255);
	EZDismount_DruidTitle:SetTextColor(255,255,255,255);
	EzDismount_Text_Status_VOFF:SetTextColor(255,0,0,255);
	EzDismount_Text_Status_VON:SetTextColor(0,255,0,255);
	EzDismount_Text_Shaman_VOFF:SetTextColor(255,0,0,255);
	EzDismount_Text_Shaman_VON:SetTextColor(0,255,0,255);
	EzDismount_Text_Druid_VOFF:SetTextColor(255,0,0,255);
	EzDismount_Text_Druid_VON:SetTextColor(0,255,0,255);
	EzDismount_Text_Moonkin_VOFF:SetTextColor(255,0,0,255);
	EzDismount_Text_Moonkin_VON:SetTextColor(0,255,0,255);
	EZDismount_DetPlayer:SetTextColor(255,255,255,255);
	
end

----------------------------------
-- Parse out option from / Command
----------------------------------
function EzDismount_options(msg)
		
        -- Show Config Menu
	if (msg == "") or (string.lower(msg) == "help") then

		EzDismount_Toggle();
       
  	end	
	
        -- Dump Textures to chatwindow
	if (string.lower(msg) == "debug") then
		EzDismount_dumpbuff();
  	end
	
	 -- Reload UI
	if (string.lower(msg) == "reload") then
		ReloadUI();
  	end
end

----------------
-- Toggle Values
----------------
function EzDismount_ChgValue(msg)


	local NewVal = ""	
		
        -- Auto-dismount toggle
	if (string.lower(msg) == "on/off") then
		
		if (EzDismount_Config[EzDPlayer]["Dismount"] == "ON") then
			EzDismount_Config[EzDPlayer]["Dismount"] = "TAXI";
			NewVal = "Y";
		end
		
		if ((EzDismount_Config[EzDPlayer]["Dismount"] == "TAXI") and (NewVal == "")) then
			EzDismount_Config[EzDPlayer]["Dismount"] = "OFF";
			NewVal = "Y";
		end
		
		if ((EzDismount_Config[EzDPlayer]["Dismount"] == "OFF") and (NewVal == "")) then
			EzDismount_Config[EzDPlayer]["Dismount"] = "ON";
			NewVal = "Y";
		end
				       
  	end

	
        -- Druid toggle
	if (string.lower(msg) == "druid") then
		
		if (EzDismount_Config[EzDPlayer]["Druid"] == "ON") then
			EzDismount_Config[EzDPlayer]["Druid"] = "OFF";
		else
			EzDismount_Config[EzDPlayer]["Druid"] = "ON";
		end
		       
  	end
	
	-- Shaman toggle
	if (string.lower(msg) == "wolf") then
		
		if (EzDismount_Config[EzDPlayer]["Wolf"] == "ON") then
			EzDismount_Config[EzDPlayer]["Wolf"] = "OFF";
		else
			EzDismount_Config[EzDPlayer]["Wolf"] = "ON";
		end
		       
  	end

	-- Moonkin toggle
	if (string.lower(msg) == "moonkin") then
		
		if (EzDismount_Config[EzDPlayer]["Moonkin"] == "ON") then
			EzDismount_Config[EzDPlayer]["Moonkin"] = "OFF";
		else
			EzDismount_Config[EzDPlayer]["Moonkin"] = "ON";
		end
		       
  	end
	
	EzDismount_Refresh();
		
end


-------------------------------
-- Check UI_ERROR_MESSAGE Event
-------------------------------
function EzDismount_chkerror(arg1)

	-- See if auto dismount is enabled
	if (EzDismount_Config[EzDPlayer]["Dismount"] ~= "OFF") then
	
		-- If not on Taxi and trying to use a spell or attack (or Anything really), Dismount	
		if ( not UnitOnTaxi("player") ) then
			
			if ( EzDismount_Config[EzDPlayer]["Dismount"] == "ON" ) then
				if ( arg1 == SPELL_FAILED_NOT_MOUNTED ) then
					EzDismount_getdown();
				end
				if ( arg1 == "Can't attack while mounted.") then
					EzDismount_getdown();
				end
			end

			if ( arg1 == "TAXI") then
				EzDismount_getdown();
			end
		end
	end

	-- See druid shapeshift is enabled
	if ((EzDismount_Config[EzDPlayer]["Druid"] ~= "OFF") and (EzDClass == "Druid")) then
	
		-- If not on Taxi 
		if ( not UnitOnTaxi("player") ) then
			
			if ( arg1 == "Can't speak while shapeshifted.") then
				EzDismount_getdown();
			end
			if ( arg1 == "Cannot use in shapeshift form") then
				EzDismount_getdown();
			end
			if ( arg1 == "You can't do that while shapeshifted.") then
				EzDismount_getdown();
			end
			if ( arg1 == "You are in shapeshift form") then
				EzDismount_getdown();
			end
		end
	end
	
	-- See GhostWolf is enabled
	if ((EzDismount_Config[EzDPlayer]["Wolf"] ~= "OFF") and (EzDClass == "Shaman")) then
	
		-- If not on Taxi 
		if ( not UnitOnTaxi("player") ) then
			
			if ( arg1 == "Can't speak while shapeshifted.") then
				EzDismount_getdown();
			end
			if ( arg1 == "Cannot use in shapeshift form") then
				EzDismount_getdown();
			end
			if ( arg1 == "You can't do that while shapeshifted.") then
				EzDismount_getdown();
			end
			if ( arg1 == "You are in shapeshift form") then
				EzDismount_getdown();
			end
		end
	end

	-- See if Moonkin is enabled
	if ((EzDismount_Config[EzDPlayer]["Moonkin"] ~= "OFF") and (EzDClass == "Druid")) then
	
		-- If not on Taxi 
		if ( not UnitOnTaxi("player") ) then
			
			if ( arg1 == "Can't speak while shapeshifted.") then
				EzDismount_getdown();
			end
			if ( arg1 == "Cannot use in shapeshift form") then
				EzDismount_getdown();
			end
			if ( arg1 == "You can't do that while shapeshifted.") then
				EzDismount_getdown();
			end
			if ( arg1 == "You are in shapeshift form") then
				EzDismount_getdown();
			end
		end
	end
	
end


-----------------------------------------
-- Look for Mount Buff Icon and cancel it
-----------------------------------------
function EzDismount_getdown()

	local currBuffTex;
	local pallyhorse = "spell_nature_swiftness";
  	local regMount  = "_mount_"; 
  	local bearform = "ability_racial_bearform";
	local catform = "ability_druid_catform";
	local travelform = "ability_druid_travelform";
        local spiritwolf = "spell_nature_spiritwolf";
	local moonkin = "spell_nature_forceofnature";

	for i=0,15,1 do

		currBuffTex = GetPlayerBuffTexture(i);

		-- Mount (or level 40 pally horse)
		if ((EzDismount_Config[EzDPlayer]["Dismount"] == "ON") or (EzDismount_Config[EzDPlayer]["Dismount"] == "TAXI")) then
			if (currBuffTex and (string.find(string.lower(currBuffTex), regMount) or string.find(string.lower(currBuffTex), pallyhorse))) then
        			CancelPlayerBuff(i);
      			end
		end
		
		-- GhostWolf
		if (EzDismount_Config[EzDPlayer]["Wolf"] == "ON") then
			if (currBuffTex and (string.find(string.lower(currBuffTex), spiritwolf))) then
        			CancelPlayerBuff(i);
      			end
		end

		-- Bear, Cat or Travel Form
		if (EzDismount_Config[EzDPlayer]["Druid"] == "ON") then
			if (currBuffTex and (string.find(string.lower(currBuffTex), bearform) or string.find(string.lower(currBuffTex), catform) or string.find(string.lower(currBuffTex), travelform))) then
        			CancelPlayerBuff(i);
      			end
		end
				
		-- Moonkin
		if (EzDismount_Config[EzDPlayer]["Moonkin"] == "ON") then
			if (currBuffTex and (string.find(string.lower(currBuffTex), moonkin))) then
        			CancelPlayerBuff(i);
			end
      		end

	end

end

----------------------------------------------------
-- Dump current Buff icon texture names (Debug Code)
----------------------------------------------------
function EzDismount_dumpbuff()

	local bufftext;
  	local debugmsg

	for i=0,15,1 do

		bufftext = GetPlayerBuffTexture(i);

		if (bufftext ~=nil) then
			debugmsg = ("Texture (" .. i .. ") : " ..bufftext);
			DEFAULT_CHAT_FRAME:AddMessage(debugmsg, 0.0, 1.0, 0.0);
		end
		
	end
	
	DEFAULT_CHAT_FRAME:AddMessage("** Debug Done **", 0.0, 1.0, 0.0);

end

-----------------------------------------
-- Toggles the showing/hiding of the Menu
-----------------------------------------
function EzDismount_Toggle()

	if ( EzDismount_Menu:IsVisible() ) then
		EzDismount_Menu:Hide();
	else
		EzDismount_Menu:Show();
	end
	
end

--------------------
-- Refresh Screen
--------------------

function EzDismount_Refresh()
	
	EzDismount_Text_Status_VOFF:SetText("");
	EzDismount_Text_Status_VON:SetText("");
	EzDismount_Text_Shaman_VOFF:SetText("");
	EzDismount_Text_Shaman_VON:SetText("");
	EzDismount_Text_Druid_VOFF:SetText("");
	EzDismount_Text_Druid_VON:SetText("");
	EzDismount_Text_Moonkin_VOFF:SetText("");
	EzDismount_Text_Moonkin_VON:SetText("");
	
	-- Mounts
	EzDismount_Text_Status:SetText("Automatic dismounting is :");
	if ( EzDismount_Config[EzDPlayer]["Dismount"] == "OFF" ) then
		EzDismount_Text_Status_VOFF:SetText("[OFF]");
	end
	
	if (EzDismount_Config[EzDPlayer]["Dismount"] == "ON" ) then
		EzDismount_Text_Status_VON:SetText("[ON]");
	end
	
	if ( EzDismount_Config[EzDPlayer]["Dismount"] == "TAXI" ) then
		EzDismount_Text_Status_VON:SetText("[TAXI]");
	end

	-- Shaman
	EzDismount_Text_Shaman:SetText("Auto-cancel of Ghostwolf is :");
	if ( EzDismount_Config[EzDPlayer]["Wolf"] == "OFF" ) then
		EzDismount_Text_Shaman_VOFF:SetText("[OFF]");
	else
		EzDismount_Text_Shaman_VON:SetText("[ON]");
	end
	
	-- Druid
	EzDismount_Text_Druid:SetText("Auto-cancel of shapeshifts is :");
	if ( EzDismount_Config[EzDPlayer]["Druid"] == "OFF" ) then
		EzDismount_Text_Druid_VOFF:SetText("[OFF]");
	else
		EzDismount_Text_Druid_VON:SetText("[ON]");
	end

	-- Moonkin
	EzDismount_Text_Moonkin:SetText("Auto-cancel of Moonkin form :");
	if ( EzDismount_Config[EzDPlayer]["Moonkin"] == "OFF" ) then
		EzDismount_Text_Moonkin_VOFF:SetText("[OFF]");
	else
		EzDismount_Text_Moonkin_VON:SetText("[ON]");
	end

      
end
