package vicnode.daris.plugin.services;

import arc.mf.plugin.PluginService;
import arc.mf.plugin.dtype.CiteableIdType;
import arc.xml.XmlDoc.Element;
import arc.xml.XmlDoc;
import arc.xml.XmlDocMaker;
import arc.xml.XmlWriter;

public class SvcLifePoolMetaExtract extends PluginService {

    public static final String SERVICE_NAME = "vicnode.daris.lifepool.metadata.extract";

    public static final String SERVICE_DESCRIPTION = "Specialised service for the LifePool project to extract specific DICOM meta-data tags.";

    private Interface _defn;

    public SvcLifePoolMetaExtract () {
        _defn = new Interface();
        _defn.add(new Interface.Element("id", CiteableIdType.DEFAULT,
                "The asset ID of the dicom/series asset. ", 1, 1));
    }

    @Override
    public Access access() {
        return ACCESS_MODIFY;
    }

    @Override
    public Interface definition() {
        return _defn;
    }

    @Override
    public String description() {
        return SERVICE_DESCRIPTION;
    }

    @Override
    public void execute(Element args, Inputs inputs, Outputs outputs,
            XmlWriter w) throws Throwable {
        String id = args.value("id");
        XmlDocMaker dm = new XmlDocMaker("args");
        dm.add("id", id);
        dm.add("if-exists", "replace");
        dm.add("tag", "00080008");   // Image Type
        dm.add("tag", "00080050");   // Accession Number
        dm.add("tag", "00080060");   // Modality
        dm.add("tag", "00080068");   // Presentation Intent Type
        dm.add("tag", "00080070");   // Instrument Manufacturer Name
        dm.add("tag", "00080080");   // Institution
        dm.add("tag", "0008103E");   // Series description
        dm.add("tag", "00081090");   // Instrument Model
        dm.add("tag", "00181400");   // Acquisition Device Processing Description
        dm.add("tag", "00185101");   // View position
        dm.add("tag", "00200062");   // Image Laterality
        XmlDoc.Element r = executor().execute("dicom.metadata.populate", dm.root());
        w.addAll(r.elements());
}
   

    @Override
    public String name() {
        return SERVICE_NAME;
    }

}

@@ -112,16 +112,56 @@
 IdType;%0A
+import arc.mf.plugin.dtype.IntegerType;%0A
 import a
@@ -183,16 +183,16 @@
 lement;%0A
-
 import a
@@ -796,22 +796,197 @@
 1, 1));%0A
+        _defn.add(new Interface.Element(%22idx%22, IntegerType.POSITIVE,%0A                %22This specifies the idx'th file in the DICOM series archive. Defaults to zero.%22, 0, 1));%0A 
     %7D%0A
-
 %0A    @Ov
@@ -1390,24 +1390,67 @@
 alue(%22id%22);%0A
+        int idx = args.intValue(%22idx%22, 0);%0A
         XmlD
@@ -1513,16 +1513,43 @@
 %22, id);%0A
+        dm.add(%22idx%22,idx);%0A
         
@@ -1573,14 +1573,12 @@
 %22, %22
-replac
+merg
 e%22);

add arg idx