(ns analysis-viewer.main
  (:require [analysis-viewer.events]
            [analysis-viewer.events.maps]
            [analysis-viewer.fxs]
            [analysis-viewer.views :as views]
            [clojure.string :as str]
            [day8.re-frame.http-fx]
            [re-frame.core :as re-frame]
            [re-frame.db]
            [reagent.dom :as rdom]
            [shared.events]))

(defn ^:dev/before-load stop []
  (js/console.log "Stopping..."))

;; google-chrome --allow-file-access-from-files file-rep-software/spread-d3/language_D3/renderers/d3/d3renderer/index.html

;; [CT] :8021/?output=a1195874-0bbe-4a8c-96f5-14cdf9097e02/5e55d631-99f8-4324-9ec9-386c9c4442d9.json&maps=
;; [CT] :8021/?output=a1195874-0bbe-4a8c-96f5-14cdf9097e02/3e18cc4c-6be8-46b7-b039-ec286f9e8e0d.json&maps=
;; [DT] :8021/?output=a1195874-0bbe-4a8c-96f5-14cdf9097e02/37f2cc7a-b3fc-4054-87d8-fd596cea22e2.json&maps=
;; [BF] :8021/?output=a1195874-0bbe-4a8c-96f5-14cdf9097e02/e4d05649-98d8-41af-a0e5-a0f1e5f63c26.json&maps=

(defn parse-url-qstring [qstring]
  (->> (str/split qstring #"&")
       (map (fn [s]
              (let [[pname pval] (str/split s #"=")]
                [(keyword pname) pval])))
       (into {})))

(defn ^:dev/after-load mount-ui []
  (rdom/render [views/main-screen]
               (.getElementById js/document "app"))
  ;; only for dev, take a look at `analysis-viewer.fxs/maybe-re-run-animation` comments for details
  (analysis-viewer.fxs/maybe-re-run-animation))

(defn start []
  (js/console.log "Starting..." )
  (let [{:keys [maps output custom_map]} (parse-url-qstring (subs js/window.location.search 1))
        country-details-maps (->> (str/split maps #",")
                                  (remove str/blank?)
                                  (map (fn [code] {:kind :country-detail
                                                   :code code})))
        effective-maps (if custom_map
                         [{:kind :custom :path custom_map}]
                         (into [{:kind :world}] country-details-maps))]
    (re-frame/dispatch-sync [:map/initialize
                             effective-maps
                             output])
    (mount-ui)))

(defn ^:export init []
  (start))

 google-chrome --allow-file-access-from-files file-rep-software/spread-d3/language_D3/renderers/d3/d3renderer/index.html
 [CT] :8021/?output=a1195874-0bbe-4a8c-96f5-14cdf9097e02/5e55d631-99f8-4324-9ec9-386c9c4442d9.json&maps=
 [CT] :8021/?output=a1195874-0bbe-4a8c-96f5-14cdf9097e02/3e18cc4c-6be8-46b7-b039-ec286f9e8e0d.json&maps=
 [DT] :8021/?output=a1195874-0bbe-4a8c-96f5-14cdf9097e02/37f2cc7a-b3fc-4054-87d8-fd596cea22e2.json&maps=
 [BF] :8021/?output=a1195874-0bbe-4a8c-96f5-14cdf9097e02/e4d05649-98d8-41af-a0e5-a0f1e5f63c26.json&maps=
 only for dev, take a look at `analysis-viewer.fxs/maybe-re-run-animation` comments for details
(ns analysis-viewer.main
  (:require [analysis-viewer.events]
            [analysis-viewer.events.maps]
            [analysis-viewer.fxs]
            [analysis-viewer.views :as views]
            [clojure.string :as str]
            [day8.re-frame.http-fx]
            [re-frame.core :as re-frame]
            [re-frame.db]
            [reagent.dom :as rdom]
            [shared.events]))

(defn ^:dev/before-load stop []
  (js/console.log "Stopping..."))



(defn parse-url-qstring [qstring]
  (->> (str/split qstring #"&")
       (map (fn [s]
              (let [[pname pval] (str/split s #"=")]
                [(keyword pname) pval])))
       (into {})))

(defn ^:dev/after-load mount-ui []
  (rdom/render [views/main-screen]
               (.getElementById js/document "app"))
  (analysis-viewer.fxs/maybe-re-run-animation))

(defn start []
  (js/console.log "Starting..." )
  (let [{:keys [maps output custom_map]} (parse-url-qstring (subs js/window.location.search 1))
        country-details-maps (->> (str/split maps #",")
                                  (remove str/blank?)
                                  (map (fn [code] {:kind :country-detail
                                                   :code code})))
        effective-maps (if custom_map
                         [{:kind :custom :path custom_map}]
                         (into [{:kind :world}] country-details-maps))]
    (re-frame/dispatch-sync [:map/initialize
                             effective-maps
                             output])
    (mount-ui)))

(defn ^:export init []
  (start))
