/* All gui_... predicates go here. These are mailny predicates that are 
   called from the interaction.pl (which means they are called indirectly 
   from the network. 
   This file also includes predicates that are not named in the gui_...-way
   but basically are predicates to connect logic-input to the gui.
*/


/**
        @descr is called when the user sends a message from the chatline
	@form  gui_chat_sendmessage(Message)
	       Message: The string the user has entered
	       */
gui_chat_sendmessage(Message):-
	append_to_history(Message).

/**
	@descr appends a new line to the chat history window
	@form  gui_chat_append_to_history(Message)
	       Message: The string to append (without linebreak at the end)
	       */
gui_chat_append_to_history(Message):-
	concat(Message,'\n',MessageNew),
	send(@chatwindow,append,MessageNew).

/**
	@descr is called when user has made his choice to move his figure.
               Moves the figure of the user, disable all highlighted fields and 
	       calls return_player_move(Move) in the interactions.pl
	       */
gui_return_player_move(Move):-
	player(me,MyColor),
	moveFigure(MyColor,Move),
	nohighlight,
	noStatusMessage,
	% and give the result to the network (interactions.pl)
	return_player_move(Move).

gui_return_ghost_placement(Place):-
	Place = (_,_),
	%ghost_to_place(GhostID),
	%place_ghost(GhostID,X,Y),
	%remove_preview_ghost,
	retractall(ghost_to_place(_)),
	nohighlight,
	noStatusMessage,
	retractall(action(getGhostPlacement)),
	retractall(possiblePlaces(_)),
	% and give the result to the network (interactions.pl)
	return_ghost_placement(Place).

/**
	@descr is called when the user shall make a move of his figure.
	       GUI enters the state to accept a movement and performs all
	       necessary steps to highlight possible fields
	       */
gui_get_player_move(PossibleMoves):-
	highlightFields(PossibleMoves),
	string(message,getMove,Message),
	displayStatusMessage(Message),
	retractall(possibleMoves(_)),
	retractall(action(getMove)),
	assert(possibleMoves(PossibleMoves)),
	assert(action(getMove)).


gui_get_ghost_placement(PossiblePlaces,GhostID):-
	assert(ghost_to_place(GhostID)),
	highlightFields(PossiblePlaces),
	show_preview_ghost(GhostID),
	string(message,placeGhost,Message),
	displayStatusMessage(Message),
	assert(possiblePlaces(PossiblePlaces)),
	assert(action(getGhostPlacement)).

gui_select_ghost(PossiblePlaces):-
	highlightFields(PossiblePlaces),
	string(message,selectGhost,Message),
	displayStatusMessage(Message),
	assert(possiblePlaces(PossiblePlaces)),
	assert(action(selectGhost)).

gui_return_select_ghost(FieldID):-
	returnFor(ghost),
	retractall(returnFor(_)),
	nohighlight,
	noStatusMessage,
	retractall(action(selectGhost)),
	retractall(possiblePlaces(_)),
	return_select_ghost(FieldID).

gui_return_select_ghost(FieldID):-
	returnFor(village),
	retractall(returnFor(_)),
	nohighlight,
	noStatusMessage,
	retractall(action(selectGhost)),
	retractall(possiblePlaces(_)),
	return_ghost_village(FieldID).

gui_select_villagefield(FieldList):-
	highlightFields(FieldList),
	string(message,selectVillage,Message),
	displayStatusMessage(Message),
	assert(possiblePlaces(FieldList)),
	assert(action(selectVillage)).

gui_return_select_villagefield(FieldID):-
	nohighlight,
	noStatusMessage,
	retractall(action(selectVillage)),
	retractall(possiblePlaces(_)),
	return_select_villagefield(FieldID).

gui_set_this_player(Color):-
	retractall(player(me,_)),
	assert(player(me,Color)),
	concat('You are: ', Color, V),
	send(@youarelabel,value,V),
	send(@youarelabel,colour,colour(Color)).

gui_remove_ghost(FromPlace):-
	forall(ghost(FromPlace,Bitmap),(
		free(Bitmap),
		retractall(ghost(FromPlace,_)))).

