--[[
	WhoLink by CGray

	Who Link allows you to add the feature of getting an instant 
	/who commands on a user by clicking on their name.

  ]]--
local WhoLink_original_ChatFrame_OnEvent = ChatFrame_OnEvent;
local WhoLink_original_CHAT_SAY_GET;
local WhoLink_original_CHAT_PARTY_GET;
local WhoLink_original_CHAT_RAID_GET;
local WhoLink_original_CHAT_RAID_LEADER_GET;
local WhoLink_original_CHAT_WHISPER_GET;
local WhoLink_original_CHAT_WHISPER_INFORM_GET;
local WhoLink_original_CHAT_BATTLEGROUND_GET;
local WhoLink_original_CHAT_BATTLEGROUND_LEADER_GET;
local WhoLink_original_CHAT_CHANNEL_GET;
local WhoLink_original_CHAT_GUILD_GET;
local WhoLink_original_CHAT_OFFICER_GET;
local WhoLink_original_CHAT_YELL_GET;

local WhoLink_SetupDone = false;

function WhoLink_OnLoad() 
	for i=1, NUM_CHAT_WINDOWS do
		local frame = getglobal("ChatFrame"..i);
		CreateFrame("frame", frame:GetName().."Gradient", frame);
	end

	this:RegisterEvent("VARIABLES_LOADED");
	this:RegisterEvent("PLAYER_ENTERING_WORLD");

	if(Cosmos_RegisterConfiguration) then
		Cosmos_RegisterConfiguration(
			"COS_WHOLINK",
			"SECTION",
			WHOLINK_SEP,
			WHOLINK_SEP_INFO
			);

		Cosmos_RegisterConfiguration(
			"COS_WHOLINK_SEP",
			"SEPARATOR",
			WHOLINK_SEP,
			WHOLINK_SEP_INFO
			);

		Cosmos_RegisterConfiguration(
			"COS_WHOLINK_COLOR",
			"CHECKBOX",
			WHOLINK_RECOLOR,
			WHOLINK_RECOLOR_INFO,
			WhoLink_ToggleRecolor,
			0
			);

		Cosmos_RegisterConfiguration(
			"COS_WHOLINK_GRADIENT",
			"CHECKBOX",
			WHOLINK_GRADIENT,
			WHOLINK_GRADIENT_INFO,
			WhoLink_ToggleGradient,
			0
			);

	--[[	Cosmos_RegisterConfiguration(
			"COS_WHOLINK_BRACE",
			"CHECKBOX",
			WHOLINK_NONBRACES,
			WHOLINK_NONBRACES_INFO,
			WhoLink_ToggleNonBraces,
			0
			); ]]
		
		Cosmos_RegisterConfiguration(
			"COS_WHOLINK_ALTINVITE",
			"CHECKBOX",
			WHOLINK_PARTY,
			WHOLINK_PARTY_INFO,
			WhoLink_ToggleAltPartyInvite,
			1
			);
		Cosmos_RegisterConfiguration(
			"COS_WHOLINK_CHANNELSTICKY",
			"CHECKBOX",
			WHOLINK_CHANNELSTICKY,
			WHOLINK_CHANNELSTICKY_INFO,
			WhoLink_ToggleChannelSticky,
			1
			);
		Cosmos_RegisterConfiguration(
			"COS_WHOLINK_RAIDWARNSTICKY",
			"CHECKBOX",
			WHOLINK_RAIDWARNSTICKY,
			WHOLINK_RAIDWARNSTICKY_INFO,
			WhoLink_ToggleRaidWarnSticky,
			0
			);
	else
		-- ADD STANDALONE CONFIG HERE
	end

	hooksecurefunc("FCF_StopResize", function()
		if ( WhoLink_GRADIENT == 1 ) then
			local cframe = getglobal(this:GetParent():GetName().."Gradient");
			cframe.grad:SetPoint("BOTTOMRIGHT", cframe, "TOPRIGHT", -4, -math.ceil(cframe:GetHeight())+5);
		end
	end);
end

function WhoLink_OnEvent()
	if ( event == "VARIABLES_LOADED" ) then
		WhoLink_CHANNELSTICKY = Cosmos_GetCVar("COS_WHOLINK_CHANNELSTICKY_X");
		WhoLink_RAIDWARNSTICKY = Cosmos_GetCVar("COS_WHOLINK_RAIDWARNSTICKY_X");
		if ( WhoLink_CHANNELSTICKY == 1 ) then
			ChatTypeInfo["CHANNEL"].sticky = 1;
		else
			ChatTypeInfo["CHANNEL"].sticky = 0;
		end
		if ( WhoLink_RAIDWARNSTICKY == 1 ) then
			ChatTypeInfo["RAID_WARNING"].sticky = 1;
		else
			ChatTypeInfo["RAID_WARNING"].sticky = 0;
		end
	elseif (event == "PLAYER_ENTERING_WORLD") then
		WhoLink_SetupChatFrames();
	end
