package ca.firstvoices.nuxeo.enrichers;

import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.nuxeo.ecm.core.api.CoreInstance;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelList;
import org.nuxeo.ecm.core.api.IdRef;
import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
import org.nuxeo.ecm.core.io.registry.reflect.Setup;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Setup(mode = SINGLETON, priority = REFERENCE)
public class AncestryEnricher extends AbstractJsonEnricher<DocumentModel> {

    public static final String NAME = "ancestry";

    public AncestryEnricher() {
        super(NAME);
    }

    // Method that will be called when the enricher is asked for
    @Override
    public void write(JsonGenerator jg, DocumentModel doc) throws IOException {
        // We use the Jackson library to generate Json
        ObjectNode wordJsonObject = constructAncestryJSON(doc);
        jg.writeFieldName(NAME);
        jg.writeObject(wordJsonObject);
    }

    protected ObjectNode constructAncestryJSON(DocumentModel doc) {

        final String docId = doc.getId();
        ObjectMapper mapper = new ObjectMapper();

        CoreInstance.doPrivileged(doc.getCoreSession(), session -> {
            // JSON object to be returned
            ObjectNode jsonObj = mapper.createObjectNode();
            /*
             * Process fvancestry values
             */

            // Process Language Family

            DocumentModel resolvedDoc = doc;
            String languageFamilyId = (String) doc.getProperty("fvancestry", "family");

            if (StringUtils.isNotEmpty(languageFamilyId)) {
                resolvedDoc = resolveTargetDoc(languageFamilyId, doc.isProxy(), session);

                ObjectNode languageFamilyObj = mapper.createObjectNode();
                languageFamilyObj.put("uid", languageFamilyId);
                languageFamilyObj.put("dc:title", resolvedDoc.getTitle());
                jsonObj.set("family", languageFamilyObj);
            }

            // Process Language

            String languageId = (String) doc.getProperty("fvancestry", "language");

            if (StringUtils.isNotEmpty(languageId)) {
                resolvedDoc = resolveTargetDoc(languageId, doc.isProxy(), session);
                ObjectNode languageObj = mapper.createObjectNode();
                languageObj.put("uid", languageId);
                languageObj.put("dc:title", resolvedDoc.getTitle());
                jsonObj.set("language", languageObj);
            }

            // Process Dialect

            String dialectId = (String) doc.getProperty("fvancestry", "dialect");

            if (StringUtils.isNotEmpty(dialectId)) {
                resolvedDoc = resolveTargetDoc(dialectId, doc.isProxy(), session);
                ObjectNode dialectDoc = mapper.createObjectNode();
                dialectDoc.put("uid", dialectId);
                dialectDoc.put("dc:title", resolvedDoc.getTitle());
                dialectDoc.put("path", resolvedDoc.getPathAsString());

                dialectDoc.put("fvdialect:country", (String) resolvedDoc.getPropertyValue("fvdialect:country"));
                dialectDoc.put("fvdialect:region", (String) resolvedDoc.getPropertyValue("fvdialect:region"));

                jsonObj.set("dialect", dialectDoc);

            }
            return jsonObj;
        });
        return mapper.createObjectNode();
    }

    protected DocumentModel resolveTargetDoc(String docIdRef, boolean isProxy, CoreSession session) {
        DocumentModel resolvedDoc = session.getDocument(new IdRef(docIdRef));
        if (isProxy) {
            DocumentModelList proxies = session.getProxies(new IdRef(docIdRef), null);

            if (proxies.size() > 0) {
                resolvedDoc = proxies.get(0);
            }
        }
        return resolvedDoc;
    }
}
@@ -1533,16 +1533,23 @@
         
+return 
 CoreInst
@@ -3745,50 +3745,8 @@
 %7D);%0A
-        return mapper.createObjectNode();%0A
     

fix return in enricher