-------------------------------
-- SSTRP
-- Author: MNWRP
--
-- chat.lua
-- Holds the chat commands and functions.
-------------------------------
ChatCommands = { }

function AddChatCommand( cmd, callback, prefixconst )

	table.insert( ChatCommands, { cmd = cmd, callback = callback, prefixconst = prefixconst } );

end

function AddChatLine(recipientfilter, player, filter, text)
	if (player) then
		umsg.Start("Chat_Player_Message", recipientfilter)
			umsg.Entity(player)
			umsg.String(filter)
			umsg.String(text)
		umsg.End()
	else
		umsg.Start("Chat_Message", recipientfilter)
			umsg.String(filter)
			umsg.String(text)
		umsg.End()
	end
end

function AddChatLineInRange(player, filter, text, pos, radius)
	for k, v in pairs(ents.GetAll()) do
		local plypos = v:GetPos()
		if pos:Distance( plypos ) < radius then
			if (v:IsPlayer()) then
				AddChatLine(v, player, filter, text)
			end
		end
	end
end

function GM:PlayerSay( ply, txt, public)
	local text = string.Trim( txt )
	
	local ftext = string.lower( text );
	
	if( string.sub( text, 1, 1 ) == "!" ) then -- All rp_ commands can be executed with !
	
		ply:ConCommand("rp_" .. string.sub( text, 2, string.len(text) ));
		return "";
		
	end
	
	for k, v in pairs( ChatCommands ) do
	
		local endpos = string.len( v.cmd );
		local strcmd = string.sub( ftext, 1, endpos );

		local argstart = 1;
		
		if( string.sub( text, endpos + 1, endpos + 1 ) == " " ) then
			argstart = 2;
		end
		
		if( strcmd == v.cmd ) then
			return v.callback( ply, string.sub( text, string.len( v.cmd ) + argstart ) or "" );

		end
	end
	AddChatLineInRange(ply, "ic", text, ply:GetPos(), 256)
	DayLog("chat.txt", ply:SteamID() .. ": " .. text);
	print( ply:Nick(), " ", ply:Name(), text ) 
	return "";

end

function PM( ply, args )

	local namepos = string.find( args, " " );
	if( not namepos ) then return ""; end
	
	local name = string.sub( args, 1, namepos - 1 );
	local msg = string.sub( args, namepos + 1 );
	
	target = FindPlayer( name );
		
	if( target ) then
		AddChatLine( target, ply, "pm", msg )
		AddChatLine( ply, ply, "pm", msg )
		
	else
		
		ply:ChatPrint( "Unable to find player: " .. name );
		
	end
	
	return "";

end
AddChatCommand( "/pm", PM );

function Whisper( ply, args )
	AddChatLineInRange( ply, "whisper", args, ply:GetPos(), 90 )
	
	return "";

end
AddChatCommand( "/w", Whisper );

function localOOC( ply, args )
	
	AddChatLineInRange( ply, "looc", args, ply:GetPos(), 300 )
	
	return "";

end
AddChatCommand( "[[", localOOC );
AddChatCommand( ".//", localOOC );

function Yell( ply, args )

	AddChatLineInRange( ply, "yell", args, ply:GetPos(), 650 )
	
	return "";

end
AddChatCommand( "/y", Yell );

function DoMe( ply, args )
	AddChatLineInRange( nil, "", ply:Nick() .. " ".. args, ply:GetPos(), 550 )
	
	return "";

end
AddChatCommand( "/me", DoMe );

function OOC( ply, args )
	if( ply.LastOOC + ConVars[ "OOCDelay" ] < CurTime() ) then
	
		ply.LastOOC = CurTime();
		AddChatLine( nil, ply, "ooc", args )
		print( ply:Nick() , " ", ply:Name(), args ) 
	
	else
	
		local TimeLeft = math.ceil( ply.LastOOC + ConVars[ "OOCDelay" ] - CurTime());
		SendChat( ply, "Please wait " .. TimeLeft .. " seconds before using OOC chat again!");
	end
	return "";
end
AddChatCommand( "//", OOC, true );
AddChatCommand( "/ooc", OOC, true );

function Advert( ply, args )
	--pl:AddMoney( - 5 )
	AddChatLine( nil, ply, "advert", args )
	return "";
end
AddChatCommand( "/advert", Advert );
AddChatCommand( "/ad", Advert );

--[[function CombineRequest( ply, args )
	for k, v in pairs( player.GetAll() ) do
		if( v:Team() == 1 ) then
			AddChatLine( v, ply, "request", args )
		end
	end
	ply:ChatPrint( "[REQUESTED]" );
	return "";
end
AddChatCommand( "/cr", CombineRequest );
--]]
function It( ply, args )
	AddChatLineInRange( ply, "it", args, ply:GetPos(), 650 )
	return ""
end
AddChatCommand( "/it", It );

function Event( ply, args )
	for k, v in pairs( player.GetAll() ) do
		if( ply:IsAdmin() or ply:IsSuperAdmin() ) then
			AddChatLine( v, ply, "event", args )
		else
			SendChat( ply, "You're not an admin" )
		end
	end
	return ""
end
AddChatCommand( "/ev", Event );

function Broadcast( ply, args )
	if( !ply:IsAdmin() )then
		SendChat( ply, "Need Admin rights" );
		return ""
	end
	
	local message = tostring( args )
	
	for k, v in pairs( player.GetAll() ) do
		AddChatLine( v, ply, "broadcast", message )
	end
	return "";
end
AddChatCommand( "/broadcast", Broadcast );
AddChatCommand( "/br", Broadcast );

function Radio( ply, text )

	local players = player.GetAll();
	local heardit = { };

	if( Teams[ ply:Team() ] == nil ) then return ""; end

	if(Teams[ply:Team()]["radio_groups"] != {}) then
		for k, v in pairs( Teams[ ply:Team() ][ "radio_groups" ] ) do
			for k2, v2 in pairs(player.GetAll()) do
				if( Teams[ v2:Team() ] != nil ) then
					if( table.HasValue( Teams[ v2:Team() ][ "radio_groups" ], v ) ) then
						AddChatLine( v2, ply, "radio", text);
						table.insert(heardit, v2);
					end
				end
			end
		end
	end

	for k, v in pairs( players ) do
		if( !table.HasValue( heardit, v ) ) then
			local range = ConVars[ "TalkRange" ]
			if( v:EyePos( ):Distance( ply:EyePos( ) ) <= range ) then
				SendChat( v, ply:Nick() .. ": " .. text );
			end
		end
	end
	print( ply:Nick(), text ) 
	return "";
end
AddChatCommand( "/r", Radio );
AddChatCommand( "/radio", Radio );