end

WhoLink_ENABLE = 1;
WhoLink_RECOLOR = nil;
WhoLink_NONBRACES = nil;
WhoLink_CHANNELSTICKY = 1;
WhoLink_RAIDWARNSTICKY = 0;
WhoLink_GRADIENT = 0;
WhoLink_PARTY = nil;
WhoLink_TARGET = nil;

function WhoLink_Toggle(toggle)
	if(toggle == 1) then
		WhoLink_ENABLE = 1;
	else
		WhoLink_ENABLE = nil;
	end
end

function WhoLink_ToggleRecolor(toggle)
	if(toggle == 1) then
		WhoLink_RECOLOR = 1;
	else
		WhoLink_RECOLOR = nil;
	end
end

function WhoLink_ToggleNonBraces(toggle)
	if(toggle == 1) then
		WhoLink_NONBRACES = 1;
	else
		WhoLink_NONBRACES = nil;
	end
end

function WhoLink_ToggleAltPartyInvite(toggle)
	if(toggle == 1) then
		WhoLink_PARTY = 1;
	else
		WhoLink_PARTY = nil;
	end
end

function WhoLink_ToggleChannelSticky(toggle)
	if(toggle == 1) then
		WhoLink_CHANNELSTICKY = 1;
	else
		WhoLink_CHANNELSTICKY = nil;
	end
end

function WhoLink_ToggleRaidWarnSticky(toggle)
	if(toggle == 1) then
		WhoLink_RAIDWARNSTICKY = 1;
	else
		WhoLink_RAIDWARNSTICKY = nil;
	end
end

function WhoLink_ToggleGradient(toggle)
	if(toggle == 1) then
		WhoLink_GRADIENT = 1;
		for i=1, NUM_CHAT_WINDOWS do
			local frame = getglobal("ChatFrame"..i.."Gradient");
			frame:Show();
		end
	else
		WhoLink_GRADIENT = nil;
		for i=1, NUM_CHAT_WINDOWS do
			local frame = getglobal("ChatFrame"..i.."Gradient");
			frame:Hide();
		end
	end
end

