--[[
	autorun/client/cl_test.lua
	Garbear
	===DStream===
]]

local furry = { }
furry.Prefix = "FRUIT BOT" -- A string with the prefix. IE putting 'FRUIT' would show '[FRUIT] msg'
furry.Blacklist = {
	"gmr_trippledecker_v2",
}
furry.MapBlacklist = {
	"gmr_spliter_crasher",
	"gmr_flash",
}
hook.Add( "InitPostEntity", "LOADJRBOT", function( )
	if ( gmod.GetGamemode( ).Name != "GMod Racer" ) then return end
	function _R.Player:NiggerLevel( )
		if ( !self or !ValidEntity( self ) ) then return 0 end
		local cash = self:GetCash( ) *.0013
		if ( cash > 100 ) then cash = 100 end
		return cash
	end

	function _R.Player:Queued( )
		if ( !self or !ValidEntity( self ) ) then return "Invalid player!" end
		local queued = "isn't"
		local yes = self:GetNWBool( "IsQueued", false )
		if ( yes ) then
			queued = "is"
		end
		return queued
	end

	function _R.Player:Record( )
		if ( !self or !ValidEntity( self ) ) then return "invalid player!" end
		if ( GetGlobalInt('GamemodeType') == 2 ) then
			return "N/A"
		end
		local time = self:GetNWInt( "MapRecord", 1337 )
		if ( time == 1337 ) then
			return "(none)"
		end
		return string.ToMinutesSecondsMilliseconds( time ).." secs"
	end

	function _R.Player:Rank( )
		if ( !self or !ValidEntity( self ) ) then return "INVALID PLAYER" end
		local rank = string.lower( LevelToString( self:GetNWInt( "ASS_isAdmin", 5 ) ) )
		return rank
	end

	function findRichest( )
		local plys = player.GetAll( )
		table.sort( plys, function( a, b )
			return a:GetCash( ) > b:GetCash( )
		end )
		local mine = plys[ 1 ]
		return mine:Nick( ).." ("..mine:SteamID( )..") is the richest nigger here with $"..formatNumber( mine:GetCash( ) ).."!"
	end

	function formatNumber( n )
		-- Thanks to someone on facepunch (raBBish?)
		if ( !n or type( n ) != "number") then
			return 0
		end
		if n >= 1e14 then return tostring( n ) end
		n = tostring( n )
		sep = sep or ","
		local dp = string.find( n, "%." ) or #n+1
		for i=dp-4, 1, -3 do
			n = n:sub( 1, i ) .. sep .. n:sub( i +1 )
		end
		return n
	end

	-- this stuff had to be below, which is weird, since meta shit goes before this
	furry.Enabled = true
	concommand.Add( "jrbot_toggle", function( )
		if ( furry.Enabled ) then
			furry.Enabled = false
			Bot( "Disabled!" )
		else
			furry.Enabled = true
			Bot( "Enabled!" )
		end
	end )
	furry.Ply = LocalPlayer( )
	furry.Replace = { }
	furry.Cmds = {
		{ 
			chat = "_jew",
			msg = "__nick__ (__steam__) has is __jew__% ($__cash__)",
		},
		{
			chat = "_cash",
			msg = "__nick__ (__steam__) has $__cash__",
		},
		/*{
			chat = "_rich",
			msg = findRichest( ),
		},*/
		{
			chat = "_me",
			msg = "__nick__ (__steam__) has $__cash__ & __isqueued__ queued for a race. Map Record: __record__",
		},
		{
			chat = "_rank",
			msg = "__nick__ (__steam__) is a __rank__",
		},
		{
			chat = "_fast",
			msg = "__nick__ (__steam__) is the quickest robbing nigger with a time of __record__",
		},
		{
			chat = "_slow",
			msg = "__nick__ (__steam__) is the worst robbing nigger with a time of __record__",
		},
		{
			chat = "_poor",
			msg = "__nick__ (__steam__) is the poorest nigger with $__cash__",
		},
		{
			chat = "_speedon",
			cmd = "ass_speed __uniqueid__ 1",
		},
		{
			chat = "_speedoff",
			cmd = "ass_speed __uniqueid__ 0",
		},
		{
			chat = "_flameon",
			msg = "__nick__ (__steam__) has turned into a flame!",
		},
	}
	furry.Queue = { }

	-- after the settings and shit
	function Bot( ... )
		if ( !{ ... } ) then return end
		local msg = table.concat( { ... }, " " )
		RunConsoleCommand( "say", "["..string.upper( furry.Prefix ).."] "..msg )
	end

	function EmptyQueue( )
		furry.Replace = {
			{
				old = "__nick__",
				new = furry.Ply:Nick( ),
			},
			{
				old = "__steam__",
				new = furry.Ply:SteamID( ),
			},
			{
				old = "__jew__",
				new = math.ceil( furry.Ply:NiggerLevel( ) ),
			},
			{
				old = "__cash__",
				new = formatNumber( furry.Ply:GetCash( ) ),
			},
			{
				old = "__isqueued__",
				new = furry.Ply:Queued( ),
			},
			{
				old = "__record__",
				new = furry.Ply:Record( ),
			},
			{
				old = "__rank__",
				new = furry.Ply:Rank( ),
			},
			{
				old = "__uniqueid__",
				new = furry.Ply:UniqueID( ),
			},
		}
		for i=1,#furry.Queue do
			local v = furry.Queue[ i ]
			if ( v and v != nil ) then
				timer.Simple( i /2, function( )
					for k2,v2 in pairs( furry.Replace ) do
						if ( v.Msg and v.Msg != nil ) then
							v.Msg = string.gsub( v.Msg, v2.old, v2.new )
						end
						if ( v.Cmd and v.Cmd != nil ) then
							v.Cmd = string.gsub( v.Cmd, v2.old, v2.new )
						end
					end
					Bot( v.Msg )
					if ( v.Cmd and v.Cmd != nil ) then
						LocalPlayer( ):ConCommand( v.Cmd.."\n" )
					end
					table.remove( furry.Queue, i )
				end )
			end
		end
	end

	hook.Add( "OnPlayerChat", 0, function( ply, msg )
		if ( !furry.Enabled ) then return end
		msg = string.lower( string.Trim( msg ) )
		for i=1,#furry.Cmds do
			local v = furry.Cmds[ i ]
			if ( v.chat == msg ) then
				if ( table.HasValue( { "_speedon", "_speedoff" }, v.chat ) and table.HasValue( furry.Blacklist, string.lower( game.GetMap( ) ) ) ) then
					return
				end
				local txt = v.msg or ""
				local cmd = nil
				if ( v.cmd ) then
					cmd = v.cmd
				
				end
				furry.Ply = ply
				timer.Simple( .1, function( )
					EmptyQueue( )
				end )
				if ( cmd and cmd != nil ) then
					table.insert( furry.Queue, { Msg = txt, Cmd = cmd } )
				else
					table.insert( furry.Queue, { Msg = txt } )
				end
			end
		end
	end )
end )

