# Path: objects/glob.py
# 		VERSION = f.read().strip()
# 	VERSION = "Unknown"
# DATADOG_PREFIX = "peppy"


from common.redis import generalPubSubHandler
from common.ripple import userUtils
from common.log import logUtils as log
from common.constants import actions
from objects import glob

def handleUsernameChange(userID, newUsername, targetToken=None):
	try:
		userUtils.appendNotes(userID, "Username change: '{}' -> '{}'".format(userUtils.getUsername(userID), newUsername))
		userUtils.changeUsername(userID, newUsername=newUsername)
		if targetToken is not None:
			targetToken.kick("Your username has been changed to {}. Please log in again.".format(newUsername), "username_change")
	except userUtils.usernameAlreadyInUseError:
		log.rap(999, "Username change: {} is already in use!", through="Bancho")
		if targetToken is not None:
			targetToken.kick("There was a critical error while trying to change your username. Please contact a developer.", "username_change_fail")
	except userUtils.invalidUsernameError:
		log.rap(999, "Username change: {} is not a valid username!", through="Bancho")
		if targetToken is not None:
			targetToken.kick("There was a critical error while trying to change your username. Please contact a developer.", "username_change_fail")

class handler(generalPubSubHandler.generalPubSubHandler):
	def __init__(self):
		super().__init__()
		self.structure = {
			"userID": 0,
			"newUsername": ""
		}

	def handle(self, data):
		data = super().parseData(data)
		if data is None:
			return
		# Get the user's token

# Path: objects/glob.py
# 		VERSION = f.read().strip()
# 	VERSION = "Unknown"
# DATADOG_PREFIX = "peppy"


# Path: pubSubHandlers/changeUsernameHandler.py
from common.redis import generalPubSubHandler
from common.ripple import userUtils
from common.log import logUtils as log
from common.constants import actions
from objects import glob


def handleUsernameChange(userID, newUsername, targetToken=None):
	try:
		userUtils.appendNotes(userID, "Username change: '{}' -> '{}'".format(userUtils.getUsername(userID), newUsername))
		userUtils.changeUsername(userID, newUsername=newUsername)
		if targetToken is not None:
			targetToken.kick("Your username has been changed to {}. Please log in again.".format(newUsername), "username_change")
	except userUtils.usernameAlreadyInUseError:
		log.rap(999, "Username change: {} is already in use!", through="Bancho")
		if targetToken is not None:
			targetToken.kick("There was a critical error while trying to change your username. Please contact a developer.", "username_change_fail")
	except userUtils.invalidUsernameError:
		log.rap(999, "Username change: {} is not a valid username!", through="Bancho")
		if targetToken is not None:
			targetToken.kick("There was a critical error while trying to change your username. Please contact a developer.", "username_change_fail")

class handler(generalPubSubHandler.generalPubSubHandler):
	def __init__(self):
		super().__init__()
		self.structure = {
			"userID": 0,
			"newUsername": ""
		}

	def handle(self, data):
		data = super().parseData(data)
		if data is None:
			return
		# Get the user's token

		targetToken = glob.tokens.getTokenFromUserID(data["userID"])