 (ns ^{:doc "Code for Timetable Builder page"}
   cljs-nusmods.timetable
-  (:use [jayq.core :only [$ attr before children hide insert-after is parent
-                          prepend prevent remove-attr show text width]])
+  (:use [jayq.core :only [$ attr before children data hide insert-after is
+                          parent prepend prevent remove-attr show text width]])
   (:require [clojure.set]
             [clojure.string]
             [cljs-nusmods.select2 :as select2]

                   unselectedLessonGroupsMap)))]
      (set! Lessons-Created-By-Draggable augmentedTTLessonInfoSeq))))

; TODO: Account for a user dropping the draggable lesson onto a different lesson
;       from the one selected.
(defn- lesson-draggable-stop-evt-handler-maker
  "Returns a function used as the `stop` event handler for a draggable lesson."
  []
  (fn [evt ui]
    (.css (aget ui "helper") "cursor" "grab")
    (let [affectedDaysSet
          (reduce (fn [daySet ttLessonInfo]
                    (conj daySet (:day ttLessonInfo)))
                  #{}

    (.draggable $divElem
                (js-obj "zIndex" 100
                        "helper" "clone"
                        "revert" "invalid"
                        "start"  (lesson-draggable-start-evt-handler-maker
                                   moduleCode lessonType lessonLabel
                                   bgColorCssClass)
                        "stop"   (lesson-draggable-stop-evt-handler-maker
                                   moduleCode lessonType lessonLabel
                                   bgColorCssClass)))))

(defn- make-fake-lessons-droppable
  "For 'fake' lessons created due to a lesson <div> being dragged, we make
   them droppable in order for the user to switch lesson groups."
  [augModuleSelectedLessonInfoSeq]
  (doseq [augModuleSelectedLessonInfo augModuleSelectedLessonInfoSeq]
    (let [$divElem (:divElem augModuleSelectedLessonInfo)]
      (.droppable
        $divElem
        (js-obj "tolerance" "intersect"

                "over"
                (fn [evt ui]
                  (.removeClass $divElem "lesson-droppable-not-hover")
                  (.addClass $divElem "lesson-droppable-hover"))

                "out"
                (fn [evt ui]
                  (.removeClass $divElem "lesson-droppable-hover")
                  (.addClass $divElem "lesson-droppable-not-hover"))

                "drop"
                (fn [evt ui]
                  (let [$helper (aget ui "helper")]
                    ; Set a data attribute on the draggable helper to signal
                    ; that the user has dropped the <div> on a droppable to
                    ; change the lesson group
                    (data $helper
                          LESSON-GROUP-CHANGE-KEY
                          (:lessonGroup augModuleSelectedLessonInfo)))))))))

(defn- add-module-lesson-group
  "Adds a lesson group of a module to the timetable.
