

if true then return end -- stops loading the file

GHU_SendItemsID = {};
GHU_SendNotItems = {};
--GHI_RegEvent("CHAT_MSG_SAY",GHULink_Event);
--GHI_RegEvent("CHAT_MSG_YELL",GHULink_Event);
--GHI_RegEvent("CHAT_MSG_WHISPER",GHULink_Event);
--need to add others;

function GHULink_RegEvents()
local eventtypes = {"SAY","YELL","WHISPER","EMOTE","RAID","PARTY","GUILD","SYSTEM"}
for i=1,#eventtypes do 
GHI_RegEvent("CHAT_MSG_"..eventtypes[1],GHULink_Event);

end

GHU_OrigSetItemRef = SetItemRef;
	SetItemRef = GHU_SetItemRef;
	
	GHU_origSendChatMessage = SendChatMessage;
	SendChatMessage = GHU_SendChatMessage;
	
	-- inc chat
	GHU_origChatFrame_MessageEventHandler = ChatFrame_MessageEventHandler;
	ChatFrame_MessageEventHandler = GHU_ChatFrame_MessageEventHandler;

end

function GHULink_Event(...)--need to be sure i add args right
local self,event,mesg,sender,lang,chanString,target, flags,ukwn1, chanNum,chanName,ukwn2, counte,guid,arg13,arg14,arg15,arg16,arg17,arg18,arg19 = ...;
if GHI_MiscData["block_std_emote"] == true then
		if event == "CHAT_MSG_TEXT_EMOTE" then
			if mesg == msgToBlock and sender == playerToBlock then
				return;
			elseif blockNextStdEmoteFromPlayer[sender] == true then
				msgToBlock = mesg;
				playerToBlock = sender;
				blockNextStdEmoteFromPlayer[sender] = false;
				return;				
			end
		end
		
		if event == "CHAT_MSG_EMOTE" then
			if strsub(mesg,mesg:len()-2,mesg:len()) == "   " then
				blockNextStdEmoteFromPlayer[sender] = true;
			end
		end
		
	end
	
	if event == "CHAT_MSG_SYSTEM" then
		
		--GHI_Message(gsub(ERR_CHAT_PLAYER_NOT_FOUND_S,"%%s","%%a+"));
		--local player = string.match(arg1,gsub(ERR_CHAT_PLAYER_NOT_FOUND_S,"%%s","%%a"));
		local player = gsub(string.match(mesg,"'.*'") or "","'","");
		
		if player and format(ERR_CHAT_PLAYER_NOT_FOUND_S,player) == mesg and ((GHI_MessageErrorsExpected_A[player] or GHI_MessageErrorsExpected_B[player])) then
			
			return;
		end
	end
	
	msgToBlock = "";
	playerToBlock = "";
	
	if string.sub(event,1,8) == "CHAT_MSG" and not(event=="CHAT_MSG_SYSTEM") then 
	local linktext = GHU_Links.ParseLink(mesg)
	GHI:SendPrioritizedMessage("ALERT","WHISPER",sender,false,"SuspectedLinkName",linkText)
	
	end
	
end
local waitIndex = {};
function GHU_Links(parent,arg1,arg2) --unsure how many args i will need in the end
   local class = Class("GHU_Links",parent)

 
  -- local
  -- local myLocalArg1 = arg1; -- example of local value
 
  -- functions
  
  class.GenerateLink = function(self,arg1)
    local ItemID = arg1;
     if ItemID == nil then return end;
	local name,icon,quality = GHU_GetItemInfo(ItemID);
	if not(name) then return end;
	local link;
	local color = ITEM_QUALITY_COLORS[quality];
	link = "|CFF" ..string.format("%.2x",color.r*255) .. string.format("%.2x",color.g*255) .. string.format("%.2x",color.b*255) .."|HGHItem:"..ItemID..":0:0:0:0:0:0:0|h["..name.."]|h|r"
	
	return link,color,ItemID;
  end
 
  class.RemoveLink = function(self,text,arg2)
  	local a,b,c,d,e,f;
	a = 0;
		while a do
			--GHU_Message("A");
			a = GHI_SearchStringForChar(text,91,a+1);
			b = strfind(text,"]",a);
			if not(a == nil) and b then 
				c = strbyte(text,a-2);
				d = strbyte(text,b+1);
				if not(c == 124 or d == 124) then
					e = strsub(text,a+1,b-1);
					text = e;
					table.insert(GHU_SendNotItems,1,e);
					GHU_SendNotItems[10] = nil;
				end
			end
		end
		return text;
  end
  
  class.InsertLink = function(self,arg1,arg2) 
  local text = arg1;
  
  local a,b,c,d,e,ID,n1,n2 = 0;
	local link,name;

	off = 0;
	for i=1,5 do
		a = string.find(text,"|HGHItem",0);
		if a then
			--GHR_Message("#"..i);
			b = string.find(text,"|C",a-10);	
			c = string.find(text,"|r",a);	
			--GHR_Message(a);
			link = string.sub(text,b,c+1)
			n1 = string.find(link,"|h")
			n2 = string.find(link,"|h",n1+1);
			
			
			name = string.sub(link,n1+3,n2-2)
			
			-- find ID
			d = string.find(text,":",a);
			e = string.find(text,":",d+1);
			
			ID = string.sub(text,d+1,e-1);
			table.insert(GHU_SendItemsID,1,ID);
			GHU_SendItemsID[10] = nil;
			
			
			--text = gsub(text,link,"<"..name..">");
			text = strsub(text,0,b-1).."["..name.."]"..strsub(text,c+2);
		else
			break;	
		end
	end
	return link,name,text;
  end
  
  class.ParseLink = function(self,arg1,arg2)
			local text = arg1;
			local linkText,a,b,c,d = 0;
			
			--local waitIndex = {};
			a = 0;
			for i=1,5 do --GHI_Message("A -3");
				--GHI_Message("A");
				a = GHI_SearchStringForChar(text,91,a+1);
				b = strfind(text,"]",a);
				if not(a == nil) and b then --GHI_Message("a and b found in "..text);
					c = strbyte(text,a-2);
					d = strbyte(text,b+1);
					--GHI_Message(c);
					--GHR_Message(d);
					if not(c == 124 or d == 124) then
						linkText = strsub(text,a+1,b-1); 
						--c,d = strfind(text,linkText);
						
									if type(GHI_ItemData[value1]) == "table" then
										if GHI_ItemData[value1].name == linkText then
											sendToSelf = true;
											isItem = true;
											table.insert(waitIndex,value1);
											break;
										end
									end
								end
							end
							end
							
  end
  -- setup
  --  class:Start() -- example of something run as setup
 
  return class;