local function mapList( )
	local F = vgui.Create( "DFrame" )
	F:SetTitle( "Map Voting List" )
	F:SetSize( 400, 350 )
	F:SetDraggable( false )
	F:Center( )
	F:MakePopup( )
	
	local info = vgui.Create( "DLabel", F )
	info:SetPos( 5, 20 )
	info:SetSize( F:GetWide( ), 40 )
	info:SetText( [[It couldn't be more fuckin' simple.  Select a map below to make it win!  Seriously.]] )
	info:SetWrap( )
	
	local mlist = vgui.Create( "DPanelList", F )
	mlist:SetPos( 5, 25 )
	mlist:SetSize( F:GetWide( ) -10, F:GetTall( ) -30 )
	mlist:SetSpacing( 5 )
	mlist:EnableHorizontal( false )
	mlist:EnableVerticalScrollbar( true )
	
	local maps = { }
	for k,v in pairs( GAMEMODE.Maps ) do
		table.insert( maps, { ID = k, MapPath = v.MapPath, MapName = v.MapName, MapAuthor = v.MapAuthor, MapDescription = v.MapDescription } )
	end
	table.sort( maps, function( a, b )
		return a.MapName < b.MapName
	end )
	
	for k,v in pairs( maps ) do
		if ( !table.HasValue( furry.MapBlacklist, v.MapPath ) ) then
			local vote = vgui.Create( "DButton" )
			vote:SetText( v.MapName.." ("..v.MapPath..")" )
			vote:SetSize( 75, 30 )
			vote:SetToolTip( string.format( [[Name: %s
			Author: %s
			Description: %s]], v.MapName, v.MapAuthor, v.MapDescription ) )
			vote.DoClick = function( )
				vote:Remove( )
				timer.Simple( .2, function( )
					F:Remove( )
				end )
				local num = 1
				local max = #player.GetAll( )
				timer.Create( "voteNow", .4, max, function( )
					RunConsoleCommand( "gmr_vote", v.ID )
					LocalPlayer( ):ChatPrint( string.format( "[%s] Voting for %s (%s) (%i/%i)", furry.Prefix, v.MapName, v.MapPath, num, max ) )
					num = num +1
					if ( num > max ) then
						LocalPlayer( ):ChatPrint( string.format( "[%s] Voted for %s (%s) %i times, hopefully it won.", furry.Prefix, v.MapName, v.MapPath, max ) )
					end
				end )
			end
			mlist:AddItem( vote )
		end
	end
end
concommand.Add( "map_list", mapList )