(ns lineup.components.schedule
  (:require
   [lineup.components.schedule.slot :refer [slot]]
   [lineup.subs :as subs]
   [re-com.core :as re-com]
   [re-frame.core :as re-frame]))

(defn schedule []
  (let [schedule-data (re-frame/subscribe [::subs/schedule])
        list-complete? (re-frame/subscribe [::subs/complete])
        schedule-name (:name @schedule-data)
        task-list (:tasks @schedule-data)
        library-data (re-frame/subscribe [::subs/library])
        library (fn [id]
                  (some #(when (= (:id %) id) %) @library-data))]
    [re-com/v-box
     :gap "10px"
     :height "100%"
     :children
     [(when (string? schedule-name)
        [re-com/title
         :label schedule-name
         :level :level3])
      [re-com/scroller
       :v-scroll :off
       :h-scroll :auto
       :class "schedule-scroller"
       :width "100%"
       :child
       [re-com/h-box
        :gap "10px"
        :align :center
        :children
        (concat
         [[re-com/box
           :height "150px"
           :width "150px"
           :child
           [:img
            {:src "go.png"}]]]
         (map-indexed
          (fn [index task]
            ^{:key (str "slot-" index)}
            [slot index (-> task
                            (merge (library (:ref task)))
                            (dissoc :ref))])
          task-list)
         [[re-com/box
           :height "150px"
           :width "150px"
           :child
           [:img
            {:src (if @list-complete? "clapping.gif" "clapping.png")}]]])]]]]))
