(ns youyesyet.core
  (:require [reagent.core :as r]
            [re-frame.core :as rf]
            [secretary.core :as secretary]
            [goog.events :as events]
            [goog.history.EventType :as HistoryEventType]
            [markdown.core :refer [md->html]]
            [ajax.core :refer [GET POST]]
            [youyesyet.ajax :refer [load-interceptors!]]
            [youyesyet.handlers]
            [youyesyet.subscriptions]
            [youyesyet.ui-utils :as ui]
            [youyesyet.views.about :as about]
            [youyesyet.views.electors :as electors]
            [youyesyet.views.followup :as followup]
            [youyesyet.views.issue :as issue]
            [youyesyet.views.issues :as issues]
            [youyesyet.views.map :as maps])
  (:import goog.History))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;
;;;; youyesyet.core: core of the app.
;;;;
;;;; This program is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU General Public License
;;;; as published by the Free Software Foundation; either version 2
;;;; of the License, or (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
;;;; USA.
;;;;
;;;; Copyright (C) 2016 Simon Brooke for Radical Independence Campaign
;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; So that we can do debug logging!
(enable-console-print!)

(defn about-page []
  (about/panel))

(defn electors-page []
  (electors/panel))

(defn followup-page []
  (followup/panel))

(defn issues-page []
  (issues/panel))

(defn issue-page []
  (issue/panel))

(defn map-page []
  (maps/panel))

(def pages
  {:about #'about-page
   :electors #'electors-page
   :followup #'followup-page
   :issues #'issues-page
   :issue #'issue-page
   :map #'map-page
   })

(defn page []
  [:div
  [:header
   [ui/navbar]]
   [(pages @(rf/subscribe [:page]))]])

;; -------------------------
;; Routes
(secretary/set-config! :prefix "#")

(secretary/defroute "/" []
  (rf/dispatch [:set-active-page :map]))

(secretary/defroute "/about" []
  (rf/dispatch [:set-active-page :about]))

(secretary/defroute "/electors" []
  (rf/dispatch [:set-active-page :electors]))

(secretary/defroute "/followup" []
  (rf/dispatch [:set-active-page :followup]))

(secretary/defroute "/issues" []
  (rf/dispatch [:set-active-page :issues]))

(secretary/defroute "/issues/:elector" {elector-id :elector}
  (rf/dispatch [:set-elector-and-page {:elector-id elector-id :page :issues}]))

(secretary/defroute "/issue/:issue" {issue :issue}
  (rf/dispatch [:set-issue issue]))

(secretary/defroute "/map" []
  (rf/dispatch [:set-active-page :map]))

;; -------------------------
;; History
;; must be called after routes have been defined
(defn hook-browser-navigation! []
  (doto (History.)
    (events/listen
      HistoryEventType/NAVIGATE
      (fn [event]
        (secretary/dispatch! (.-token event))))
    (.setEnabled true)))

;; -------------------------
;; Initialize app

(defn mount-components []
  (r/render [#'page] (.getElementById js/document "app")))

(defn init! []
  (rf/dispatch-sync [:initialize-db])
  (load-interceptors!)
  (hook-browser-navigation!)
  (mount-components))