gui_set_current_player(PlayerColor):-
	% TODO: Write message if meColor = currentColor
	retractall(player(current,_)),
	assert(player(current,PlayerColor)),
	set_active_player(PlayerColor).

gui_haunt_village(Place,Name):-
	!,
	file(villagefield,Name,haunted,FilePath),
	tile(Place,X,Y,v),
	new(VillageFieldBitmap,bitmap(FilePath)),
	send(@playground,display,VillageFieldBitmap,point(X,Y)),
	drawFigures.

gui_unhaunt_village(Place,Name):-
	!,
	place_villagefield(Name,Place),
	drawFigures.

gui_do_next(List):-
	highlightFields(List),
	assert(possibleMoves(List)),
	string(message,villageOrFight,Message),
	displayStatusMessage(Message),
	assert(action(villageOrFight)).

gui_return_do_next(Choice):-
	nohighlight,
	noStatusMessage,
	return_do_next(Choice).

% Endrekursion! Bam.
place_all_villagefields(OrderList):-
	% first find out where the buddhafield is located
	villageName(Element,buddhist_temple),
	nth1(Index,OrderList,Element),
	retractall(buddhafield(_)),
	assert(buddhafield(Index)),
	% and get the position of the circle of prayer field!
	villageName(Element2,circle_of_prayer),
	nth1(Index2,OrderList,Element2),
	retractall(prayerfield(_)),
	assert(prayerfield(Index2)),
	% just asserts the list of fields
	retractall(villageFieldsRightNow(_)),
	assert(villageFieldsRightNow(OrderList)),
	% now recursion:
	place_all_villagefields(OrderList,1).
place_all_villagefields([],_).
place_all_villagefields([FieldId|Rest],Position):-
	place_villagefield(FieldId,Position),
	PositionNew is Position + 1,
	place_all_villagefields(Rest,PositionNew).
		

gui_set_villagefields(OrderList):-
	place_all_villagefields(OrderList),
	drawFigures,
	place_buddha_all.
	

gui_next_action:-
	noStatusMessage,
	next_action.


%Dice_actions

%One Die
gui_dice_color_all(1,ColorList):-
	hide_dice,
	gui_color_one_die(ColorList).

%Two Dice
gui_dice_color_all(2,ColorList):-
	hide_dice,
	gui_color_two_dice(ColorList).

%Three Dice
gui_dice_color_all(3,ColorList):-
	hide_dice,
       	gui_color_three_dice(ColorList).

%Four Dice
gui_dice_color_all(4,ColorList):-
	hide_dice,
	gui_additional_die_colour_all(ColorList).

%opens a window for choosing a player
gui_choose_player(PlayerList):-
	retractall(chooseTaoist(_,_)),
	free(@playerwindow),
	new(@playerwindow,dialog('Choose a Player')),
	length(PlayerList,Count),
	choose_player(Count,PlayerList),
	send(@playerwindow,open).


%Buddha Actions
gui_buddha_to_player(Color,BuddhaID):-
	player_takes_buddha(Color,BuddhaID).

gui_buddha_to_place(BuddhaID,FieldID):-
	FieldID = (_,_),
	place_buddha_ghostfield(BuddhaID,FieldID).

gui_buddha_to_place(BuddhaID,FieldID):-
	not(FieldID = (_,_)),
	place_buddha_villagefield(BuddhaID,FieldID).
	

gui_get_buddha_placement(PossiblePlaces,BuddhaID):-
	assert(buddha_to_place(BuddhaID)),
	highlightFields(PossiblePlaces),
	string(message,placeBuddha,Message),
	displayStatusMessage(Message),
	assert(possiblePlaces(PossiblePlaces)),
	assert(action(getBuddhaPlacement)).

gui_return_buddha_placement(Place):-
	buddha_to_place(BuddhaID),
	gui_buddha_to_place(BuddhaID,Place),
	retractall(buddha_to_place(_)),
	nohighlight,
	noStatusMessage,
	retractall(action(getBuddhaPlacement)),
	retractall(possiblePlaces(_)),
	return_buddha_placement(Place).
       


