package org.datacite.conres.view;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.datacite.conres.model.Model;
import org.datacite.conres.model.Pair;

import java.util.List;
import java.util.Arrays;

public class JsonLdRepresentation {
    public static JSONObject writeJSON(Model mr) {
        try {
            JSONArray authors = new JSONArray();
            for (String  p : mr.getCreators()) {
                if (p.contains(",")) {
                    List<String> pList = Arrays.asList(p.split(", ", 2));
                    authors.put(new JSONObject()
                        .put("@type", "Person")
                        .put("familyName", pList.get(0))
                        .put("givenName", pList.get(1)));
                } else {
                    authors.put(new JSONObject()
                        .put("@type", "Organisation")
                        .put("name", p));
                }
            }

            JSONObject issued = new JSONObject();
            Integer year = Integer.valueOf(mr.getPublicationYear());
            issued.put("date-parts", new JSONArray().put(new JSONArray().put(year)));

            String type = "CreativeWork";
            for (Pair p : mr.getResourceTypes()) {
                if (p.getKey().toUpperCase().equals("DATASET")) {
                    type = "Dataset";
                    break;
                }
                else if (p.getKey().toUpperCase().equals("TEXT")) {
                    type = "Article";
                    break;
                }
                else if (p.getKey().toUpperCase().equals("SOFTWARE")) {
                    type = "SoftwareApplication";
                    break;
                }
            }

            return new JSONObject()
                .put("@context", "http://schema.org")
                .put("@type", type)
                .put("@id", "https://doi.org/" + mr.getDoi())
                .put("name", mr.getTitles().get(0).getValue())
                .put("author", authors)
                .put("publisher", mr.getPublisher())
                .put("datePublished", mr.getPublicationYear())
                .put("version", mr.getVersion());
        } catch (JSONException je) {
            return null;
        }
    }
}

@@ -938,20 +938,13 @@
 %22, %22
-Organisation
+Agent
 %22)%0A 
@@ -1021,214 +1021,8 @@
  %7D%0A%0A
-            JSONObject issued = new JSONObject();%0A            Integer year = Integer.valueOf(mr.getPublicationYear());%0A            issued.put(%22date-parts%22, new JSONArray().put(new JSONArray().put(year)));%0A%0A
     
@@ -1569,32 +1569,348 @@
               %7D%0A
+                else if (p.getKey().toUpperCase().equals(%22IMAGE%22)) %7B%0A                    type = %22ImageObject%22;%0A                    break;%0A                %7D%0A                else if (p.getKey().toUpperCase().equals(%22COLLECTION%22)) %7B%0A                    type = %22Collection%22;%0A                    break;%0A                %7D%0A
             %7D%0A%0A 

use generic agent if author has no clear personal name