package org.bdgp.OpenHiCAMM.DB;

import java.io.File;

import org.bdgp.OpenHiCAMM.Dao;
import org.bdgp.OpenHiCAMM.Util;
import org.json.JSONException;
import org.json.JSONObject;
import org.micromanager.MMStudio;
import org.micromanager.acquisition.MMAcquisition;
import org.micromanager.utils.MMScriptException;

import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable
public class Acquisition {
    @DatabaseField(generatedId=true, canBeNull=false) private int id;
    @DatabaseField(canBeNull=false,dataType=DataType.LONG_STRING) private String name;
    @DatabaseField(canBeNull=false,dataType=DataType.LONG_STRING) private String prefix;
    @DatabaseField(canBeNull=false,dataType=DataType.LONG_STRING) private String directory;
    
    public Acquisition() {}
    public Acquisition (String name, String prefix, String directory) {
        this.name = name;
        this.prefix = prefix;
        this.directory = directory;
    }
    public int getId() {return this.id;}
    public String getName() {return this.name;}
    public String getPrefix() {return this.prefix;}
    public String getDirectory() { return this.directory; }

    public MMAcquisition getAcquisition(Dao<Acquisition> acquisitionDao) {
        return this.getAcquisition(acquisitionDao, true);
    }

    public MMAcquisition getAcquisition(Dao<Acquisition> acquisitionDao, boolean existing) {
        if (existing) {
            MMStudio mmstudio = MMStudio.getInstance();
            try {
                MMAcquisition acquisition = mmstudio.getAcquisitionWithName(this.name);
                JSONObject summaryMetadata = acquisition.getImageCache().getSummaryMetadata();
                if (summaryMetadata != null &&
                    summaryMetadata.has("Directory") && 
                    summaryMetadata.get("Directory").toString().equals(this.directory) &&
                    summaryMetadata.has("Prefix") &&
                    summaryMetadata.get("Prefix").toString().equals(this.prefix))
                {
                    return acquisition;
                }
            } 
            catch (JSONException e) {throw new RuntimeException(e);}
            catch (MMScriptException e) { 
                // this means there was no acquisition loaded with this name, that's OK. Let's try to 
                // open the acquisition next...
            } 

            try {
                File acquisitionPath = new File(this.directory, this.prefix);
                if (!acquisitionPath.exists()) throw new RuntimeException(String.format(
                        "Acquisition path %s does not exist!", acquisitionPath.getPath()));
                this.name = mmstudio.openAcquisitionData(acquisitionPath.getPath(), false, false);
                MMAcquisition acquisition = mmstudio.getAcquisitionWithName(this.name);
                if (acquisitionDao != null) {
                    acquisitionDao.update(this, "id");
                }
                return acquisition;
            } 
            catch (MMScriptException e1) {throw new RuntimeException(e1);}
        }
        else {
            try { 
                MMAcquisition acquisition = new MMAcquisition(this.name, this.directory, false, true, false); 
                acquisition.initialize();
                // update the prefix metadata
                JSONObject summaryMetadata = acquisition.getSummaryMetadata();
                if (summaryMetadata != null && summaryMetadata.has("Prefix")) {
                    this.prefix = summaryMetadata.get("Prefix").toString();
                    if (acquisitionDao != null) {
                        acquisitionDao.update(this, "id");
                    }
                }
                return acquisition;
            } 
            catch (JSONException e) {throw new RuntimeException(e);}
            catch (MMScriptException e) {throw new RuntimeException(e);}
        }
    }
    
    public MMAcquisition getAcquisition() {
        return this.getAcquisition(null);
    }
    
    public String toString() {
    	return String.format("%s(id=%d, name=%s, prefix=%s, directory=%s)",
    			this.getClass().getSimpleName(), 
    			this.id, 
    			Util.escape(this.name), 
    			Util.escape(this.prefix), 
    			Util.escape(this.directory));
    }
}

@@ -1532,24 +1532,66 @@
 Instance();%0A
+            synchronized (mmstudio) %7B%0A    
             
@@ -1604,32 +1604,36 @@
                 
+    
 MMAcquisition ac
@@ -1680,32 +1680,36 @@
 ame(this.name);%0A
+    
                 
@@ -1795,32 +1795,36 @@
                 
+   
+ 
 if (summaryMetad
@@ -1834,24 +1834,28 @@
  != null &&%0A
+    
             
@@ -1911,32 +1911,36 @@
                 
+    
 summaryMetadata.
@@ -2005,32 +2005,36 @@
                 
+ 
+   
 summaryMetadata.
@@ -2046,24 +2046,28 @@
 Prefix%22) &&%0A
+    
             
@@ -2136,16 +2136,20 @@
 refix))%0A
+    
         
@@ -2170,32 +2170,36 @@
                 
+    
 return acquisiti
@@ -2210,34 +2210,40 @@
                 
-%7D%0A
+    %7D%0A  
             %7D %0A 
@@ -2230,35 +2230,41 @@
 %7D%0A              
+  
 %7D %0A
+    
             catc
@@ -2320,32 +2320,36 @@
 );%7D%0A            
+    
 catch (MMScriptE
@@ -2379,16 +2379,20 @@
         
+ 
+   
 // this 
@@ -2486,16 +2486,20 @@
         
+    
 // open 
@@ -2538,12 +2538,20 @@
     
+   
+ 
 %7D %0A%0A
+    
     
@@ -2556,32 +2556,36 @@
           try %7B%0A
+    
                 
@@ -2658,24 +2658,28 @@
             
+  
+  
 if (!acquisi
@@ -2763,16 +2763,20 @@
         
+    
 %22Acquisi
@@ -2831,24 +2831,28 @@
 etPath()));%0A
+    
             
@@ -2946,32 +2946,36 @@
                 
+    
 MMAcquisition ac
@@ -3038,32 +3038,36 @@
                 
+    
 if (acquisitionD
@@ -3092,32 +3092,36 @@
                 
+   
+ 
 acquisitionDao.u
@@ -3147,34 +3147,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -3205,35 +3205,43 @@
 on;%0A            
+ 
-%7D %0A
+   %7D %0A    
             catc
@@ -3295,24 +3295,38 @@
 ption(e1);%7D%0A
+            %7D%0A
         %7D%0A  

fix synchronization error with acquisition manager