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

import org.texttechnologylab.project.gruppe_05_1.domain.mdb.*;
import org.texttechnologylab.project.gruppe_05_1.util.GeneralUtils;
import org.texttechnologylab.project.gruppe_05_1.util.Logger;
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 Wahlperiode_File_Impl extends Wahlperiode implements XmlOperations {
    @Override
    public Wahlperiode fromXmlNode(Node node) {

        Node current = node.getFirstChild();
        Node child;
        Wahlperiode wp = new Wahlperiode_File_Impl();
        while (null != current) {
            child = current.getFirstChild();
            switch (current.getNodeName()) {
                case "WP":
                    wp.setWp(null == child ? null : GeneralUtils.parseInt(child.getNodeValue()));
                    break;

                case "MDBWP_VON":
                    wp.setMdbWpVon(null == child ? null : GeneralUtils.parseDate(child.getNodeValue()));
                    break;

                case "MDBWP_BIS":
                    wp.setMdbWpBis(null == child ? null : GeneralUtils.parseDate(child.getNodeValue()));
                    break;

                case "WKR_NUMMER":
                    wp.setWknNr(null == child ? null : child.getNodeValue());
                    break;

                case "WKR_NAME":
                    wp.setWkrName(null == child ? null : child.getNodeValue());
                    break;

                case "WKR_LAND":
                    wp.setWkrLand(null == child ? null : child.getNodeValue());
                    break;

                case "LISTE":
                    wp.setListe(null == child ? null : child.getNodeValue());
                    break;

                case "MANDATSART":
                    if (null == child) {
                        wp.setMandatsart(Mandatsart.NA);
                    } else {
                        wp.setMandatsart(Mandatsart.byText(child.getNodeValue()));
                    }
                    break;

                case "INSTITUTIONEN":
                    // Node institutionenNode = XmlUtils.getFirstChildByName(node, "INSTITUTIONEN");
                    List<Node> institutionNodeList = XmlUtils.getChildrenByName(current, "INSTITUTION");
                    List<Institution> institutionList = new ArrayList<>();
                    for (Node institutionNode : institutionNodeList) {
                        Institution inst = factory.createInstitution(institutionNode);
                        institutionList.add(inst);
                    }
                    wp.setInstitutionen(institutionList);
                    break;

                case "#text":
                    break;

                default:
                    Logger.error("Node name does not match anything known in Wahlperiode: " + current.getNodeName());
            }

            current = current.getNextSibling();
        }

        return wp;
    }

}
