(ns hoplon.app-pages._index_DOT_html (:require [weathergen.ipc :as ipc] [weathergen.ui :as ui] [weathergen.ui.buttons :as buttons] [weathergen.ui.common :as comm] [javelin.core :refer [->Cell input? cell cell? destroy-cell! lift lens? set-formula! constant? cell-doseq* deref* set-cell! lens formula? alts! dosync* cell-map formula]] [hoplon.jquery :refer [set-attributes! check-val! set-styles! text-val!]] [hoplon.core :refer [form audio input menuitem hgroup do! timeout $text base h1 set-attributes! embed shadow h3 body keygen progress main cite on-page-load i p nav ruby a menu blockquote img $comment span track data u dl select html thead del fieldset aside figure figcaption q on! bdi append-child! video address caption dd rp hr tbody table html-var add-initfn! pre ul replace-child! html-time html-map sup dfn sub mark script button wbr insert-before! strong normalize-class li dt rtc td tr section th optgroup bust-cache iframe remove-child! legend em kbd spliced html-object article abbr template multicol prerendering? <!-- source output set-styles! route-cell header datalist tfoot s ins footer title h5 canvas param div option summary samp small style textarea loop-tpl* h4 head ol details col label picture rt when-dom h6 link page-load colgroup meter html-meta static-elements bdo --> b attribute? code dialog noframes do-watch noscript safe-nth h2 area br]]) (:require-macros [weathergen.cljs.macros :refer [with-bbox with-time formula-of]] [javelin.core :refer [with-let mx2 dosync cell= set-cell!= prop-cell cell-doseq formulet defc defc= macroexpand-all mx formula-of cell-let]] [hoplon.core :refer [definterval text elem+ cache-key with-timeout defelem+ when-tpl static sexp defelem elem def-values if-tpl cond-tpl with-page-load for-tpl with-dom case-tpl loop-tpl with-interval with-init!]]))







(def electron (js/require "electron"))

(defc loading? false)
(defc progress-messages [])
(defc load-error nil)

(defmethod ipc/on-message "progress-message"
  [_ event msg]
  (swap! progress-messages conj msg))

(defmethod ipc/on-message "load-complete"
  [_ event]
  (dosync
   (swap! progress-messages conj "Load complete")
   (reset! loading? false)))

(defmethod ipc/on-message "load-failed"
  [_ _ err]
  (reset! loading? false)
  (reset! load-error err))

(defn load-mission
  []
  (when-let [[path] (-> electron
                        .-remote
                        .-dialog
                        (.showOpenDialog
                         (clj->js {:title      "Select a campaign or tactical engagement file"
                                   :properties ["openFile"]
                                   :filters    [{:name       "Campaign file"
                                                 :extensions ["cam"]}
                                                {:name       "Tactical engagement file"
                                                 :extensions ["tac"]}]})))]
    (dosync
     (reset! loading? true)
     (reset! load-error nil)
     (reset! progress-messages ["Setting up to load mission"]))
    (ipc/send-to-main "open-mission" path)))

(defn load-briefing
  []
  (when-let [[path] (-> electron
                        .-remote
                        .-dialog
                        (.showOpenDialog
                         #js {:title "Select a campaign or tactical engagement file"
                              :openFile true
                              :filters #js [#js {:name "VMT Briefing"
                                                 :extensions #js ["vmtb"]}]}))]
    (dosync
     (reset! loading? true)
     (reset! load-error nil)
     (reset! progress-messages ["Setting up to load briefing"]))
    (ipc/send-to-main "open-briefing" path)))

(html
 (ui/head)
 (body
  :css {:margin "0"}
  (div
   :css {:display        "flex"
         :flex-direction "column"
         :height         "100vh"}
   (div
    :css {:padding "8px 8px 0"}
    (ui/titlebar))
   (div
    :css {:font-size "150%"}
    (buttons/a-button
     :css {:display "inline-block"
           :margin  "5px 10px"}
     :disabled loading?
     :click load-mission
     "Load mission (.cam/.tac)")
    (buttons/a-button
     :css {:display "inline-block"
           :margin  "5px 10px"}
     :disabled loading?
     :click load-briefing
     "Load briefing (.vmtb)"))
   (with-let [elem (div
                    :css {:overflow-y "auto"
                          :overflow-x "auto"
                          :flex       1
                          :font-size  "150%"
                          :border     "solid 1px black"
                          :margin     "0 10px 20px 10px"
                          :padding    "4px"}
                    (for-tpl [message (cell= (butlast progress-messages))]
                      (div (span :css {:font-weight "bold"
                                       :color       "green"
                                       :width       "24px"
                                       :display     "inline-block"}
                                 "✓")
                           message))
                    (let [last-message (cell= (last progress-messages))]
                      (div
                       (if-tpl loading?
                         (img :css {:width          "24px"
                                    :vertical-align "middle"
                                    :margin-top     "-5px"}
                              :src "images/spinner.gif")
                         (span :css (formula-of [load-error]
                                      {:font-weight "bold"
                                       :color       (if load-error
                                                      "red"
                                                      "green")
                                       :width       "24px"
                                       :display     "inline-block"})
                               (cond-tpl
                                 (cell= (not last-message)) ""
                                 load-error "✗"
                                 :else "✓")))
                       last-message
                       (when-tpl load-error
                         (let [show? (cell false)]
                           [(span
                             :css {:color "blue"
                                   :text-decoration "underline"
                                   :font-size "70%"
                                   :margin-left "5px"}
                             :click #(swap! show? not)
                             (if-tpl show?
                               "Hide error details"
                               "Show error details"))
                            (pre :toggle show?
                                 :css {:padding-left "10px"}
                                 load-error)])))))]
     (add-watch progress-messages
                ::scroll-notifications
                (fn [_ _ _ _]
                  (with-timeout 0
                    (-> elem .-scrollTop (set! (.-scrollHeight elem))))))))))