end



--	Catch item links in outgoing text
function GHU_SendChatMessage(text, type, language, target, ...)---trying move move this into class, call the class as an event?
	
	
	--- search for []   without links
	local a,b,c,d,e,f;
	a = 0;
		while a do
			--GHU_Message("A");
			a = GHU_SearchStringForChar(text,91,a+1);
			b = strfind(text,"]",a);
			if not(a == nil) and b then 
				c = strbyte(text,a-2);
				d = strbyte(text,b+1);
				if not(c == 124 or d == 124) then
					e = strsub(text,a+1,b-1);
					
					table.insert(GHU_SendNotItems,1,e);
					GHU_SendNotItems[10] = nil;
				end
			end
		end
	
	
	--GHU_Message("Handle Outgoing");

	local a,b,c,d,e,ID,n1,n2 = 0;
	local link,name;

	off = 0;
	for i=1,5 do
		a = string.find(text,"|HGHUtem",0);
		if a then
			--GHR_Message("#"..i);
			b = string.find(text,"|C",a-10);	
			c = string.find(text,"|r",a);	
			--GHR_Message(a);
			link = string.sub(text,b,c+1)
			n1 = string.find(link,"|h")
			n2 = string.find(link,"|h",n1+1);
			
			
			name = string.sub(link,n1+3,n2-2)
			
			-- find ID
			d = string.find(text,":",a);
			e = string.find(text,":",d+1);
			
			ID = string.sub(text,d+1,e-1);
			table.insert(GHU_SendItemsID,1,ID);
			GHU_SendItemsID[10] = nil;
			
			
			--text = gsub(text,link,"<"..name..">");
			text = strsub(text,0,b-1).."["..name.."]"..strsub(text,c+2);
		else
			break;	
		end
	end

	
	GHU_origSendChatMessage(text, type, language, target, ...)
end


function GHU_Links:SetItemRef(link, text, button)

	

	local prefix = string.sub(link,0,6)

	if prefix == "GHUtem" then
		local a = strfind(link,":",0);
		if not(a) then return end;
		local b = strfind(link,":",a+1);
		if not(b) then return end;
			
		local ID = strsub(link,a+1,b-1);
		if IsShiftKeyDown() == 1 then
			local GHU_link = GHU_GenerateLink(ID);
			if ( not(ChatEdit_InsertLink(GHU_link)) and not(GHU_FCCC_InsertLink(GHU_link)) ) then
			end
		else
			if ItemRefTooltip:IsShown() and GHU_LastLinkID == ID then
				ItemRefTooltip:Hide();
			else
				GHU_ItemRefTooltip(ID);
			end
		end
		
		GHU_LastLinkID = ID;
	else
		GHU_OrigSetItemRef(link, text, button);
		GHU_LastLinkID = 0;
	end
end

function GHU_ItemRefTooltip(ID) 
	ShowUIPanel(ItemRefTooltip);
	if ( not ItemRefTooltip:IsShown() ) then
		ItemRefTooltip:SetOwner(UIParent, "ANCHOR_PRESERVE");
	end
	GHU_SetTooltipInfo(ItemRefTooltip,ID);
	ItemRefTooltip:Show();
	

end