/***
@author	8njemz & 8boomgaa
@date	2010/02/28
@descr	gui for starting a client & game
*/
:-consult('main.pl').
:-dynamic(number_of_clients/1).

go:- 
	free(@ipinput),
	free(@portinput),
	free(@setnick),
	new(@dialog_client,dialog('Start client')),
	send_list(@dialog_client,append,
		[
		new(@ipinput,text_item('IP')),
		new(@portinput,int_item('Port')),
		new(@playercount,int_item('Number of players')),
		new(Ok,button('Start client',message(@prolog,start_all,@playercount?selection,@ipinput?selection,@portinput?selection))),
		new(Close,button('Stop client',message(@prolog,disconnect_from_server))),
		new(@setnick,text_item('Nickname')),
		new(SetNick,button('Set nickname',message(@prolog,nickname,@setnick?selection))),
		new(CloseAll,button('Close all',message(@prolog,close_all)))
		]),
	send(@ipinput,value,'127.0.0.1'),
	send(@portinput,value,'1337'),
	send(@playercount,value,'1'),
	send(@playercount,range,1,4),
	send(SetNick,default_button,@on),
	send(@dialog_client,open).

/**
@form	start_all(+Number,+IP,+Port)
@descr	starts a Number of clients to IP and with Port
*/
start_all(Num,IP,Port):-
	% debug output
	write_ln('starting client...'),
	start_client(IP,Port),
	Num1 is Num - 1,
	assert(number_of_clients(Num1)).

/**
@form	nickname(+Nick)
@descr	change nickname with Nick
*/
nickname('42'):-
	nick('42'),
	send(@setnick,value,''),
	sendmessage('\\nthe answer to life, the universe and everything\\n').
%	append_status_to_history('\nthe answer to life, the universe and everything\n').
nickname(Nick):-
	nick(Nick),
	send(@setnick,value,'').

/**
@form	close_all
@descr	closes all windows
*/
close_all:-
	send(@mainwindow,destroy),
	free(@mainwindow),
	send(@dialog_client,destroy),
	free(@dialog_client).

/**
@form	disconnect_from_server
@descr	disconnects all connections
*/
disconnect_from_server:-
	exit_connection,
	( retractall(number_of_clients(_)) ; true ),
	append_status_to_history('disconnected from server').

:-go.
