package it.unibo.cesop.demo;

import it.unibo.cesop.demo.model.Document;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import spark.ModelAndView;
import spark.Request;
import spark.Response;
import spark.template.jade.JadeTemplateEngine;
import java.net.MalformedURLException;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static spark.Spark.*;

public class Programma {

    public static void main(String[] args) throws MalformedURLException {

        //Set Java Spark server port
        port(8080);

        //Set static file folder
        staticFileLocation("/public/css");

        //URL Solr
        final SolrClient server = new HttpSolrClient.Builder("http://137.204.57.106:8983/solr/VisionTestCurriculumCollection").build();

        //Program param
        final boolean mode = args.length != 0 ? Boolean.FALSE : Boolean.TRUE;

        get("/", (request, response) -> {
            Map<String, String> model = new HashMap<>();
            model.put("message", "Hello Jade!");
            return new ModelAndView(model, "index"); // located in resources/templates directory
        }, new JadeTemplateEngine());

        post("/cerca", (Request request, Response response) -> {
            //Start time
            Instant start = Instant.now();
            Map<String, Object> model = new HashMap<>();

            String cerca = request.queryParams("cerca");
            model.put("message", cerca);

            //Set solr query
            final String wildCard= "http\\:\\/\\/www.cesop.it\\/ontologies\\/";
            SolrQuery query = new SolrQuery();
            String squery;
            if (mode) {
                squery = String.format("Competenze_ancestors_labels_t:%s OR Competenze_parent_labels_t:%s OR Competenze_label_t:%s", cerca, cerca, cerca);
            } else {
                squery = String.format("Competenze:%s", cerca);
            }
            query.setQuery(squery);
            //query.addFilterQuery("Cognome:"+ cerca);

            //Get results
            QueryResponse rsp = server.query(query);
            SolrDocumentList docs = rsp.getResults();
            List<Document> beans = rsp.getBeans(Document.class);

            Instant stop = Instant.now();
            Duration duration = Duration.between(start, stop);
            double seconds = (double)duration.getNano() / 1000000000.0;
            model.put("total", String.format("%.3f", seconds));
            model.put("nris", beans.size());
            model.put("documents", beans);
            model.put("mode", mode);

            return new ModelAndView(model, "cerca"); // located in resources/templates directory
        }, new JadeTemplateEngine());


    }
}

@@ -871,24 +871,143 @@
 lic/css%22);%0A%0A
+        //Program param%0A        final boolean mode = args%5B0%5D.equals(%22137.204.57.106%22) ? Boolean.TRUE : Boolean.FALSE;%0A%0A
         //UR
@@ -1082,30 +1082,27 @@
 %22http://
-137.204.57.106
+%22+args%5B0%5D+%22
 :8983/so
@@ -1151,111 +1151,8 @@
 );%0A%0A
-        //Program param%0A        final boolean mode = args.length != 0 ? Boolean.FALSE : Boolean.TRUE;%0A%0A
     
@@ -2182,16 +2182,62 @@
       %7D%0A
+            query.setRows(Integer.MAX_VALUE);%0A
         

fix modality