:- initialization(demo).

% % % % % % % % % %
% DEMO MENU LOGIC %
% % % % % % % % % %

demo :-
    clear,
    header,
    menu.

menu :-
    nl,
    options,
    write('DEMO: Enter your choice: '),
    read(Choice),
    choice(Choice).

header :-
    writeln('* * * * * * * * * * * * * * * * * * * * * * * * *'),
    writeln('*               GHENT PROLOG DEMO               *'),
    writeln('* * * * * * * * * * * * * * * * * * * * * * * * *').

options :-
    writeln('-------------------------------------------------'),
    writeln('INPUT DESCRIPTION              CATEGORY SOURCE'),
    writeln('-------------------------------------------------'),
    writeln('(1)   A password prompt        basics   password.pl'),
    writeln('(2)   Add i in [a..b]          maths    summer.pl'),
    writeln('(3)   A translator             meta     mib_voorbeelden.pl'),
    writeln('(4)   A tale of a ceremony     cont.    ceremony.pl'),
    writeln('(5)   Maths with Shift/Reset   cont.    continuations.pl'),
%    writeln('(6)   Alternative lists        meta     my_list.pl'),
    nl,
    writeln('(interpreter) to enter the interpreter'),
    writeln('(exit)        to exit the demo'),
    nl.

choice(1) :- !, run_login_example, menu.
choice(2) :- !, run_summer_example, menu.
choice(3) :- !, run_mib_voorbeelden_example, continue, menu.
choice(4) :- !, run_ceremony_example, continue, menu.
choice(5) :- !, run_continuations_example, continue, menu.
%choice(6) :- !, run_my_list_example, continue, menu.
choice(interpreter) :- !, run_interpreter, continue, menu.
choice(exit) :- !, writeln('Exiting...').
choice(_) :- write('DEMO: Invalid choice, please try again: '), read(Choice), choice(Choice).

% % % % % % % % % %
% DEMO REPOSITORY %
% % % % % % % % % %

run_login_example :-
    say('Loading login example...'),
    consult('examples/basics/repeat.pl'),
    consult('examples/basics/password.pl'),
    say('Showing login/0'),
    say('You might try logging in with tibo/noaccess and john/john1.'),
    login, login_example_loop, !,
    say('Login example finished.').

login_example_loop :-
    yes_or_no('Do you want to try again?', (login, login_example_loop), true).

run_summer_example :-
    say('Loading summer example...'),
    consult('examples/basics/summer.pl'),
    say('Showing my_sum/1'),
    say('You might try summing 1 to 9.'),
    my_sum, summer_example_loop, !,
    say('Summer example finished.').

summer_example_loop :-
    yes_or_no('Do you want to try again?', (my_sum, summer_example_loop), true).

run_mib_voorbeelden_example :-
    say('Loading mib_voorbeelden example...'),
    consult('examples/meta/mib_voorbeelden.pl'), clear,
    say('Showing example1/0'), example1, next((
    say('Showing example2/0'), example2, next((
    say('Showing example3/0'), example3)))),
    say('mib_voorbeelden example finished.').

run_ceremony_example :-
    say('Loading ceremony example...'),
    consult('examples/meta/ceremony.pl'), clear,
    say('Showing ceremony_main/0'),
    ceremony_main,
    say('Ceremony example finished.').

run_continuations_example :-
    say('Loading continuations example...'),
    consult('examples/meta/continuations.pl'), clear,
    say('Showing continuations_main/0'),
    continuations_main,
    say('Continuations example finished.').

run_my_list_example :-
    say('Loading my_list example...'),
    consult('examples/meta/my_list.pl'),
    say('Showing my_member/2'),
    showcase(my_member(X, a(b(c))), 'X', X),
    continue,
    say('Showing my_length/2'),
    showcase(my_length(a(b(c)), Length), 'Length', Length),
    say('my_list example finished.').

run_interpreter :-
    say('Loading interpreter...'),
    consult('examples/meta/ground.pl'),
    consult('examples/meta/interpreter.pl'),
    say('Showing interpreter/0'),
    say('Have fun!'),
    interpreter,
    say('Interpreter finished.').

% % % % % % % % % % %
% HELPER PREDICATES %
% % % % % % % % % % %

yes_or_no(Question, IfYes, IfNo) :-
    write(Question), write(' (y/n): '),
    read(Answer),
    ( ( was_yes(Answer), !, call(IfYes)) ; ( was_no(Answer), !, call(IfNo))).

was_yes(y).
was_yes('Y').
was_yes(yes).
was_yes('Yes').
was_yes('YES').

was_no(n).
was_no('N').
was_no(no).
was_no('No').
was_no('NO').

say(Message) :-
    write('DEMO: '),
    writeln(Message).

showcase(Goal, VarName, Var) :-
    say(Goal),
    call(Goal),
    say(solution(VarName, Var)).

showcase(Goal) :-
    Goal =.. List,
    say(List),
    call(Goal).

next(Next) :- yes_or_no('See next?', call(Next), true).

continue :-
    write('Write anything to continue... '),
    read(_).

clear :- clear(30).

clear(0) :- !.
clear(N) :-
    nl,
    M is N - 1,
    clear(M).
