/***
@author	8njemz & 8boomgaa
@date	2010/02/28
@descr	gui for starting a server
*/
:-consult('../network/server.pl').
:-consult('strings.pl').
:-consult('../network/server_interface.pl').
:-consult('../logik/main.pl').

go:- 
	free(@iplabel),
	free(@portinput),
	free(@console),
	free(@kickgroup),
	string(serverwindowtitle,TitleString),
	new(@dialog_server,dialog(TitleString)),
%	send_list(@dialog_server,append,
%	[
	send(@dialog_server,append,new(@portinput,int_item('Port'))),
	send(@dialog_server,append,new(@iplabel,label('foo')),next_row),
	send(@dialog_server,append,new(Ok,button('Open Server',message(@prolog,start_server,@portinput?selection))),next_row),
	send(@dialog_server,append,new(Shutdown,button('Shutdown Server',message(@prolog,exit))),right),
	send(@dialog_server,append,new(Start,button('Start Game', message(@prolog, start_game),next_row)),next_row),
	send(@dialog_server,append,new(Close,button('Close',message(@prolog,close_all))),right),
	send(@dialog_server,append,new(@console,editor),next_row),
	send(@dialog_server,append,new(Update,button('Update status of players',message(@prolog,update_status_of_players))),next_row),
	send(@dialog_server,append,new(@kickgroup,dialog_group('kick players')),next_row),
%	]),
	Text = 'Your IP address is: ',
	get_ip(IP),
	atom_concat(Text,IP,Text2),
	send(@iplabel,value,Text2),
	send(@portinput,value,'1337'),
	send(@console,editable,false),
	%%%%%%%%%%%%%%%%%% KICKGROUP %%%%%%%%%%%%%%%
	send(@kickgroup,append,new(@label1,label('player 1'))),
	send(@kickgroup,append,new(@button1,button('kick',message(@prolog,kick_player,1))),right),
	send(@kickgroup,append,new(@label2,label('player 1'))),
	send(@kickgroup,append,new(@button2,button('kick',message(@prolog,kick_player,2))),right),
	send(@kickgroup,append,new(@label3,label('player 1'))),
	send(@kickgroup,append,new(@button3,button('kick',message(@prolog,kick_player,3))),right),
	send(@kickgroup,append,new(@label4,label('player 1'))),
	send(@kickgroup,append,new(@button4,button('kick',message(@prolog,kick_player,4))),right),
	send(@kickgroup,area,area(0,0,350,130)),
	send(@label1,value,'player not connected'),
	send(@label2,value,'player not connected'),
	send(@label3,value,'player not connected'),
	send(@label4,value,'player not connected'),
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	send(@dialog_server,open).

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

/**
@form	update_status_of_players
@descr	updates the list of which players are connected
*/
update_status_of_players:-
	findall(IDs,client(_,_,_,_,IDs),IDList),
	iter_over_idlist(4).

/**
@form	iter_over_idlist(+ID)
@descr	checks IDs, if they are connected
*/
iter_over_idlist(0).
iter_over_idlist(ID):-
	( client(_,_,_,_,ID) -> ( id2nick(ID,Nick) -> Name = Nick ; Txt = 'player ', concat(Txt,ID,Name) ) ; Name = 'player not connected' ),
	add_player(ID,Name),
	IDNew is ID - 1,
	iter_over_idlist(IDNew).

/**
@form	add_player(+ID,+Name)
@descr	set new label description of the ID
*/
add_player(1,Name):-
	send(@label1,value,Name).
add_player(2,Name):-
	send(@label2,value,Name).
add_player(3,Name):-
	send(@label3,value,Name).
add_player(4,Name):-
	send(@label4,value,Name).

/**
@form	kick_player(+ID)
@descr	kicks the player with ID, if he is connected
*/
kick_player(ID):-
	not(client(_,_,_,_,ID)),
	new(String,string('player %d not connected',ID)),
	get(String,value,Txt),
	write_log(Txt).
kick_player(ID):-
	client(_,_,_,_,ID),
	id2socket(ID,Socket),
	( id2nick(ID,Nick) -> Name = Nick ; Name = ID ),
	concat(Name, ' is being kicked. Bye.', Txt),
	term_to_atom(append_status_to_history(Txt),Atom),
	send_to_all(Atom),
	send(Socket,unlink),
	leave_message(ID),
	( retractall(id2nick(ID,_)) ; true ),
	retractall(client(_,_,_,Socket,_)).

:-go.