function ChatFrame_OnEvent(event)
	local cstring;

	if ( WhoLink_RECOLOR == 1 ) then
		cstring = "|c2288ffaa%%s|r";
	end
	if ( cstring ) then
		if( event == "CHAT_MSG_CHANNEL" and arg1 ) then
			WhoLink_original_CHAT_CHANNEL_GET = CHAT_CHANNEL_GET;
			CHAT_CHANNEL_GET = gsub(CHAT_CHANNEL_GET, "%%s", cstring);
		end
		if( event == "CHAT_MSG_SAY" and arg1 ) then
			WhoLink_original_CHAT_SAY_GET = CHAT_SAY_GET;
			CHAT_SAY_GET = gsub(CHAT_SAY_GET, "%%s", cstring);
		end
		if( event == "CHAT_MSG_PARTY" and arg1 ) then
			WhoLink_original_CHAT_PARTY_GET = CHAT_PARTY_GET;
			CHAT_PARTY_GET = gsub(CHAT_PARTY_GET, "%%s", cstring);
		end
		if( event == "CHAT_MSG_RAID" and arg1 ) then
			WhoLink_original_CHAT_RAID_GET = CHAT_RAID_GET;
			CHAT_RAID_GET = gsub(CHAT_RAID_GET, "%%s", cstring);
		end
		if( event == "CHAT_MSG_RAID_LEADER" and arg1 ) then
			WhoLink_original_CHAT_RAID_LEADER_GET = CHAT_RAID_LEADER_GET;
			CHAT_RAID_LEADER_GET = gsub(CHAT_RAID_LEADER_GET, "%%s", cstring);
		end
		if( event == "CHAT_MSG_WHISPER" and arg1 ) then
			WhoLink_original_CHAT_WHISPER_GET = CHAT_WHISPER_GET;
			CHAT_WHISPER_GET = gsub(CHAT_WHISPER_GET, "%%s", cstring);
		end
		if( event == "CHAT_MSG_WHISPER_INFORM" and arg1 ) then
			WhoLink_original_CHAT_WHISPER_INFORM_GET = CHAT_WHISPER_INFORM_GET;
			CHAT_WHISPER_INFORM_GET = gsub(CHAT_WHISPER_INFORM_GET, "%%s", cstring);
		end
		if( event == "CHAT_MSG_BATTLEGROUND" and arg1 ) then
			WhoLink_original_CHAT_BATTLEGROUND_GET = CHAT_BATTLEGROUND_GET;
			CHAT_BATTLEGROUND_GET = gsub(CHAT_BATTLEGROUND_GET, "%%s", cstring);
		end
		if( event == "CHAT_MSG_BATTLEGROUND_LEADER" and arg1 ) then
			WhoLink_original_CHAT_BATTLEGROUND_LEADER_GET = CHAT_BATTLEGROUND_LEADER_GET;
			CHAT_BATTLEGROUND_LEADER_GET = gsub(CHAT_BATTLEGROUND_LEADER_GET, "%%s", cstring);
		end
		if( event == "CHAT_MSG_GUILD" and arg1 ) then
			WhoLink_original_CHAT_GUILD_GET = CHAT_GUILD_GET;
			CHAT_GUILD_GET = gsub(CHAT_GUILD_GET, "%%s", cstring);
		end
		if( event == "CHAT_MSG_OFFICER" and arg1 ) then
			WhoLink_original_CHAT_OFFICER_GET = CHAT_OFFICER_GET;
			CHAT_OFFICER_GET = gsub(CHAT_OFFICER_GET, "%%s", cstring);
		end
		if( event == "CHAT_MSG_YELL" and arg1 ) then
			WhoLink_original_CHAT_YELL_GET = CHAT_YELL_GET;
			CHAT_YELL_GET = gsub(CHAT_YELL_GET, "%%s", cstring);
		end
	end
	WhoLink_original_ChatFrame_OnEvent(event);
	if ( cstring ) then
		if( event == "CHAT_MSG_CHANNEL" and arg1 ) then
			CHAT_CHANNEL_GET = WhoLink_original_CHAT_CHANNEL_GET;
		end
		if( event == "CHAT_MSG_SAY" and arg1 ) then
			CHAT_SAY_GET = WhoLink_original_CHAT_SAY_GET;
		end
		if( event == "CHAT_MSG_PARTY" and arg1 ) then
			CHAT_PARTY_GET = WhoLink_original_CHAT_PARTY_GET;
		end
		if( event == "CHAT_MSG_RAID" and arg1 ) then
			CHAT_RAID_GET = WhoLink_original_CHAT_RAID_GET;
		end
		if( event == "CHAT_MSG_RAID_LEADER" and arg1 ) then
			CHAT_RAID_LEADER_GET = WhoLink_original_CHAT_RAID_LEADER_GET;
		end
		if( event == "CHAT_MSG_WHISPER" and arg1 ) then
			CHAT_WHISPER_GET = WhoLink_original_CHAT_WHISPER_GET;
		end
		if( event == "CHAT_MSG_WHISPER_INFORM" and arg1 ) then
			CHAT_WHISPER_INFORM_GET = WhoLink_original_CHAT_WHISPER_INFORM_GET;
		end
		if( event == "CHAT_MSG_BATTLEGROUND" and arg1 ) then
			CHAT_BATTLEGROUND_GET = WhoLink_original_CHAT_BATTLEGROUND_GET;
		end
		if( event == "CHAT_MSG_BATTLEGROUND_LEADER" and arg1 ) then
			CHAT_BATTLEGROUND_LEADER_GET = WhoLink_original_CHAT_BATTLEGROUND_LEADER_GET;
		end
		if( event == "CHAT_MSG_GUILD" and arg1 ) then
			CHAT_GUILD_GET = WhoLink_original_CHAT_GUILD_GET;
		end
		if( event == "CHAT_MSG_OFFICER" and arg1 ) then
			CHAT_OFFICER_GET = WhoLink_original_CHAT_OFFICER_GET;
		end
		if( event == "CHAT_MSG_YELL" and arg1 ) then
			CHAT_YELL_GET = WhoLink_original_CHAT_YELL_GET;
		end
	end
end

function WhoLink_ProcessLink(wholink, button)
	local index, length, name = string.find(wholink, "^player:([^:]+)");
	if (index) then
		WhoLink_Show(name, button);
	end
	return index;
end

