package org.texttechnologylab.project.gruppe_05_1.database.domainimpl.mdb;

import org.bson.Document;
import org.texttechnologylab.project.gruppe_05_1.database.MongoDBHandler;
import org.texttechnologylab.project.gruppe_05_1.database.MongoOperations;
import org.texttechnologylab.project.gruppe_05_1.domain.mdb.Institution;
import org.texttechnologylab.project.gruppe_05_1.domain.mdb.Wahlperiode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Wahlperiode_Mongo_Impl extends Wahlperiode implements MongoOperations<Wahlperiode> {
    @Override
    public Document createEntity(Wahlperiode entity) {

        Map<String, Object> fields = new HashMap<>();
        fields.put("wp", entity.getWp());
        fields.put("mdbWpVon", entity.getMdbWpVon());
        fields.put("mdbWpBis", entity.getMdbWpBis());
        fields.put("wknNr", entity.getWknNr());
        fields.put("wkrName", entity.getWkrName());
        fields.put("wkrLand", entity.getWkrLand());
        fields.put("liste", entity.getListe());
        fields.put("mandatsart", entity.getMandatsart().name());

        List<Institution>  institutionen = entity.getInstitutionen();
        List<Document> instDocs = new ArrayList<>();
        for (Institution inst : institutionen) {
            instDocs.add(factory.createInstitution(inst));
        }
        fields.put("institutionen", instDocs);

        return MongoDBHandler.createDocument(false, fields);
    }

    @Override
    public List<Document> createList(List<Wahlperiode> list) {
        List<Document> result = new ArrayList<>();
        for (Wahlperiode wp : list) {
            result.add(createEntity(wp));
        }
        return result;
    }
}
