package org.texttechnologylab.project.gruppe_05_1.database;


import org.texttechnologylab.project.gruppe_05_1.xml.speeches.Interfaces.*;
import org.texttechnologylab.project.gruppe_05_1.xml.speeches.SpeechParser;

import java.util.List;
import java.util.stream.Collectors;

public class SpeechIndexFactoryImpl implements SpeechIndexFactory {
    private SpeechParser speechParser;
    private List<Session> sessions;
    private List<AgendaItem> agendaItems;
    private List<Speech> speeches;
    private List<MemberOfParliament> members;


    @Override
    public SpeechIndexFactoryImpl parseLegislativePeriods(Boolean parseLegislativePeriods) {
        try {this.speechParser.setParseLegislativePeriods(parseLegislativePeriods);} catch (NullPointerException ignored) {}
        return this;
    }

    public SpeechIndexFactoryImpl builder() {
       this.speechParser = new SpeechParser();
       return this;
    }

    public SpeechIndexFactoryImpl parseSessions() {
        this.sessions = this.speechParser.parseAllSessions();
        this.speeches = this.speechParser.getSpeeches();
        this.agendaItems = this.speechParser.getAgendaItems();
        return this;
    }


    @Override
    public SpeechIndex build() {
        return new SpeechIndex(sessions, speeches, agendaItems);
    }


    public SpeechIndexFactoryImpl filterForCurrentMembers(boolean filterForCurrentMembers) {
        if (!filterForCurrentMembers) return this;
        this.members = this.members.stream().filter(MemberOfParliament::isCurrentMember).collect(Collectors.toList());
        return this;
    }
}
