 (ns mininova-web-ui.ui
-  (:require [reagent.core :as reagent]
+  (:require [reagent.core :as r]
             [re-frame.core :as rf]
             [mininova-web-ui.midi :as midi]
             [mininova-web-ui.params :as params]))
 
+(def patch-inspector-enabled? true)
+
 (def patch-request-message
   [0xF0 0x00 0x20 0x29 0x03 0x01 0x7F 0x40 0x00 0x00 0x00 0x00 0x00 0xF7])
 
 (def patch-response-sentinel
   [0xF0 0x00 0x20 0x29 0x03 0x01 0x7F 0x00 0x00 0x09 0x04])
 
-(def default-panel :osc)
+(def default-panel (keyword (subs js/location.hash 1)))
 
 ;; FIXME: Get ridi of `re-frame: no :fx handler registered for :event` error
 (rf/reg-fx :event (fn [_ _]))


(defn make-set-patch-value [data]
  (fn [db [id param]]
    (if (contains? param :offset)
      (assoc-in db [::control id] (nth data (:offset param)))
      db)))

(rf/reg-event-db ::midi/sysex
  [rf/debug]
  (fn [db [_ data]]
    (if (patch-dump? data)
      (reduce (make-set-patch-value data) db params/params))))

(defn tabs []
  (let [tab @(rf/subscribe [::panel])]
    [:ul.tabs
      [:li {:class (if (or (= tab :osc)) (nil? tab) "is-active")
            :on-click #(rf/dispatch [::panel :osc])}
        "Osc"]
      [:li {:class (if (= tab :filter) "is-active")
            :on-click #(rf/dispatch [::panel :filter])}
        "Filter"]
      [:li {:class (if (= tab :env) "is-active")
            :on-click #(rf/dispatch [::panel :env])}
        "Env"]
      [:li {:class (if (= tab :lfo) "is-active")
            :on-click #(rf/dispatch [::panel :lfo])}
        "LFO"]
      [:li {:class (if (= tab :arp) "is-active")
            :on-click #(rf/dispatch [::panel :arp])}
        "ARP"]]))

(defn map-in-out [in out value]
  (+ (- value (first in)) (first out)))

        :env [env-panel]
        :lfo [lfo-panel]
        :arp [arp-panel]
        :patch-inspector [patch-inspector-panel]
        [:h2 "Select a panel"])]))
