:- module(yaz_page,
	  [yaz_page//1,
	   html_page_controls//0
	  ]).

:- use_module(library(http/html_write)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_host)).
:- use_module(library(http/http_path)).
:- use_module(library(http/html_head)).
:- use_module(library(http/http_parameters)).
:- use_module(user(user_db)).
:- use_module(applications(isearch)).
:- use_module(library(http/http_wrapper)).
:- use_module(library(semweb/rdf_db)).

:- multifile
	user:body//2.

user:body(yaz, Body) -->
	yaz_page(Body).
user:body(user(isearch), Body) -->
	yaz_search_page(Body).

%%	yaz_page(+Body)
%
%	Hook to provide the same layout and style for each YAZ page.

yaz_page(Body) -->
	html_requires(css('yaz.css')),
	html(body(class('yui3-skin-sam'),
		  [\html_page_header,
		   div(id(body),
		       div(id(content), Body))
		  ])).

%%	yaz_search_page(+Body)
%
%	Hook to provide to apply YAZ style to the search result page.

yaz_search_page(Body) -->
	html_requires(css('yaz.css')),
	html(body([\html_page_header,
		   div([id(body), class(search)],
		       Body)
		  ])).

%%	html_page_header//
%
%	Emit header of the html page.

html_page_header -->
	html(div(id(header),
		 div(class('header-content'),
		     [ h1(a(href(location_by_id(http_yaz_home)), 'YAZ')),
		       div(id(pagesearch), \html_search),
		       %ul(id(pagenavigation), \html_navigation),
		       div(id(pagecontrols), \html_page_controls)
		     ]))).

html_search -->
	{ setting(search:target_class, Class),
	  http_current_request(Request),
	  http_parameters(Request,
			[ q(Query,
			    [ default('')
			    ])
			])
	},
	html(form(action(location_by_id(isearch_page)),
		  \isearch_field(Query, Class))).

html_navigation -->
	html([li(a(href(location_by_id(http_yaz_video_stats)), statistics)),
	      li(a(href(location_by_id(http_yaz_game)), game))
	      %li(a(href(location_by_id(http_yaz_annotate)), annotate))
	     ]).

html_page_controls -->
	login.

login -->
	{ logged_on(User, _),
	  nonvar(User)
	},
	!,
	html(div(class(login),
		 [ a(href(location_by_id(http_yaz_user)), User), ' | ',
		   a(href(location_by_id(http_yaz_logout)), 'Sign out')
		 ])).
login -->
	{ http_link_to_id(http_yaz_home, [login(true)], HREF) },
	html(div(class(login),
		 a(href(HREF), 'Sign in'))).