import sys
import threading

from Code.Communication.Direction import Direction
from Code.Communication.Member import Member
from Code.Communication.Neighbours import Neighbours
from Code.Communication.Server import Server
from Code.UI.PlayingField import run_game, GameState

if __name__ == "__main__":

	"""
	Getting the args
	- own ip
	- own port
	
	Optional: A neighbour:
		- ip
		- port 
		- direction
	"""

	args = sys.argv

	print(args)
	if len(args) >= 3:
		own_ip = args[1]
		own_port = int(args[2])
	else:
		print("using default ip 0.0.0.0")
		print("using default port 8080")
		own_ip = "0.0.0.0"
		own_port = 8080

	neighbours = Neighbours(own_process=Member(own_ip, own_port))

	counter = 0
	n_direction = Direction.LEFT
	if len(args) > 5:
		n_direction = args[5]
	if len(args) >= 5:
		n_ip = args[3]
		n_port = int(args[4])
		counter= neighbours.connect(Direction[n_direction], n_ip, n_port)

	game_state = GameState(neighbours,counter)
	server = Server(neighbours, game_state)

	serverThread = threading.Thread(target=server.start)
	serverThread.start()
	run_game(game_state)
	print("finished game")
	server.stop_server()
