/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.micromanager.acquisition;

import java.awt.Color;
import java.util.ArrayList;
import mmcorej.AcquisitionSettings;
import mmcorej.CMMCore;
import mmcorej.Metadata;
import org.micromanager.api.ScriptInterface;
import org.micromanager.image5d.Image5D;
import org.micromanager.utils.ChannelSpec;
import org.micromanager.utils.MMScriptException;
import org.micromanager.utils.ReportingUtils;

/**
 *
 * @author arthur
 */
public abstract class AcquisitionDisplay extends Thread {

   protected final CMMCore core_;
   protected int imgCount_;
   protected   ArrayList<Image5D> i5dVector_;
   private final ScriptInterface gui_;
   AcquisitionSettings acqSettings_;

   AcquisitionDisplay(ScriptInterface gui, CMMCore core, AcquisitionSettings acqSettings, ArrayList<ChannelSpec> channels) {
      gui_ = gui;
      core_ = core;
      i5dVector_ = new ArrayList<Image5D>();
      acqSettings_ = acqSettings;
      
      int nPositions = Math.max(1, (int) acqSettings.getPositionList().size());
      int nTimes = Math.max(1, (int) acqSettings.getTimeSeries().size());
      int nChannels = Math.max(1, (int) acqSettings.getChannelList().size());
      int nSlices = Math.max(1, (int) acqSettings.getZStack().size());


      for (int posIndex = 0; posIndex < nPositions; ++posIndex) {
         String posName = acqSettings_.getPositionList().get(posIndex).getName();
         try {
            gui_.openAcquisition(posName, acqSettings.getRoot(), 1, nChannels, nSlices);
            for (int i=0; i<channels.size(); ++i) {
               gui_.setChannelColor(posName, i, channels.get(i).color_);
               gui_.setChannelName(posName, i, channels.get(i).config_);
            }
            gui_.initializeAcquisition(posName, 512, 512, 1);


         } catch (MMScriptException ex) {
            ReportingUtils.logError(ex);
         }
      }
   }

   public abstract void run();
   

   protected void displayImage(Object img, Metadata m) {
      int posIndex = getMetadataIndex(m, "Position");
      int channelIndex = getMetadataIndex(m, "ChannelIndex");
      int sliceIndex = getMetadataIndex(m, "Slice");
      int frameIndex = getMetadataIndex(m, "Frame");

      try {
         String posName = acqSettings_.getPositionList().get(posIndex).getName();
         gui_.addImage(posName, img, frameIndex, channelIndex, sliceIndex);
      } catch (MMScriptException ex) {
         ReportingUtils.logError(ex);
      }
   }

   private int getMetadataIndex(Metadata m, String key) {
      if (m.getFrameData().has_key(key)) {
         String val = m.getFrameData(key);
         if (val == null || val.length() == 0)
            return -1;
         else
            System.out.println("frameData[\"" + key + "\"] = \"" + val + "\"");
            int result;
            try {
               result = Integer.parseInt(val);
            } catch (Exception e) {
               e.printStackTrace();
               System.out.println("error. now val = \"" + val + "\"");
               result = -1;
            }
            return result;
      } else {
         return -1;
      }
   }


   protected boolean sameFrame(Metadata lastMD, Metadata mdCopy) {
      if (lastMD == null || mdCopy == null) {
         return false;
      }
      boolean same = true;
      same = same && (getMetadataIndex(lastMD, "Position") == getMetadataIndex(mdCopy, "Position"));
      same = same && (getMetadataIndex(lastMD, "ChannelIndex") == getMetadataIndex(mdCopy, "ChannelIndex"));
      same = same && (getMetadataIndex(lastMD, "Slice") == getMetadataIndex(mdCopy, "Slice"));
      same = same && (getMetadataIndex(lastMD, "Frame") == getMetadataIndex(mdCopy, "Frame"));
      return same;
   }

   protected void cleanup() {
      try {
         gui_.closeAllAcquisitions();
      } catch (Exception e) {
         ReportingUtils.logError(e);
      }
   }
}

@@ -317,49 +317,8 @@
 ce;%0A
-import org.micromanager.image5d.Image5D;%0A
 impo
@@ -356,16 +356,16 @@
 elSpec;%0A
+
 import o
@@ -606,54 +606,8 @@
 t_;%0A
-   protected   ArrayList%3CImage5D%3E i5dVector_;%0A
    p
@@ -846,53 +846,8 @@
 re;%0A
-      i5dVector_ = new ArrayList%3CImage5D%3E();%0A
     
@@ -1183,25 +1183,46 @@
 ).size());%0A%0A
+      String posName;
 %0A
-
       for (i
@@ -1276,39 +1276,32 @@
 dex) %7B%0A         
-String 
 posName = acqSet
@@ -1298,64 +1298,31 @@
 e = 
-acqSettings_.getPositionList().get(posIndex).getName(
+getPosName(posIndex
 );%0A
+%0A
     
@@ -1801,24 +1801,297 @@
     %7D%0A   %7D%0A%0A
+   private String getPosName(int posIndex) %7B%0A      String posName;%0A      if (acqSettings_.getPositionList().isEmpty()) %7B%0A         posName = %22acq%22;%0A      %7D else %7B%0A         posName = acqSettings_.getPositionList().get(posIndex).getName();%0A      %7D%0A      return posName;%0A   %7D%0A%0A
    public ab
@@ -2419,111 +2419,42 @@
     
-String posName = acqSettings_.getPositionList().get(posIndex).getName();%0A         gui_.addImage(posName
+gui_.addImage(getPosName(posIndex)
 , im

remove image5d imports; make sure that not using position list doesn't crash