-- Lavacano: I've converted all the comments to pure Lua - it breaks my editor otherwise.

local CATEGORY_NAME = "Fun"

function ulx.settheirtitle(calling_ply, target, targettitle)

	if not target then
		ULib.tsay( ply, err, true )
		return
	end
	
		if string.len(targettitle) > 40 then
		ULib.tsay( calling_ply, "Titles cannot be longer than 40 characters!", true )
		return
	end
	
	target:SetNWString("title", targettitle)
	
	
	if (calling_ply == target) then
		-- If players are using this function to set their own titles, remind them that the !title function is meant for this.
		ULib.tsay( calling_ply, "Your title has been set to '" .. targettitle .. "'!", true )
		ULib.tsay( calling_ply, "You can also use !title to set your own title.", true )
	end
	
	-- Store title in database of user's choice
	local result = executequery("SELECT * FROM titles WHERE steamid = '" .. target:SteamID() .. "'")
	local num_rows = 0
	
	-- Check if the result is nil, if not, check the number of rows. MySQL returns data even if it returns no rows.
	if not (result == nil) then
		for k, v in pairs(result) do
			num_rows = num_rows + 1
		end
	end
	
	if (num_rows == 0) then -- Check if user exists
		
		-- if they don't, make a new record
		result = executequery("INSERT INTO titles (steamid, title) VALUES('" .. target:SteamID() .. "', '" .. targettitle .. "')")
		
	else
		
		-- If they do,  update the DB
		result = executequery("UPDATE titles SET title = '" .. targettitle .. "' WHERE steamid = '" .. target:SteamID() .. "'")
		
	end
	
end

local settitle_cmd = ulx.command(CATEGORY_NAME, "ulx settitle", ulx.settheirtitle, "!settitle")
settitle_cmd:addParam{ type=ULib.cmds.PlayerArg }
settitle_cmd:addParam{ type=ULib.cmds.StringArg, hint="title", ULib.cmds.takeRestOfLine }
settitle_cmd:defaultAccess(ULib.ACCESS_ADMIN)
settitle_cmd:help("Change target's title to <title>")
settitle_cmd:logString("#1s changed #2s's title to #3s")

function ulx.settitle(ply, targettitle)
	
	if string.len(targettitle) > 40 then
		ULib.tsay( ply, "Titles cannot be longer than 40 characters!", true )
		return
	end

	ply:SetNetworkedString("title", targettitle)
	
	ULib.tsay( ply, "Your title has been set to '" .. targettitle .. "'!", true )

	-- Store title in database of user's choice
	local result = executequery("SELECT * FROM titles WHERE steamid = '" .. ply:SteamID() .. "'")
	local num_rows = 0
	
	// Check if the result is nil, if not, check the number of rows. MySQL returns data even if it returns no rows.
	if not (result == nil) then
		for k, v in pairs(result) do
			num_rows = num_rows + 1
		end
	end
	
	if (num_rows == 0) then -- Check if user exists
		
		-- if they don't, make a new record
		result = executequery("INSERT INTO titles (steamid, title) VALUES('" .. ply:SteamID() .. "', '" .. targettitle .. "')")
		
	else
		
		-- If they do,  update the DB
		result = executequery("UPDATE titles SET title = '" .. targettitle .. "' WHERE steamid = '" .. ply:SteamID() .. "'")
		
	end
end
local title_cmd = ulx.command(CATEGORY_NAME, "ulx title", ulx.settitle, "!title")
title_cmd:addParam{ type=ULib.cmds.StringArg, hint="title", ULib.cmds.takeRestOfLine }
title_cmd:defaultAccess(Ulib.ACCESS_ALL)
title_cmd:help("Set your title to <title>. Note that admins can change it at any time.")

function ulx.mytitle(ply)

local mytitle = ply:GetNetworkedString("title")
if mytitle == "" then
	ULib.tsay( ply, "You don't have a custom title.", true )
else
	ULib.tsay( ply, "Your title is '" .. mytitle .. "'.", true )
end
end

local mytitle_cmd = ulx.command(CATEGORY_NAME, "ulx mytitle", ulx.mytitle, "!mytitle")
mytitle_cmd:defaultAccess(ULib.ACCESS_ALL)
mytitle_cmd:help("Check your title.")


-- Lavacano: Yeah, uh...I have no idea wtf this is. Imma leave it alone.
function checkexclusive()

	for k, v in pairs(player:GetAll()) do
		
		if v:GetNWBool("exclusivestatus") == true then
			-- Check current status
			if ulx.getExclusive( v, v ) then
				-- Check if  the type is on the exclusion list
				excltype = ulx.getExclusive( v, v)
				
				if (excltype == "You are in jail!") then
					-- Player is in jail, name needs to be shown.
					v:SetNWBool("exclusivestatus", false)
				else
					-- Names need to be hidden and the bool is set correctly
				end
				
			else
				-- Bool is not set correctly set to false so we can see people's names.
				v:SetNWBool("exclusivestatus", false)
			end
		else
			-- Bool is false. Check if exclusive is set and set nwbool if necessary
			if ulx.getExclusive( v, v ) then
				-- Check if  the type is on the exclusion list
				excltype = ulx.getExclusive( v, v)
				
				if (excltype == "You are in jail!") then
					-- Player is in jail, bool is set correctly.
				else
					-- Player is exclusive'd but the bool isn't set. Set it.
					v:SetNWBool("exclusivestatus", true)
				end
				
			else
				-- Bool is set correctly.
			end
		end
	end

end
timer.Create( "exclusivechecktimer", 4, 0, checkexclusive)
