/*
 * A HTML Pretty Printer for ProtoGen protocol definitions
 *
 * Copyright (c) 1994, 1995, 1996 Gunther Schadow.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
:- module(mkc_html, [html_title/1,html_headline/4,pp_msg/1,html_th/1,
		     html_td/1]).

:- style_check(+string).

:- ensure_loaded(supp-format).
:- ensure_loaded(pl-tools).

mkc :- 
        main_package(PM),
	assertz(PM:functional_area('',0,'NO CHAPTER')),
	( messages; true ),
	( segments; true ),
	( composites; true ),
        ( codes; true ),
	retract(PM:functional_area('',0,'NO CHAPTER')).

html_title(T) :-
	html_header,
	main_package(P),
	uppercase(P,UP),
	format("<HTML><HEADER><TITLE>~w ~w</TITLE></HEADER><BODY>", [UP, T]),
	format("<H1><A NAME=~'~w~'>~w ~w</A></H1>~n", [T, UP, T]).

html_header :-
	format("<EM>Created by "), banner, format("</EM><BR>~n"),
	format("<A HREF=~'messages.html~'>MESSAGES</A>~n"),
	format("<A HREF=~'segments.html~'>SEGMENTS</A>~n"),
	format("<A HREF=~'tables.html~'>TABLES</A>~n"),
	format("<A HREF=~'composites.html~'>COMPOSITES</A><HR>~n").
	
html_footer :-
	format("<HR><A HREF=~'messages.html~'>MESSAGES</A>~n"),
	format("<A HREF=~'segments.html~'>SEGMENTS</A>~n"),
	format("<A HREF=~'tables.html~'>TABLES</A>~n"),
	format("<A HREF=~'composites.html~'>COMPOSITES</A><BR>~n"),
	format("<EM>Created by "), banner, format("</EM>~n").

html_headline(L,H,N,R) :-
	format("<H~w><A NAME=~'~w~' HREF=~'#~w~'>~n", [L, N, R]),
	format("~w</A></H~w>~n", [H, L]).

chapter(1,'1',chapter).
chapter(2,'2',chapter).
chapter(3,'3',chapter).
chapter(4,'4',chapter).
chapter(5,'5',chapter).
chapter(6,'6',chapter).
chapter(7,'7',chapter).
chapter(8,'8',chapter).
chapter(-3,c,chapter).
chapter(0,'the rest','').

messages :-
	tell('messages.html'),
	html_title('MESSAGES'),
	forall(chapter(CHN, CH, TI),
        (
	    (
		(
		    functional_area(FA,CHN,FAN),
		    message_type(_,_,FA)
		) ->
		(
		    uppercase(FA,UFA),
		    uppercase(FAN,UFAN),
		    uppercase(TI,UTI),
		    uppercase(CH,UCH),

		    sformat(H2, "~w ~w ~w - ~w", [UTI, UCH, UFA, UFAN]),
		    html_headline(2, H2, UFA, 'MESSAGES'),

		    forall(message_type(M, TC, FA),
		    (
			uppercase(M, UM),
			uppercase(TC, UTC),	    
			(
			    sformat(H3, "MESSAGE ~w - ~w", [UM, UTC]),
			    html_headline(3,H3,UM,UFA)
			),
			forall(message(M, E, _, DefL),
			(
			    ( E \= ''->
				(
				    value(3, E, C, _),
				    uppercase(E, UE),
				    uppercase(C, UC),
				    
				    sformat(H4, "EVENT ~w - ~w", [UE, UC]),
				    html_headline(4, H4, UE, UM),
				    format("<P><PRE>~>"), 
				    pp_msg(DefL),
				    format("~<~n</PRE></P>~n")
				);
				format("<P><PRE>~>"),
				pp_msg(DefL),
				format("~<~n</PRE></P>~n")
			    )
			)),
			format("</P>~n")
		    ))
		);
		true
	    )
	)),
	html_footer,
	format("</BODY></HTML>"),
	told.

pp_msg([]).
pp_msg([H|T]) :-
	pp_msg1(H),
	pp_msg(T).

pp_msg1(rep(opt(X))) :- atom(X),
	uppercase(X, UX),
	format("~n{ [ <A HREF=~'segments.html#~w~'>~w</A> ] }", [X, UX]).
pp_msg1(opt(rep(X))) :- atom(X),
	uppercase(X, UX),
	format("~n[ { <A HREF=~'segments.html#~w~'>~w</A> } ]", [X, UX]).
pp_msg1(rep(X)) :- atom(X),
	uppercase(X, UX),
	format("~n{ <A HREF=~'segments.html#~w~'>~w</A> }", [X, UX]).
pp_msg1(opt(X)) :- atom(X),
	uppercase(X, UX),
	format("~n[ <A HREF=~'segments.html#~w~'>~w</A> ]", [X, UX]).
pp_msg1(rep(opt(grp(X)))) :- atom(X),
	uppercase(X, UX),
	format("~n{ [ <A HREF=~'segments.html#~w~'>~w</A> ] }", [X, UX]).
pp_msg1(opt(rep(grp(X)))) :- atom(X),
	uppercase(X, UX),
	format("~n[ { <A HREF=~'segments.html#~w~'>~w</A> } ]", [X, UX]).
pp_msg1(rep(grp(X))) :- atom(X),
	uppercase(X, UX),
	format("~n{ <A HREF=~'segments.html#~w~'>~w</A> }", [X, UX]).
pp_msg1(opt(grp(X))) :- atom(X),
	uppercase(X, UX),
	format("~n[ <A HREF=~'segments.html#~w~'>~w</A> ]", [X, UX]).
pp_msg1(X) :-
	X =.. [rep|A],
	format("~n{~>"),
	pp_msg(A),
	format("~<~n}").
pp_msg1(X) :-
	X =.. [opt|A],
	format("~n[~>"),
	pp_msg(A),
	format("~<~n]").
pp_msg1(X) :-
	X =.. [any|A],
	format("~nANY"),
	anyof(A).
pp_msg1(X) :-
	atom(X),
	uppercase(X, UX),
	format("~n<A HREF=~'segments.html#~w~'>~w</A>", [X, UX]).

anyof([]).
anyof(X) :-
	format(" OF"),
	anyof1(X).


anyof1([X]) :-
	uppercase(X, UX),
	format(" <A HREF=~'segments.html#~w~'>~w</A>", [X, UX]).
anyof1([X, Y]) :-
	uppercase(X, UX),
	uppercase(Y, UY),
	format(" <A HREF=~'segments.html#~w~'>~w</A>", [X, UX]),
	format(" AND <A HREF=~'segments.html#~w~'>~w</A>", [Y, UY]).
anyof1([X|R]) :-
	uppercase(X, UX),
	format(" <A HREF=~'segments.html#~w~'>~w</A>,", [X, UX]),
	anyof1(R).

html_th(L) :-
	format("<TR>"),
	html_th1(L).
html_th1([]) :-
	format("</TR>~n").
html_th1([H|T]) :-
	format("<TH>~w</TH>", [H]),
	html_th1(T).

html_tr(L) :-
	format("<TR>"),
	html_tr1(L).
html_tr1([]) :-
	format("</TR>~n").
html_tr1([H|T]) :-
	html_td(H),
	html_tr1(T).

html_td([D,R]) :-
	( number(D) ->
	    format("<TD ALIGN=RIGHT><A HREF=~'~w#~w~'>~w</A></TD>", [R,D,D]);
	    format("<TD><A HREF=~'~w#~w~'>~w</A></TD>", [R,D,D])
	).
html_td(D) :-
	( number(D) ->
	    format("<TD ALIGN=RIGHT>~w</TD>", [D]);
	    format("<TD>~w</TD>", [D])
	).

segments :-
	tell('segments.html'),
	html_title('SEGMENTS'),
	forall(chapter(CHN, CH, TI),
        (
	    (
		(
		    functional_area(FA,CHN,FAN),
		    segment(_,_,FA)
		) ->
		(
		    uppercase(FA,UFA),
		    uppercase(FAN,UFAN),
		    uppercase(TI,UTI),
		    uppercase(CH,UCH),

		    sformat(H2, "~w ~w ~w - ~w", [UTI, UCH, UFA, UFAN]),
		    html_headline(2, H2, UFA, 'SEGMENTS'),

		    forall(segment(SG, SC, FA),
		    (
			uppercase(SG, USG),
			uppercase(SC, USC),
			sformat(H3, "SEGMENT ~w - ~w", [USG, USC]),
			html_headline(3, H3, SG, UFA),
			format("<P><TABLE BORDER>~n"),
			html_th(['SEQ','NAME','TYPE','TABLE','R/O',
			         'REP','LEN','ITEM#']),
			forall(
			(
			    between(1,60, FNr),
			    field(SG, FNr, FC, DT, CDE, REQ, REP, LEN, DEN)
			),
			(
			    uppercase(FC,UFC),
			    uppercase(DT,UDT),
			    ( (
                                value(CDE,_,_,_) ) ->
				CDR = [CDE,'tables.html'];
				CDR = CDE
			    ),
			    ( (
                                ctyp_imp(DT,_) ) ->
				TYP = [UDT,'composites.html'];
				TYP = UDT
			    ),
			    html_tr([FNr,UFC,TYP,CDR,REQ,REP,LEN,DEN])
			)),
			format("</TABLE></P>~n")
		    ))
		);
		true
	    )
	)),
	html_footer,
	format("</BODY></HTML>"),
	told.

codes :-
	tell('tables.html'),
	html_title('TABLES'),
	forall(
	(
	    between(1,9999, I),
	    table(I, TN, TT)
	),
	(
	    \+ value(I,_,_,_);
	    uppercase(TN, UTN),
	    tabtype(TT,PTT),
	    sformat(H3, "~w TABLE ~w - ~w", [PTT, I, UTN]),
	    html_headline(3, H3, I, 'TABLES'),
	    format("<P><TABLE BORDER>~n"),
	    html_th(['VALUE','DESCRIPTION','ITEM#']),
	    forall(value(I, V, D, N),
	    (
		uppercase(V,UV),
		uppercase(D,UD),
		( N = '' ; true ),
		html_tr([UV,UD,N])
	    )),
	    format("</TABLE></P>~n")
	)),
	html_footer,
	format("</BODY></HTML>"),
	told.

composites :-
	tell('composites.html'),
	html_title('COMPOSITES'),
	forall(ctyp_imp(CT, DEF),
	(
	    uppercase(CT, UCT),
	    sformat(H3, "COMPOSITE TYPE ~w", [UCT]),
	    html_headline(3, H3, UCT, 'COMPOSITES'),
	    format("<P><TABLE BORDER>~n"),
	    html_th(['TYPE','NAME','TABLE']),
	    forall(member([DT,N], DEF),
	    (
		uppercase(N,UN),
		( DT = code(CDE) ->
		    TYP = 'ID',
		    ( (
                        value(CDE,_,_,_) ) ->
			CDR = [CDE,'tables.html'];
			CDR = CDE
		    );
		    uppercase(DT,TYP),
		    CDR = ''
		),
		html_tr([TYP,UN,CDR])
	    )),
	    format("</TABLE></P>~n")
	)),
	html_footer,
	format("</BODY></HTML>"),
	told.

tabtype(hl7_standard,'HL7').
tabtype(astm_standard,'ASTM').
tabtype(user_defined,'USER').
tabtype(derived,'DERIVED').
tabtype(X,UX) :- uppercase(X, UX).
