(ns sample.core
    (:require [uikit.core :as uikit]
              [com.stuartsierra.component :as component]
              [com.github.harlanji.clojure_stack.greetings :as c]))

; last idea: name mangling of dash/underscore isn't right... try eliminating from package. must sleep.

(def white (-> ($ UIColor)
               ($ :whiteColor)))

(declare abc)

(defn on-tap [scope]
      (println "Tap"))

(defn- my-screen [g] [:UIView :main
                {:setBackgroundColor white
                 :constraints        ["C:button.centerx=main.centerx"
                                      "C:button.centery=main.centery"]}
                [(uikit/button 1) :button {:setTitle:forState [g 0]
                                           :gestures          {:UITapGestureRecognizer #'on-tap}}]])

(defrecord MainUI []
           component/Lifecycle
           (start [component]
                  (let [window (-> ($ UIWindow)
                                   ($ :alloc)
                                   ($ :initWithFrame (-> ($ UIScreen)
                                                         ($ :mainScreen)
                                                         ($ :bounds))))
                        nav (-> ($ UINavigationController)
                                ($ :new))

                        ]
                       ;(println greeting)
                       (doto window
                             ($ :makeKeyAndVisible)
                             ($ :setRootViewController nav)
                             ($ :setBackgroundColor white))
                       (reset! uikit/current-top-controller nav)
                       ($ nav :setNavigationBarHidden true)
                       (assoc component
                              ; FIXME replacing the greeting call with a string works.
                              :ui (uikit/nav-push (uikit/controller "Screen title" (my-screen "Hey hey!"))))))
           (stop [component]
                 ))


(defonce system (atom (component/system-map :ui (->MainUI))))

(defn main []
      (println "Hai! hello:" (c/hello-again))
      (let [{:keys [ui]} (swap! system component/start)]
           ui))