package org.texttechnologylab.project.gruppe_05_1.xml.speaker;

import org.texttechnologylab.project.gruppe_05_1.domain.mdb.*;
import org.texttechnologylab.project.gruppe_05_1.domain.speaker.Membership;
import org.texttechnologylab.project.gruppe_05_1.domain.speaker.Speaker;
import org.texttechnologylab.project.gruppe_05_1.util.PPRUtils;
import org.texttechnologylab.project.gruppe_05_1.util.XmlUtils;
import org.texttechnologylab.project.gruppe_05_1.xml.XmlOperations;
import org.w3c.dom.Node;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class Speaker_File_Impl extends Speaker implements XmlOperations {
    @Override
    public Speaker fromXmlNode(Node node) {
        Speaker speaker = new Speaker_File_Impl();

        // ID
        Node idNode = XmlUtils.getFirstChildByName(node, "ID");
        assert idNode != null;
        speaker.setId(idNode.getFirstChild().getNodeValue());

        // Name: alle Namen lesen, nur den letzten berücksichtigen
        Node namenNode = XmlUtils.getFirstChildByName(node, "NAMEN");
        assert namenNode != null;
        List<Node> nameNodeList = XmlUtils.getChildrenByName(namenNode, "NAME");
        List<MdbName> mdbNameList = new ArrayList<>();
        for (Node nameNode : nameNodeList) {
            MdbName mdbName = factory.createMdbName(nameNode);
            mdbNameList.add(mdbName);
        }
        mdbNameList.sort((n1, n2 )-> n2.getHistorieVon().compareTo(n1.getHistorieVon()));   // Descending order
        MdbName newestMdbName = mdbNameList.get(0); // TODO
        speaker.setName(newestMdbName.getNachname());
        speaker.setFirstName(newestMdbName.getVorname());
        speaker.setAkademischertitel(newestMdbName.getAkadTitel());
        speaker.setTitle(newestMdbName.getAndereTitel());

        // Biografische Daten:
        Node bioNode = XmlUtils.getFirstChildByName(node, "BIOGRAFISCHE_ANGABEN");
        BiografischeAngaben bio = factory.createBiografischeAngaben(bioNode);
        if (bio.getGeburtsland() == null || bio.getGeburtsland().isBlank()) {
            speaker.setGeburtsort(bio.getGeburtsort());
        } else {
            speaker.setGeburtsort(bio.getGeburtsort() + " (" + bio.getGeburtsland() + ")");
        }
        speaker.setGeburtsdatum(bio.getGeburtsdatum());
        speaker.setSterbedatum(bio.getSterbedatum());
        speaker.setGeschlecht(bio.getGender());
        speaker.setBeruf(bio.getBeruf());
        speaker.setFamilienstand(bio.getFamilienstand());
        speaker.setReligion(bio.getReligion());
        speaker.setVita(bio.getVitaKurz());

        // Da wir nach Partei sortieren und vergleichen, darf das Feld nicht null sein...
        speaker.setParty(bio.getParteiKuerzel() == null ? PPRUtils.PARTEILOS_KUERZEL : bio.getParteiKuerzel());

        // Memberships
        Node wpenNode = XmlUtils.getFirstChildByName(node, "WAHLPERIODEN");
        assert wpenNode != null;
        List<Node> wpNodeList = XmlUtils.getChildrenByName(wpenNode, "WAHLPERIODE");
        List<Wahlperiode> wpList = new ArrayList<>();
        for (Node wpNode : wpNodeList) {
            Wahlperiode wp = factory.createWahlperiode(wpNode);
            wpList.add(wp);
        }
        wpList.sort((wp1, wp2 )-> wp2.getMdbWpVon().compareTo(wp1.getMdbWpVon()));  // Descending order
        List<Membership> memberships = new ArrayList<>();
        for (Wahlperiode wp : wpList) {
            LocalDate mdbwpVon = wp.getMdbWpVon();
            LocalDate mdbwpBis = wp.getMdbWpBis();

            for (Institution inst : wp.getInstitutionen()) {
                Membership m = new Membership();
                m.setBegin(inst.getMdbinsVon() == null? mdbwpVon : inst.getMdbinsVon());
                m.setEnd(inst.getFktinsBis() == null? mdbwpBis : inst.getFktinsBis());
                m.setRole(inst.getFktLang());
                m.setLabel(inst.getInsLang());
                m.setWp(wp.getWp());

                memberships.add(m);
            }

        }
        speaker.setMemberships(memberships);

        return speaker;
    }
}
