 (ns db-quiz.components
-  (:require [db-quiz.layout :refer [hex-triangle]]
-            [db-quiz.logic :refer [init-board make-a-guess]]
+  (:require [db-quiz.layout :refer [hex-triangle shade-colour]]
+            [db-quiz.logic :refer [annull-game! init-board make-a-guess]]
             [db-quiz.model :as model] 
             [db-quiz.state :refer [app-state]]
             [db-quiz.util :refer [join-by-space now redirect toggle]]
             [db-quiz.modals :as modals]
             [db-quiz.geometry :as geo]
             [db-quiz.config :refer [config]]
-            [db-quiz.layout :refer [shade-colour]]
+            [db-quiz.normalize :refer [trim-player-name]]
             [clojure.string :as string]
             [reagent.core :as reagent :refer [atom]]
             [reagent-forms.core :refer [bind-fields]]
             [reagent-modals.modals :as reagent-modals]))
 
 (defn parse-hash
+  "Parse 'query parameters' from hash fragment in the URL.
+  For example '#?doc=1234' is parsed to {:doc \"1234\"}."
   [h]
   (let [kv-pairs (-> h
                      (string/replace #"^#\?" "")

    (when-not (nil? verdict)
      [:div#verdict.row
       [:div.col-sm-12
        [:p {:class (str "alert " verdict-class)}
         [icon]
         success ". Správná odpověď je " [:strong correct-answer] "."]]])))


  (let [{{:keys [completion start]} :timer
         :keys [on-turn players]} @app-state
        player-name (on-turn players)
        [trimmed-name font-class] (trim-player-name player-name)]
    [:div
      [:div.row
        [:div#on-turn {:class (join-by-space (name on-turn) font-class)}
          trimmed-name
          (when (= player-name "Rybička") [easter-egg on-turn])]]
      [timeout on-turn completion]]))

(defn play-page
  []
  (let [{:keys [board current-field]} @app-state]
    (if (empty? board)
      (redirect "/") ; If no data is loaded, redirect to home page. TODO: Can we redirect to root?
      [:div.container-fluid
       info-button
       [:div.row
        [:div.col-sm-6 [hex-triangle]]
        [:div#question-box.col-sm-6
         [player-on-turn]
         (when current-field
           [question-box current-field])]]
       [reagent-modals/modal-window]])))

; ----- End page -----

