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

import org.texttechnologylab.project.gruppe_05_1.domain.mdb.Mdb;
import org.texttechnologylab.project.gruppe_05_1.domain.mdb.MdbName;
import org.texttechnologylab.project.gruppe_05_1.domain.mdb.Wahlperiode;
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.util.ArrayList;
import java.util.List;

public class Mdb_File_Impl extends Mdb implements XmlOperations {
    @Override
    public Mdb fromXmlNode(Node node) {
        Mdb mdb = new Mdb_File_Impl();
        Node idNode = XmlUtils.getFirstChildByName(node, "ID");
        assert idNode != null;
        mdb.setId(idNode.getFirstChild().getNodeValue());

        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);
        }
        mdb.setNamen(mdbNameList);

        Node bioNode = XmlUtils.getFirstChildByName(node, "BIOGRAFISCHE_ANGABEN");
        mdb.setBio(factory.createBiografischeAngaben(bioNode));


        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);
        }
        mdb.setWahlperioden(wpList);

        return mdb;
    }
}
