package org.texttechnologylab.project.gruppe_05_1;


import org.texttechnologylab.project.gruppe_05_1.database.*;
import org.texttechnologylab.project.gruppe_05_1.nlp.NlpUtils;
import org.texttechnologylab.project.gruppe_05_1.nlp.XmiExtractor;
import org.texttechnologylab.project.gruppe_05_1.rest.RESTHandler;
import org.texttechnologylab.project.gruppe_05_1.util.Logger;
import org.texttechnologylab.project.gruppe_05_1.util.PPRUtils;
import org.texttechnologylab.project.gruppe_05_1.xml.FileObjectFactory;
import java.util.Arrays;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

public class Main {
    public static boolean UPLOAD_MEMBER_PHOTOS;
    public static boolean FORCE_UPLOAD_MEMBERS;
    public static boolean FORCE_UPLOAD_SPEECHES;
    private static final FileObjectFactory xmlFactory = FileObjectFactory.getFactory();
    private static final MongoObjectFactory mongoFactory = MongoObjectFactory.getFactory();

    public static void main(String[] args) throws Exception {
        UPLOAD_MEMBER_PHOTOS = Arrays.asList(args).contains("uploadMemberPhotos");
        FORCE_UPLOAD_MEMBERS = Arrays.asList(args).contains("forceUploadMembers");
        FORCE_UPLOAD_SPEECHES = Arrays.asList(args).contains("forceUploadSpeeches");

        System.out.println("Starting Multimodal Parliament Explorer...");
        System.out.println("--------------------------");
        System.out.println("Arguments:");
        System.out.println(" - Upload the Member Photos to the DB: " + UPLOAD_MEMBER_PHOTOS);
        System.out.println(" - Force Upload Members:               " + FORCE_UPLOAD_MEMBERS);
        System.out.println(" - Force Upload Speeches:              " + FORCE_UPLOAD_SPEECHES);
        System.out.println("--------------------------");

        //TEST
        MongoDBHandler mongoDBHandler = new MongoDBHandler();

        SpeechIndexFactoryImpl speechIndexFactory = new SpeechIndexFactoryImpl();
        if ((mongoDBHandler.getDatabase().getCollection(MongoPprUtils.SPEECH_COLLECTION_NAME).countDocuments() != 0) && !FORCE_UPLOAD_SPEECHES) {
            Logger.info("Skipping Speech parsing and DB insertion as they are already present...");
        } else {
            Logger.warn("Deleting all speeches from DB if existing...");
            mongoDBHandler.deleteSpeechRelatedDocuments();

            Logger.pink("Parsing XML and inserting data into DB...");
            SpeechIndex speechIndex = speechIndexFactory
                    .parseLegislativePeriods(TRUE)
                    .builder()
                    .parseSessions()
                    .filterForCurrentMembers(FALSE)
                    .build();

            //speechIndex.printInfo();

            Logger.info("Data retrieved from DB:");
            Logger.info("SPEECHCOUNT: " + speechIndex.getSpeeches().size());
            Logger.info("SESSIONCOUNT: " + speechIndex.getSessions().size());
            Logger.info("AGENDAITEMCOUNT: " + speechIndex.getAgendaItems().size());

            mongoDBHandler.deleteSpeechRelatedDocuments(); // Clear speeches, sessions, agendas (history)

            Logger.pink("Adding Sessions to DB...");
            mongoDBHandler.insertSessions(speechIndex.getSessions());

            Logger.pink("Adding Agenda Items to DB...");
            mongoDBHandler.insertAgendaItems(speechIndex.getAgendaItems());

            Logger.pink("Adding Speeches to DB...");
            mongoDBHandler.insertSpeeches(speechIndex.getSpeeches());

            // only upload member photos if database was empty by default, not when speeches are force-overwritten
            if (!FORCE_UPLOAD_SPEECHES) {
                Logger.pink("Uploading Member Photos to DB...");
                mongoDBHandler.uploadMemberPhotos();
            }
        }

        // Alle Informationen (Parlamentarier, Reden, Kommentare etc.) lesen und in die Mongo-DB einfügen, falls diese noch nicht vorhanden sind.
        PPRUtils.parlamentExplorerInit(xmlFactory, mongoFactory);

        //das muss auf jeden fall noch angepasst werden xd
        if(mongoDBHandler.checkAnalysisResultsField() < 21518) {
            try {
                long startTime = System.currentTimeMillis();

                XmiExtractor extractor = new XmiExtractor();
                extractor.extractAndUploadXmiData();

                long endTime = System.currentTimeMillis();
                Logger.pink("Execution time: " + (endTime - startTime) + " ms");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (UPLOAD_MEMBER_PHOTOS) {
            Logger.pink("Uploading Member Photos to DB...");
            mongoDBHandler.uploadMemberPhotos();
        }
        mongoDBHandler.close();
        try {
            NlpUtils.runRemoteDriver();
        } catch (Exception e) {
            Logger.error("Error while running NLP remote driver");
            Logger.error(e.getMessage());
        }
        RESTHandler restHandler = new RESTHandler();
        restHandler.startJavalin();

    }
}

