(ns wlmmap.map
  (:require [mrhyde.extend-js]
            [blade :refer [L]]
            [cljs.core.async :refer [chan <! >!]]
            [shoreleave.remotes.http-rpc :refer [remote-callback]])
  (:require-macros [cljs.core.async.macros :refer [go]]))

(blade/bootstrap)

(def mymap (-> L .-mapbox (.map "map" "examples.map-uci7ul8p")
      (.setView [45 3.215] 5)))

(def markers (L/MarkerClusterGroup.))

(let [ch (chan)]
  (go (while true
        (let [a (<! ch)]
          (let [lat (first (first a))
                lng (last (first a))
                title (last a)
                icon ((get-in L [:mapbox :marker :icon])
                      {:marker-symbol "post" :marker-color "0044FF"})
                marker (-> L (.marker (L/LatLng. lat lng) {:icon icon :title title}))]
            (.bindPopup marker title)
            (.addLayer markers marker))
          (.addLayer mymap markers))))
  (remote-callback
   :get-markers []
   #(doseq [a %] (go (>! ch a)))))


;; FIXME: get language
;; (js/alert (.-language js/navigator))

(ns wlmmap.map
  (:require [mrhyde.extend-js]
            [blade :refer [L]]
            [cljs.core.async :refer [chan <! >! timeout]]
            [shoreleave.remotes.http-rpc :refer [remote-callback]])
  (:require-macros [cljs.core.async.macros :refer [go]]))

(blade/bootstrap)

(def mymap (-> L .-mapbox (.map "map" "examples.map-uci7ul8p")
               (.setView [45 3.215] 5)))

(def markers (L/MarkerClusterGroup.))

(let [ch (chan)]
  (go (while true
        (let [a (<! ch)]
          (let [lat (first (first a))
                lng (last (first a))
                title (last a)
                icon ((get-in L [:mapbox :marker :icon])
                      {:marker-symbol "" :marker-color "0044FF"})
                marker (-> L (.marker (L/LatLng. lat lng)
                                      {:icon icon}))]
            (.bindPopup marker title)
            (.addLayer markers marker)))))
  (remote-callback :get-markers []
                   #(go (doseq [a %]
                          (<! (timeout 1))
                          (>! ch a)))))

(.addLayer mymap markers)

;; FIXME: get language
;; (js/alert (.-language js/navigator))

Add a timeout when loading markers.
Add a timeout when loading markers.

It works fine with chromium, but there is still something wrong
with recent version of Firefox.

bzg/wlmmap,bzg/wlmmap