 (ns ais.core
   (:require [clojure.data.json :as json])
-  (:require [ais.extractors  :as ais-extractors])
+  (:require [ais.extractors  :as ais-ex])
   (:require [ais.mappings  :as ais-mappings])
   (:require [ais.types :as ais-types])
   (:require [ais.util :as ais-util])

;; ---

(defn parse-group [msgs]
  (let [[msg-1 msg-2] (sort-by ais-extractors/extract-fragment-number msgs)
        fill (ais-extractors/extract-fill-bits msg-2)
        payload (reduce str (map ais-extractors/extract-payload [msg-1 msg-2]))
        full-msg (str (ais-extractors/extract-packet-type msg-1) ",1,1,1,A," payload "," fill)
       ]
    (str full-msg "*" (ais-util/checksum full-msg))))


    (decode-binary-payload binary-payload specs)))

(defn parse-envelope [envelope]
  (let [fill (ais-ex/extract-fill-bits envelope)
        bits (ais-util/pad (payload->binary (ais-ex/extract-payload envelope)) fill)
       [type binary-payload] (split-type-binary-payload bits)]
    (parse-binary type binary-payload)))


(defn parse [line]
  (let [[envelope checksum] (ais-ex/extract-envelope-checksum line)]
    (if (not-any? nil? [envelope checksum])
      (if (valid-envelope? envelope checksum)
        (try
