(defn get-audio-port [node-id port-type port-id]
  (let [node (get (:nodes @*db*) node-id)]
    (if node
      (let [an (:audio-node node)
            typ (:type node)]
        (if an
          (if (= typ :destination)
            an
            (if (= port-type "input")
              ;; Either an audio "in" stream, or a modifiable AudioParam (frequency, detune, delayTime, etc)
              (if (= port-id "in")
                (if (:in an) (:in an) (if (:cleanup an) nil an))
                ;; Resolve AudioParam based on type map structure
                (cond
                  (= typ :filter)     (js/get an port-id)
                  (= typ :oscillator) (js/get an port-id)
                  (= typ :gain)       (js/get an port-id)
                  (= typ :panner)     (js/get an port-id)
                  
                  (= typ :delay)
                  (cond
                    (= port-id "delayTime") (js/get (:delay an) "delayTime")
                    (= port-id "feedback")  (js/get (:fb an) "gain")
                    true nil)
                    
                  (= typ :distortion)
                  (if (= port-id "amount") (js/get (:drive an) "gain") nil)
                  
                  (= typ :reverb)
                  (if (= port-id "amount") (js/get (:wet an) "gain") nil)
                  
                  (= typ :lfo)
                  (cond
                    (= port-id "frequency") (js/get (:osc an) "frequency")
                    (= port-id "depth")     (js/get (:gain an) "gain")
                    true nil)
                    
                  (= typ :eq)
                  (cond
                    (= port-id "low")  (js/get (:low an) "gain")
                    (= port-id "mid")  (js/get (:mid an) "gain")
                    (= port-id "high") (js/get (:high an) "gain")
                    true nil)
                    
                  true nil))
              (if (:out an) (:out an) 
                (if (:cleanup an) nil an))))
          nil))
      nil)))

(defn connect-nodes! [from-id from-port to-id to-port]
  (swap! *db* (fn [db]
    (let [cs (:connections db)]
      (if (loop [c cs, found false]
            (if (empty? c) found
              (let [itm (first c)]
                (if (and (= (:from-node itm) from-id) (= (:to-node itm) to-id))
                  true
                  (recur (rest c) found)))))
        db
        (assoc db :connections (conj cs {:from-node from-id :from-port from-port :to-node to-id :to-port to-port}))))))
        
  (let [out-node (get-audio-port from-id "output" from-port)
        in-node (get-audio-port to-id "input" to-port)]
    (if (and out-node in-node)
      (do
         (js/log (str "NATIVE CONNECT: " from-id " -> " to-id))
         (js/call out-node "connect" in-node))
      (js/log "Failed to find native audio nodes!")))
  (save-local!))

(defn load-conns-async [cs ok fail total-conns done-cb]
  (if (empty? cs)
    (done-cb {:ok ok :fail fail})
    (let [c (first cs)]
      (swap! *db* (fn [db]
        (assoc db :loading {:text (str "Wiring " (:from-node c) " -> " (:to-node c))
                            :progress (/ (float (+ ok fail)) (float total-conns))})))
      (render-app)
      (js/call (js/global "window") "setTimeout"
        (fn []
          (let [on (get-audio-port (:from-node c) "output" (:from-port c))
                in (get-audio-port (:to-node c) "input" (:to-port c))]
            (if (and on in)
              (do (js/call on "connect" in) (load-conns-async (rest cs) (+ ok 1) fail total-conns done-cb))
              (load-conns-async (rest cs) ok (+ fail 1) total-conns done-cb))))
        5))))

(defn load-nodes-async [ctx parsed-nodes ks acc ok-list fail-list total-nodes done-cb]
  (if (empty? ks)
    (done-cb {:nodes acc :ok ok-list :fail fail-list})
    (let [k (first ks)
          n (get parsed-nodes k)
          p-type (:type n)
          def (get node-registry (keyword p-type))]
      (swap! *db* (fn [db]
        (assoc db :loading {:text (str "Spawning " p-type "...")
                            :progress (/ (float (count acc)) (float total-nodes))})))
      (render-app)
      (js/call (js/global "window") "setTimeout"
        (fn []
          (if def
            (let [an ((:create def) ctx (:params n))]
              (if (= p-type :sampler)
                (let [path (:path (:params n))]
                  (if (and path (> (count path) 0))
                    (load-remote-audio-file ctx path (fn [buf fname]
                      (js/call (js/global "window") "load_audio_buffer" k buf fname)))
                    nil))
                nil)
              (load-nodes-async ctx parsed-nodes (rest ks) (assoc acc k (assoc n :audio-node an)) (conj ok-list p-type) fail-list total-nodes done-cb))
            (load-nodes-async ctx parsed-nodes (rest ks) acc ok-list (conj fail-list p-type) total-nodes done-cb)))
        5))))


