/*

  Should be much more configurable -- the exports the notion
  of Ontaria down into SemWalker far more than it should....
  
*/

:- dynamic(front_cache/1).
:- dynamic(number_subject/2).
:- dynamic(max_subject/1).

homeBody(Q, H) :-
	app_config(site_title, Title),
	app_config(release, Release),
	H=div(class=homeBody,
	      div(class=homeBody2,
		  div(class=left,
		      +tenRandomSubjects,
		      +tenRandomOntologies),
		  div(class=main,
		      div(class=main2,
			  h2('Welcome to ', Title, ' (', Release, ')'),
			  include_static_html('about-short')
			 )
		     ),
		  div(class=right,
		      +gettingStarted,
		      +dbInfo(Q))
		  )
	     ).
/*	
ten(Htree) :-
	subjects_list(S),
	pick10(S, T),
	foo(T, H),
	Htree = [p('Ten Random Subjects:  [', a(href="javascript:window.location.reload()",reload), ']'), ol(H)].

*/

foo([], []).
foo([U|Us], [li(+shortWord([], U))|R]) :- foo(Us, R).

/*
xten(Htree) :-
	sources_cache(S),
	pick10(S, T),
	xfoo(T, H),
	Htree = [p('Ten Random Sources:'), ol(H)].
*/

xfoo([], []).
xfoo([U|Us], [li(+word([truncate(22)], U))|R]) :- xfoo(Us, R).

reset_home_page_cache :-
	retractall(max_subject(_)),
	retractall(front_cache(_)).
	
numbered_subjects(Max) :-
	max_subject(Max), !.
numbered_subjects(Max) :-
	with_mutex(home_number_subjects_mutex,
		   (
		     retractall(number_subject),
		     flag(home_ns, _, 0),
		     forall( (  rdf(Subject, rdfs:label, _),
				 \+ rdfpage_uri(Subject)
			     ),
			     (  flag(home_ns, N, N+1),
				 assert(number_subject(N, Subject))
			     )
			   ),
		     flag(home_ns, Max, Max),
		     assert(max_subject(Max))
		   )
		  ).
	
tenRandomSubjects(Htree) :-
	numbered_subjects(Max),
	Max > 0,
	setofsome(10, _, Subject,
		  (   repeat,
		      Random is random(Max),
		      number_subject(Random, Subject)
		  ),
		  Subjects),
	xfoo(Subjects, H),
	Htree = div(class=box,
		    h3('Random Recent Subjects'),
		    ol(H),
		    p('Reload for a new selection')
		   ).

tenRandomSubjects('(No active subjects)').

tenRandomSources(Htree) :-
        sources(S),
	S = [_|_],
	pick10(S, T),
	foo(T, H),
	Htree = div(class=box,
		    h3('Ten Random Sources'),
		    ol(H),
		    p('Reload for a new selection')
		   ).
tenRandomSources(' ').

tenRandomOntologies(Htree) :-
        ontologies(S),
	S = [_|_],
	pick10(S, T),
	foo(T, H),
	Htree = div(class=box,
		    h3('Random Ontologies'),
		    ol(H),
		    p('Reload for a new selection')
		   ).
tenRandomOntologies(' ').

number_of_sources(N) :-
	(   front_cache(number_of_sources(N))
	->  true
	;   sources(L),
	    length(L, N),
	    assert(front_cache(number_of_sources(N)))
	).
	    

sources(L) :-
	findall(Source,
		(   address_latest_ret(Source, Ret),
		    ret_get(Ret, retr:triples, _)
		),
		S),
	sort(S, L).
	

ontologies(L) :-
	findall(Ont,
		(   rdfpage(Ret, retr:initialResource, Ont),
		    rdfpage(Ret, retr:chunk, Graph),
		    rdfpage(Ret, retr:inference, Proof),
		    rdf(Graph, rdf:type, owl:'Ontology', Proof)
		),
		S),
	sort(S, L).

dbInfo(_Q, HTree) :-
	%   redo as just displaying self:db in a box
	%   include "last visited" etc.
	disk_triples(DTriples),
	rdf_statistics(triples(RTriples)),
	flag(latest_rdf_db_mod_time, LastModTime, LastModTime),
	flag(latest_rdf_db_mod_source, LastModSrc, LastModSrc),
	%list_of_sources_object(ListOfSourcesObject),
	number_of_sources(N),
	%ListOfSources= \self_uri(basic, Q, [focus=ListOfSourcesObject]),
	HTree = div(class=box,
		    h3('Current Database'),
		    table(class=miniPTable,
			  tr(td(class=miniPTableProperty, 'Triples on Disk:' ),
			     td(class=miniPTableValue, format('~D', DTriples))),
			  tr(td(class=miniPTableProperty, 'Triples in RAM:' ),
			     td(class=miniPTableValue, format('~D', RTriples))),
			  tr(td(class=miniPTableProperty, 'RDF Sources:'),
			     td(class=miniPTableValue, a(href=list_of_sources, format('~D', N)))),
			  +lm
			 )
		   ).

disk_triples(D) :-
	findall(T,
		rdfpage(_, retr:triples, literal(T)),
		Numbers),
	sum(Numbers, D).

sum([], 0).
sum([N|Ns], Sum) :-
	sum(Ns, Rest),
	Sum is N + Rest.

lm(HTree) :-
	flag(latest_rdf_db_mod_time, LastModTime, LastModTime),
	flag(latest_rdf_db_mod_source, LastModSrc, LastModSrc),
	LastModTime \= 0,
	HTree = tr(td(class=miniPTableProperty, 'Latest Change:'),
		   td(class=miniPTableValue,
		      +word([truncate(18)], LastModSrc), ', ',
		      +reltime(LastModTime)
		     )
		  ).
lm([]).



gettingStarted(div(class=box, H)) :-
	Q=[],
	H = [h3(a(href='static/help-getting-started', 'Working with Ontologies')),
	     ul(li(+helplink(Q, 'what-is', 'What is an Ontology?')),
		li(+helplink(Q, 'finding', 'Finding an Ontology')),
		li(+helplink(Q, 'choosing', 'Choosing an Ontology')),
		li(+helplink(Q, 'using', 'Using an Ontology')),
		li(+helplink(Q, 'creating', 'Creating an Ontology')),
		li(+helplink(Q, 'publishing', 'Publishing an Ontology')),
		li(+helplink(Q, 'giving-feedback', 'Giving Useful Feedback'))
	       ),
	     h3(a(href='static/@@', 'About Ontaria')),
	     ul(li(a(href='static/project', 'The Project')),
		li(a(href='static/news', 'News')),
		li(a(href='static/faq', 'FAQ')),
		li(a(href='static/features', 'Features')),
		li(a(href='static/bugs', 'Bugs')),
		li(a(href='static/plans', 'Plans')),
		li(a(href='static/thanks', 'Thanks')),
		li(a(href='static/discuss', 'Discuss')),
		li(a(href='static/contact', 'Contact'))
	       )
	    
	    ].


	     
