""" Contains functions used to write specific server packets to byte streams """
import packetHelper
import dataTypes
import userHelper
import glob
import userRanks
import packetIDs
import slotStatuses
import matchModModes
import random

""" Login errors packets
(userID packets derivates) """
def loginFailed():
	return packetHelper.buildPacket(packetIDs.server_userID, [[-1, dataTypes.sInt32]])

def forceUpdate():
	return packetHelper.buildPacket(packetIDs.server_userID, [[-2, dataTypes.sInt32]])

def loginBanned():
	return packetHelper.buildPacket(packetIDs.server_userID, [[-3, dataTypes.sInt32]])

def loginError():
	return packetHelper.buildPacket(packetIDs.server_userID, [[-5, dataTypes.sInt32]])

def needSupporter():
	return packetHelper.buildPacket(packetIDs.server_userID, [[-6, dataTypes.sInt32]])


""" Login packets """
def userID(uid):
	return packetHelper.buildPacket(packetIDs.server_userID, [[uid, dataTypes.sInt32]])

def silenceEndTime(seconds):
	return packetHelper.buildPacket(packetIDs.server_silenceEnd, [[seconds, dataTypes.uInt32]])

def protocolVersion(version = 19):
	return packetHelper.buildPacket(packetIDs.server_protocolVersion, [[version, dataTypes.uInt32]])

def mainMenuIcon(icon):
	return packetHelper.buildPacket(packetIDs.server_mainMenuIcon, [[icon, dataTypes.string]])

def userSupporterGMT(supporter, GMT):
	result = 1
	if supporter == True:
		result += 4
	if GMT == True:
		result += 2
	return packetHelper.buildPacket(packetIDs.server_supporterGMT, [[result, dataTypes.uInt32]])

def friendList(userID):
	friendsData = []

	# Get friend IDs from db
	friends = userHelper.getFriendList(userID)

	# Friends number
	friendsData.append([len(friends), dataTypes.uInt16])

	# Add all friend user IDs to friendsData
	for i in friends:
		friendsData.append([i, dataTypes.sInt32])

	return packetHelper.buildPacket(packetIDs.server_friendsList, friendsData)

def onlineUsers():
	onlineUsersData = []

	users = glob.tokens.tokens

	# Users number
	onlineUsersData.append([len(users), dataTypes.uInt16])

	# Add all users user IDs to onlineUsersData
	for _,value in users.items():
		onlineUsersData.append([value.userID, dataTypes.sInt32])

	return packetHelper.buildPacket(packetIDs.server_userPresenceBundle, onlineUsersData)


""" Users packets """
def userLogout(userID):
	return packetHelper.buildPacket(packetIDs.server_userLogout, [[userID, dataTypes.sInt32], [0, dataTypes.byte]])

def userPanel(userID):
	# Get user data
	userToken = glob.tokens.getTokenFromUserID(userID)
	username = userHelper.getUsername(userID)
	timezone = 24	# TODO: Timezone
	country = userToken.getCountry()
	gameRank = userHelper.getGameRank(userID, userToken.gameMode)
	latitude = userToken.getLatitude()
	longitude = userToken.getLongitude()

	# Get username color according to rank
	# Only admins and normal users are currently supported
	rank = userHelper.getRankPrivileges(userID)
	if username == "FokaBot":
		userRank = userRanks.MOD
	elif rank == 4:
		userRank = userRanks.ADMIN
	elif rank == 3:
		userRank = userRank.MOD
	elif rank == 2:
		userRank = userRanks.SUPPORTER
	else:
		userRank = userRanks.NORMAL


	return packetHelper.buildPacket(packetIDs.server_userPanel,
	[
		[userID, dataTypes.sInt32],
		[username, dataTypes.string],
		[timezone, dataTypes.byte],
		[country, dataTypes.byte],
		[userRank, dataTypes.byte],
		[longitude, dataTypes.ffloat],
		[latitude, dataTypes.ffloat],
		[gameRank, dataTypes.uInt32]
	])


def userStats(userID):
	# Get userID's token from tokens list
	userToken = glob.tokens.getTokenFromUserID(userID)

	# Get stats from DB
	# TODO: Caching system
	rankedScore = 	userHelper.getRankedScore(userID, userToken.gameMode)
	accuracy = 		userHelper.getAccuracy(userID, userToken.gameMode)/100
	playcount = 	userHelper.getPlaycount(userID, userToken.gameMode)
	totalScore = 	userHelper.getTotalScore(userID, userToken.gameMode)
	gameRank = 		userHelper.getGameRank(userID, userToken.gameMode)
	pp = 			int(userHelper.getPP(userID, userToken.gameMode))

	return packetHelper.buildPacket(packetIDs.server_userStats,
	[
		[userID, 				dataTypes.uInt32],
		[userToken.actionID, 	dataTypes.byte],
		[userToken.actionText, 	dataTypes.string],