:- encoding(utf8).
:- dynamic reported_symptom/1.
:- dynamic disease_score/2.
:- include('main.pl').

contains(Element, [Element|_]) :- !.
contains(Element, [_|Tail]) :- contains(Element, Tail).


clear_reported_symptoms :-
    retractall(reported_symptom(_)).

add_reported_symptom(Symptom) :-
    assertz(reported_symptom(Symptom)).

list_reported_symptoms :-
    findall(Symptom, reported_symptom(Symptom), Symptoms),
    (   Symptoms = []
        -> writeln('No symptoms')
        ;   writeln('Symptoms:'),
            maplist(writeln, Symptoms)
    ).

clear_disease_scores :-
    retractall(disease_score(_, _)).

disease_score_add(Disease, Inc) :-
       disease_score(Score, Disease),
        NewScore is Score + Inc,
        retract(disease_score(Score, Disease)),
        assertz(disease_score(NewScore, Disease))
    ;   assertz(disease_score(1, Disease)).

possible_disease([],_).
possible_disease(Disease) :-
    symptom(Disease, Symptoms),
    reported_symptom(Symptom),
    contains(Symptom, Symptoms),
    disease_score_add(Disease, 1).


disease_check(Disease, CheckList):-
    check(Disease, CheckList).

all_symptoms_present([]).
all_symptoms_present([Symptom|OtherSymptom]) :-
    reported_symptom(Symptom),
    writeln(Symptom),
    all_symptoms_present(OtherSymptom).


diagnose(DiseaseList) :-
    clear_disease_scores,
    findall(Disease, possible_disease(Disease), _),
    findall([Disease,Score], disease_score(Score, Disease), DiseaseScores) ,
    DiseaseList = DiseaseScores.
