from Logger import logger
from importlib import reload

from sims4.commands import *

import SimulationClient as sim_client

_client = None

# class DualWriter(object):
# 	def __init__(self,a,b):
# 		self.a = a
# 		self.b = b
# 		
# 	def __call__(self,message):
# 		self.a(message)
# 		self.b(message)

@Command("rtbp_client_mod.restart",command_type = CommandType.Live)
def rtbpReload(_connection = None):
	global _client
	
	try:
		out = CheatOutput(_connection)
		
		message = "Reloading and restarting client"
		
		logger.info(message)
		out(message)
		
		if _client:
			_client.close()
		
		reload(sim_client)
		
	except Exception as error:
		message = "{}: {}".format(error.__class__.__name__,error)
		
		logger.error(message)
		out(message)
	
	rtbpClientStart()

def rtbpClientStart():
	global _client
	
	try:
		logger.info("RTBP simulation client v0.1")
		
		_client = sim_client.SimulationClient()
		
		if not _client.connect("127.0.0.1",8009):
			raise Exception("Unable to connect")
		
	except Exception as error:
		logger.error("{}: {}".format(error.__class__.__name__,error))

rtbpClientStart()
