--
-- TeamSpeak 3 custom functions for testing and demonstration
--

require("ts3defs")
require("ts3errors")

--
-- Call these function from the TeamSpeak 3 client console via: /lua run <function>
-- Note the serverConnectionHandlerID of the current server is always passed.
--
-- You might want to pass the "-console" option when starting the TeamSpeak 3 client to get a console where a lot
-- of plugin related debug output will appear.
--

-- Run with "/lua run showClientLibVersion"
function showClientLibVersion(serverConnectionHandlerID)
	local version, error = ts3.getClientLibVersion()
	if error ~= ts3errors.ERROR_ok then
		print("Error getting clientlib version: " .. error)
		return
	end
	ts3.printMessageToCurrentTab("Clientlib version: " .. version)
end

-- Run with "/lua run test"
function test(serverConnectionHandlerID)
	ts3.printMessageToCurrentTab("Test on serverConnectionHandlerID: " .. serverConnectionHandlerID)

	-- Get own client ID
	local myClientID, error = ts3.getClientID(serverConnectionHandlerID)
	if error ~= ts3errors.ERROR_ok then
		print("Error getting own client ID: " .. error)
		return
	end
	if myClientID == 0 then
		ts3.printMessageToCurrentTab("Not connected")
		return
	end
	ts3.printMessageToCurrentTab("My client ID: " .. myClientID)

	-- Get own nickname
	local myNickname, error = ts3.getClientVariableAsString(serverConnectionHandlerID, myClientID, ts3defs.ClientProperties.CLIENT_NICKNAME)
	if error ~= ts3errors.ERROR_ok then
		print("Error getting own client nickname: " .. error)
		return
	end
	ts3.printMessageToCurrentTab("My nickname: " .. myNickname)

	-- Get which channel we are in
	local myChannelID, error = ts3.getChannelOfClient(serverConnectionHandlerID, myClientID)
	if error ~= ts3errors.ERROR_ok then
		print("Error getting own channel: " .. error)
		return
	end

	-- Get the name of my channel
	myChannelName, error = ts3.getChannelVariableAsString(serverConnectionHandlerID, myChannelID, ts3defs.ChannelProperties.CHANNEL_NAME)
	if error ~= ts3errors.ERROR_ok then
		print("Error getting channel name: " .. error)
		return
	end
	ts3.printMessageToCurrentTab("I am in channel ID: " .. myChannelName .. " (" .. myChannelID .. ")")
end

-- Run with "/lua run argsTest <arg1> <arg2> <arg3>", args can be numbers or strings
function argsTest(serverConnectionHandlerID, arg1, arg2, arg3)
	ts3.printMessageToCurrentTab("argsTest: " .. serverConnectionHandlerID .. " - " .. arg1 .. " " .. arg2 .. " " .. arg3)
end

-- Run with "/lua run showClients"
function showClients(serverConnectionHandlerID)
	local clients, error = ts3.getClientList(serverConnectionHandlerID)
	if error == ts3errors.ERROR_not_connected then
		ts3.printMessageToCurrentTab("Not connected")
		return
	elseif error ~= ts3errors.ERROR_ok then
		print("Error getting client list: " .. error)
		return
	end

	local msg = ("There are currently " .. #clients .. " visible clients:")
	for i=1, #clients do
		local clientName, error = ts3.getClientVariableAsString(serverConnectionHandlerID, clients[i], ts3defs.ClientProperties.CLIENT_NICKNAME)
		if error == ts3errors.ERROR_ok then
			msg = msg .. "\n " .. clients[i] .. " " .. clientName
		else
			clientName = "Error getting client name"
		end
	end
	ts3.printMessageToCurrentTab(msg)
end

-- Run with "/lua run complainAboutMyself <complainReason>"
function complainAboutMyself(serverConnectionHandlerID, complainReason)
	-- Add a complain about own client. Makes no sense, but hey, it's a demo. :-)
	
	-- Get own client ID
	local myClientID, error = ts3.getClientID(serverConnectionHandlerID)
	if error ~= ts3errors.ERROR_ok then
		print("Error getting own client ID: " .. error)
		return
	end
	if myClientID == 0 then
		ts3.printMessageToCurrentTab("Not connected")
		return
	end
	
	-- As we need the databaseID for complains, get own database ID from own client ID
	local myDatabaseID, error = ts3.getClientVariableAsUInt64(serverConnectionHandlerID, myClientID, ts3defs.ClientProperties.CLIENT_DATABASE_ID)
	if error ~= ts3errors.ERROR_ok then
		print("Error getting own client database ID: " .. error)
		return
	end
	
	-- Send complain
	local error = ts3.requestComplainAdd(serverConnectionHandlerID, myDatabaseID, complainReason)
	if error ~= ts3errors.ERROR_ok then
		print("Error adding complain: " .. error)
		return
	end
	ts3.printMessageToCurrentTab("Complain about myself added with reason: " .. complainReason)
end

-- Run with "/lua run showClientsInChannel <channelID>"
function showClientsInChannel(serverConnectionHandlerID, channelID)
	-- Get list of clients in channelID passes as parameter
	clientList, error = ts3.getChannelClientList(serverConnectionHandlerID, channelID)
	if error ~= ts3errors.ERROR_ok then
		print("Error getting channel client list: " .. error)
		return
	end

	-- Get name of this channel
	channelName, error = ts3.getChannelVariableAsString(serverConnectionHandlerID, channelID, ts3defs.ChannelProperties.CHANNEL_NAME)
	if error ~= ts3errors.ERROR_ok then
		print("Error getting channel name: " .. error)
		return
	end

	-- Loop through all clients in list and assemble message from their clientID and nickname
	msg = "Visible clients in channel " .. channelName
	for i=1, #clientList do
		local clientName, error = ts3.getClientVariableAsString(serverConnectionHandlerID, clientList[i], ts3defs.ClientProperties.CLIENT_NICKNAME)
		if error == ts3errors.ERROR_ok then
			msg = msg .. "\n " .. clientList[i] .. " " .. clientName
		else
			clientName = "Error getting client name"
		end
	end	
	ts3.printMessageToCurrentTab(msg)
end
