; @File(label="Image stack directory",description="directory containing raw image stacks",style="directory") source-directory
; @File(label="Output directory for merged channels",description="directory for storing merged images",style="directory") merge-directory


(ns plugins.Scripts.Plugins.JunctionJ.Train_
  (:require [fun.imagej.img :as img]
            [fun.imagej.core :as ij]
            [fun.imagej.ops :as ops]
            [fun.imagej.img.shape :as shape]
            [fun.imagej.conversion :as convert]
            [fun.imagej.segmentation.imp :as ij1seg]
            [fun.imagej.img.cursor :as cursor]
            [fun.imagej.imp.roi :as roi]
            [fun.imagej.img.type :as imtype]
            [fun.imagej.imp :as ij1]
            [clojure.string :as string])
  (:import (ij.plugin ChannelSplitter)
           (trainableSegmentation Weka_Segmentation)))

(refer 'user)

;(println (.getPath source-directory))

(def vecad (atom []))
(def signal (atom []))
(def names (atom []))

;(def source-directory "/Users/kharrington/Data/Venkatraman_Lakshmi/Images for weka segmentaion/Raw images of 22_5_17")

(doseq [file (.listFiles (if (= (class source-directory) java.io.File)
                           source-directory
                           (java.io.File. source-directory)))]
    (when (.endsWith (.getPath file) "tif")
      (let [img1 (ij/open-img (.getPath file))
            imp1 (convert/img->imp img1)
            imp1 (ij.plugin.ZProjector/run imp1 "max")]
        ; DEBUG: remove crop stuff
        ;(.setRoi imp1 388 417 200 200)
        ;(ij.IJ/run imp1 "Crop" "")
        (let [basename (.getName file)
              basename (.substring basename 0 (- (count basename) 4)); remove extension
              channels (ChannelSplitter/split imp1)
              nchannel (fun.imagej.ops.threshold/maxEntropy (convert/imp->img (aget channels 0)))
              vchannel (fun.imagej.ops.convert/uint16 (convert/imp->img (aget channels 1)))
              schannel (convert/imp->img (aget channels 2))
              _ (fun.imagej.ops.image/invert (img/copy nchannel) nchannel)
              enuc-vecad (fun.imagej.ops.math/multiply (fun.imagej.ops.convert/uint16 (fun.imagej.ops.image/invert (img/create-img-like nchannel (imtype/bit-type)) nchannel))
                                                       vchannel)
              enuc-signal (fun.imagej.ops.math/multiply (fun.imagej.ops.convert/uint16 (fun.imagej.ops.image/invert (img/create-img-like nchannel (imtype/bit-type)) nchannel))
                                                        schannel)
              ]
          (ij/save-img enuc-vecad (str (.getPath merge-directory) java.io.File/separator basename "-vecad.tif"))
          (ij/save-img enuc-signal (str (.getPath merge-directory) java.io.File/separator basename "-signal.tif"))
          #_(ij1/save-imp (ij1/imps-to-rgb (map convert/img->imp [enuc-vecad enuc-signal]))
                        (str (.getPath merge-directory) java.io.File/separator basename "-merged.tif"))
          (swap! names conj basename)
          (swap! vecad conj (ij1/set-title (convert/img->imp enuc-vecad)
                                           (str basename)))
          (swap! signal conj (ij1/set-title (convert/img->imp (fun.imagej.ops.threshold/maxEntropy enuc-signal))
                                            (str basename)))))))

(let [signal-stack (.createEmptyStack (first @signal))
      vecad-stack (.createEmptyStack (first @vecad))]
  (dotimes [k (count @signal)]
    (.addSlice signal-stack (nth @names k)
               (.getProcessor (nth @signal k)))
    (.addSlice vecad-stack (nth @names k)
               (.getProcessor (nth @vecad k))))

  #_(ij1/show-imp (ij.ImagePlus. "Signal Stack" signal-stack))
  (ij1/show-imp (ij1/autocontrast! (ij.ImagePlus. "Vecad Stack" vecad-stack))))

(let [weka (Weka_Segmentation.)]
  (.run weka "")
  )

(println "done with weka")


;; TODO: output the probability stacks, track filename
;; TODO: output data file from weka
