Sonic_GuildTextColor = "|c0000FF00";    --工会文字颜色 绿色
Sonic_GuildColor = "|c00FFFFFF";        --工会名称颜色 白色
Sonic_TargetTextColor = "|c0000FF00";   --目标文字颜色 绿色
Sonic_TargetColor = "|c00FFFFFF";       --目标名称颜色 白色
Sonic_TargetClassColor = "|c00FF9B00";  --目标职业颜色 橙色

function Sonic_GameTooltip_OnLoad()
   	this:RegisterEvent("VARIABLES_LOADED");
	GameTooltip.existingSetUnit = GameTooltip.SetUnit;
	function GameTooltip:SetUnit(unit)
		local result, reason = pcall(GameTooltip.existingSetUnit, GameTooltip, unit);
		if(not result) then
			error(reason, 2);
		end
		Sonic_AddGuildInfo(unit);
		Sonic_AddMouseoverTargetInfo(unit);
		GameTooltip:Show();
	end
	this:RegisterEvent("UPDATE_MOUSEOVER_UNIT");
end

function Sonic_GameTooltip_OnEvent()
	if (event == "UPDATE_MOUSEOVER_UNIT") then
		if (UnitExists("mouseover")) then
			Sonic_AddGuildInfo("mouseover");
			Sonic_AddMouseoverTargetInfo("mouseover");
			GameTooltip:Show();
		end
	elseif (event == "VARIABLES_LOADED") then
	  	Sonic_OriginalSetDefaultAnchor = GameTooltip_SetDefaultAnchor;
	    GameTooltip_SetDefaultAnchor = Sonic_GameTooltip_SetDefaultAnchor;
	end
end

function Sonic_GameTooltip_SetDefaultAnchor(tooltip, parent)
	Sonic_OriginalSetDefaultAnchor(tooltip, parent);
	GameTooltip:ClearAllPoints();
	GameTooltip:SetOwner(UIParent, "ANCHOR_CURSOR");
	GameTooltip.default = nil;

	local scale = UIParent:GetEffectiveScale();
	local x, y = GetCursorPosition();
	local m = UIParent:GetWidth() - 150; 
	local t = 150;
	if(scale and scale ~= 0) then 
			x = x / scale 
			y = y / scale 
	end;
	if(x > m and y <t) then
		GameTooltip:ClearAllPoints();
		GameTooltip:SetOwner(UIParent, "ANCHOR_CURSOR");
		GameTooltip:SetPoint("BOTTOMRIGHT", "UIParent", "BOTTOMRIGHT", -10, 30); 
	elseif(x > m) then
		GameTooltip:ClearAllPoints();
		GameTooltip:SetOwner(UIParent, "ANCHOR_CURSOR");
		GameTooltip:SetPoint("BOTTOMRIGHT", "UIParent", "BOTTOMRIGHT", 0, y-30); 
	elseif(y > t) then
		GameTooltip:ClearAllPoints();
		GameTooltip:SetOwner(UIParent, "ANCHOR_CURSOR");
		GameTooltip:SetPoint("BOTTOMLEFT", "UIParent", "BOTTOMLEFT", x+10, y-30); 
	else 
		GameTooltip:ClearAllPoints();
		GameTooltip:SetOwner(UIParent, "ANCHOR_CURSOR");
		GameTooltip:SetPoint("BOTTOMLEFT", "UIParent", "BOTTOMLEFT", x+20, y-20); 
	end;
end



function Sonic_AddGuildInfo(unit)
	local guild = GetGuildInfo(unit);
	if guild then
		GameTooltip:AddLine(Sonic_GuildTextColor.."工会: "..Sonic_GuildColor..guild.."|r");
	end;
end

function Sonic_AddMouseoverTargetInfo(unit)
	if UnitIsVisible(unit.."target") then
		if UnitIsPlayer(unit.."target") then
		    GameTooltip:AddLine(Sonic_TargetTextColor.."目标: "..Sonic_TargetClassColor..UnitClass(unit.."target").." "..Sonic_TargetColor..UnitName(unit.."target").."|r");
		  else
		    GameTooltip:AddLine(Sonic_TargetTextColor.."目标: "..Sonic_TargetColor..UnitName(unit.."target").."|r");
		end;
	end;
end
