:- dynamic(sendmessage/1).
:- dynamic(append_to_history/1).
:- dynamic(player_position/2).
:- dynamic(action/1).
:- dynamic(player_position/2).
:- dynamic(ghost/2).
:- [guigroups].
:- [interaction].
:- [data].
:- [strings].
:- [hauntghost].
:- [buddha].
:- [drawing].
:- [gui_actions].
:- [gameover].
:- [fightdialog].
:- [dice_curse].
:- [diceresult_graphical].
:- [death].
:- [alive].
:- [choose_tao_marker].
:- [tao_on_prayer].
:- [choose_player_window].


/**
    @descr Performs all necessary steps to initialize and display the GUI
    @form main
*/
main:-
	free(@mainwindow),
	new(@mainwindow, dialog('Ghost Stories')),
	% Clear old rubbish:
	retractall(action(_)),
	retractall(player(_,_)),
	% create and fetch groups here
	gameboardGroup,
	playersGroup,
	chatGroup,
	statusGroup,
	next_action_group,
	send(@mainwindow, display, @gameboardgroup, point(5,5)),
	send(@mainwindow, display, @playersgroup, point(810,5)),
	send(@mainwindow, display, @chatgroup, point(810, 200)),
	send(@mainwindow, display, @statusgroup,point(5,790)),
	send(@mainwindow, display, @nextgroup,point(910,790)),
	send(@mainwindow,open),
	% FIXME: still dummy
	place_all_villagefields([1,2,3,4,5,6,7,8,9]),
	gui_set_this_player(yellow),
 	gui_set_current_player(red),
	drawFigures,
	set_label_all,
	place_buddha_all,
	send(@mainwindow, display, @gameboardgroup, point(5,5)),
	send(@mainwindow, display, @playersgroup, point(810,5)),
	send(@mainwindow, display, @chatgroup, point(810, 200)),
	send(@mainwindow, display, @statusgroup,point(5,790)),
	send(@mainwindow, display, @nextgroup,point(810,790)).


/********************** GUI event handling **********************************/


/**
	@descr is called when the playground is clicked. 
	       This one works for ghost fields 
	@form  call_playground_clicked(X,Y)       
	       */
% TODO: Still implemented as dummy that places a ghost on the clicked field
call_playground_clicked(X,Y):-
	action(getGhostPlacement),
	which_tile(X,Y,TileID),
	possiblePlaces(Places),
	member(TileID,Places),
	%tile(TileID,_,_),
	TileID = (_,_),
	gui_return_ghost_placement(TileID).

call_playground_clicked(X,Y):-
	action(selectGhost),
	which_tile(X,Y,TileID),
	possiblePlaces(Places),
	member(TileID,Places),
	TileID = (_,_),
	gui_return_select_ghost(TileID).


call_playground_clicked(X,Y):-
	action(selectVillage),
	which_tile(X,Y,TileID),
	possiblePlaces(Places),
	member(TileID,Places),
	not(TileID = (_,_)),
	gui_return_select_villagefield(TileID).

	
call_playground_clicked(X,Y):-
	action(getBuddhaPlacement),
	which_tile(X,Y,TileID),
	possiblePlaces(Places),
	member(TileID,Places),
	TileID = (_,_),
	gui_return_buddha_placement(TileID).


/** 
        @descr is called when the playground is clicked
	       This one is for choosing a village field to move the player
	@form  call_playground_clicked(X,Y)
	*/
call_playground_clicked(X,Y):-
	action(getMove),
	which_tile(X,Y,TileID),
	format('(move) ~d / ~d : ~d',[X,Y,TileID]),
	possibleMoves(Possible),
	member(TileID,Possible),
	retractall(possibleMoves(_)),
	retractall(action(getMove)),
	gui_return_player_move(TileID).



% village or fight ghost?
call_playground_clicked(X,Y):-
	action(villageOrFight),
	which_tile(X,Y,TileID),
	possibleMoves(Possible),
	member(TileID,Possible),
	retractall(possibleMoves(_)),
	retractall(action(villageOrFight)),
	gui_return_do_next(TileID).

/**
	@descr for debug purposes, prints X/Y on the command line if 
	       action(debug) is set 
	       */
call_playground_clicked(X,Y):-
	action(debug),
	which_tile(X,Y,(A,B)),
	format('(click) ~d / ~d : (~d, ~d ) ~n',[X,Y,A,B]).

	
/********************** GUI pixel calculations **********************************/

/**
        @descr Returns the TileID that is located at the clicked point
	       When a village field is clicked, TileID is Number 1-9
	       When a ghost field is clicked, TileID is a tuple (_,_)
	@form  which_tile(X,Y,TileID)
	*/
% village fields
which_tile(X,Y,TileID):- 
	% get a village tile
	tile(TileID, LeftBorder, TopBorder, v),
	RightBorder is LeftBorder + 120,
	BottomBorder is TopBorder + 120,
	X > LeftBorder,
	X < RightBorder,
	Y > TopBorder,
	Y < BottomBorder.
which_tile(X,Y,TileID):- 
	% get a non village tile from top or down
	tile(TileID, LeftBorder, TopBorder, updown),
	RightBorder is LeftBorder + 120,
	BottomBorder is TopBorder + 180,
	X > LeftBorder,
	X < RightBorder,
	Y > TopBorder,
	Y < BottomBorder.
which_tile(X,Y,TileID):- 
	% get a non village tile from left or right side
	tile(TileID, LeftBorder, TopBorder, Where),
	not( Where = updown ),
	not( Where = v),
	RightBorder is LeftBorder + 180,
	BottomBorder is TopBorder + 120,
	X > LeftBorder,
	X < RightBorder,
	Y > TopBorder,
	Y < BottomBorder.



/********************** Program start **********************************/

% FIXME: Still contains dummy data
% TODO: Disable scrolling for PlaygroundImage
:- main.
%:- next_action_group.

% by Rolf
:- [village_info].


%:- gui_set_this_player(yellow).
%:- gui_set_current_player(red).