function WhoLink_Show(whoName, button)
	if ( not chatFrame ) then
		chatFrame = DEFAULT_CHAT_FRAME;
	end
	if(WhoLink_ENABLE) then
		if ( IsAltKeyDown() and WhoLink_PARTY ) then 
			InviteUnit(whoName);
			return;
		elseif ( IsShiftKeyDown() ) then
			local staticPopup = StaticPopup_Visible("ADD_IGNORE");
			if ( staticPopup ) then
				-- If add ignore dialog is up then enter the name into the editbox
				getglobal(staticPopup.."EditBox"):SetText(whoName);
			else
				SendWho("n-"..whoName);
			end
			return;
		elseif ( button == "RightButton" ) then
			FriendsFrame_ShowDropdown(whoName, 1);
			return;
		end
	
		if ( not chatFrame.editBox:IsVisible() ) then
			ChatFrame_SendTell(whoName);
		else
			chatFrame.editBox:SetText("/w "..whoName.." ");
		end
		ChatEdit_ParseText(chatFrame.editBox, 0);
	end
end

function WhoLink_ProcessText(arg1,arg2,arg3,pflag,type)
	local wcolor = "";
	local wcolorend = "";
	local lbrace = "[";
	local rbrace = "]";
	local wholinktag = "";
	local wholinktagend = "";
	local showLink = 1;

	if(WhoLink_ENABLE) then
		wholinktag = "|Hplayer:"..arg2;
		wholinktagend = "|h";
		if ( WhoLink_RECOLOR ) then
			wcolor = "|c2288ffaa";
			wcolorend = "|r";
		end
		if ( WhoLink_NONBRACES == 1 ) then
			lbrace = "";
			rbrace = "";
		end
	end

	if ( strsub(type, 1, 7) == "MONSTER" ) then
		showLink = nil;
		wcolor = "";
		wcolorend = "";
		lbrace = "";
		rbrace = "";
	end

	if ( (strlen(arg3) > 0) and (arg3 ~= "Universal") and (arg3 ~= this.defaultLanguage) ) then
		local languageHeader = "["..arg3.."] ";
		if ( showLink and (strlen(arg2) > 0) ) then
			body = format(TEXT(getglobal("CHAT_"..type.."_GET"))..languageHeader..arg1, pflag..wcolor..wholinktag..wholinktagend..lbrace..arg2..rbrace..wholinktagend..wcolorend);
		else
			body = format(TEXT(getglobal("CHAT_"..type.."_GET"))..languageHeader..arg1, pflag..wcolor..arg2..wcolorend);
		end
	else
		if ( showLink and (strlen(arg2) > 0) and (type ~= "EMOTE") ) then
			body = format(TEXT(getglobal("CHAT_"..type.."_GET"))..arg1, pflag..wcolor..wholinktag..wholinktagend..lbrace..arg2..rbrace..wholinktagend..wcolorend);
		else
			body = format(TEXT(getglobal("CHAT_"..type.."_GET"))..arg1, pflag..arg2);
		end
	end
	return body;
end

function WhoLink_SetupChatFrames()
	if ( WhoLink_SetupDone ) then
		return;
	end
	for i=1, NUM_CHAT_WINDOWS do
		local frame = getglobal("ChatFrame"..i);
		--local cframe = CreateFrame("frame", frame:GetName().."Gradient", frame);
		local cframe = getglobal(frame:GetName().."Gradient");

		cframe:SetFrameStrata("BACKGROUND");
		cframe:SetFrameLevel(0);
		cframe:ClearAllPoints();
		cframe:SetPoint("TOPLEFT", frame, "TOPLEFT", -4, 4);
		cframe:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 4, 4);
		cframe:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", -4, -8);
		cframe:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", 4, -8);
		cframe:SetBackdrop({bgFile = "Interface\\ChatFrame\\ChatFrameBackground", tile = true, tileSize = 16,
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", edgeSize = 16, insets = {left = 4, right = 4, top = 4, bottom = 4}});
		cframe:SetBackdropBorderColor(0.5, 0.5, 0.5, 1);
		cframe:SetBackdropColor(0, 0, 0, 0.9);

		cframe.grad = cframe:CreateTexture(nil, "BORDER");
		cframe.grad:SetTexture("Interface\\ChatFrame\\ChatFrameBackground");

		cframe.grad:SetPoint("TOPLEFT", cframe, "TOPLEFT", 4, -4);
		cframe.grad:SetPoint("BOTTOMRIGHT", cframe, "TOPRIGHT", -4, -math.ceil(cframe:GetHeight())+5);

		cframe.grad:SetBlendMode("ADD");
		cframe.grad:SetGradientAlpha("VERTICAL", 0.25, 0.25, 0.25, 1, 0.1, 0.1, 0.1, 0);
	end
	WhoLink_SetupDone = true;
end