(defn toggle-recording []
  (let [window (js/global "window")
        mr (js/get window "mediaRecorder")
        state (if mr (js/get mr "state") nil)]
    (if (and mr (= state "recording"))
      (do
        (js/call mr "stop")
        (js/set window "is_recording" false)
        (js/call window "force_render")
        nil)
      (let [audio-ctx (js/get window "audioCtx")
            out-dest (js/get window "audioRecorderDest")]
        (if (not out-dest)
          (js/call window "alert" "Audio destination not ready. Please connect an Audio Output node.")
          (do
            (js/set window "recordedChunks" [])
            (let [new-mr (js/call (js/global "MediaRecorder") "new" (js/get out-dest "stream"))]
              (js/set new-mr "ondataavailable" (fn [e]
                (let [data (js/get e "data")
                      size (js/get data "size")
                      arr (js/get window "recordedChunks")]
                  (if (> size 0)
                    (js/call arr "push" data)
                    nil))))
              (js/set new-mr "onstop" (fn []
                (let [chunks (js/get window "recordedChunks")
                      options (js-obj)
                      _ (js/set options "type" "audio/webm")
                      blob (js/call (js/global "Blob") "new" chunks options)
                      url (js/call (js/global "URL") "createObjectURL" blob)
                      doc (js/global "document")
                      a (js/call doc "createElement" "a")]
                  (js/set (js/get a "style") "display" "none")
                  (js/set a "href" url)
                  (js/set a "download" "coni_synthesizer_export.webm")
                  (js/call (js/get doc "body") "appendChild" a)
                  (js/call a "click")
                  (js/call window "setTimeout" (fn [] 
                    (js/call (js/get doc "body") "removeChild" a)
                    (js/call (js/global "URL") "revokeObjectURL" url)) 100))))
              (js/set window "mediaRecorder" new-mr)
              (js/call new-mr "start")
              (js/set window "is_recording" true)
              (js/call window "force_render")
              nil)))))))


(defn delete-connection! [from-node from-port to-node to-port]
  (let [out-node (get-audio-port from-node "output" from-port)
        in-node (get-audio-port to-node "input" to-port)]
    (if (and out-node in-node)
      (js/call out-node "disconnect" in-node)
      nil))
  (swap! *db* (fn [db]
    (let [cs (:connections db)
          new-cs (loop [c cs, acc []]
                   (if (empty? c) acc
                     (let [itm (first c)]
                       (if (and (= (:from-node itm) from-node) (= (:to-node itm) to-node) (= (:from-port itm) from-port) (= (:to-port itm) to-port))
                         (recur (rest c) acc)
                         (recur (rest c) (conj acc itm))))))]
      (assoc db :connections new-cs))))
  (save-local!))

(defn disconnect-all! [node-id]
  (let [node (get (:nodes @*db*) node-id)]
    (if node
      (let [an (:audio-node node)]
        (if (:cleanup an) ((:cleanup an)) nil)
        (if (:out an)
          (.disconnect (:out an))
          (if (:disconnect an) (js/call an "disconnect") nil))
        (if (and (:osc an) (:disconnect (:osc an))) (.disconnect (:osc an)) nil))))
  
  (swap! *db* (fn [db]
    (let [cs (:connections db)
          new-cs (loop [c cs, acc []]
                   (if (empty? c) acc
                     (let [itm (first c)]
                       (if (or (= (:from-node itm) node-id) (= (:to-node itm) node-id))
                         (recur (rest c) acc)
                         (recur (rest c) (conj acc itm))))))]
      (assoc db :connections new-cs))))
      
  (let [cs (:connections @*db*)]
    (loop [c cs]
      (if (empty? c) nil
        (let [itm (first c)
              out-node (get-audio-port (:from-node itm) "output" (:from-port itm))
              in-node (get-audio-port (:to-node itm) "input" (:to-port itm))]
             (if (and out-node in-node) (js/call out-node "connect" in-node) nil)
             (recur (rest c))))))
  (save-local!))