                      ["appshare_element_box" "ui"]
                      ["appshare_element_left_padding" "ui"]
                      ["appshare_element_top_padding" "ui"]
+                     ["appshare_custom_component" "component"]
+                     ["appshare_call_custom_component" "ui"]
                      ])
 
 


            (add-blocks "Basic"     basic-blocks)

            (add-blocks "Glue"     [["appshare_custom_component" "component"]
                                    ["appshare_call_custom_component" "ui"]
                                    ])



            (add-blocks "Text"     [  ["appshare_div" "ui"]
                                      ["appshare_no_attributes" "ui"]
                                      ["appshare_element_attribute" "ui"]
                                      ["appshare_element_text" "ui"]])

            (add-blocks "Shapes"   [])

            (add-blocks "Custom"  [ ["appshare_ui_component" "ui"]
                                    ["appshare_div" "ui"]
                                    ["appshare_no_attributes" "ui"]
                                    ["appshare_element_attribute" "ui"]
                                    ["appshare_element_text" "ui"]])

            )





(defn reeval [app-id   calling-app-id]
  (if (not @in-eval)
  (go
    (reset! in-eval true)
    ;(js/alert (str "(get @app-state :ui :editor :mode):" (get @app-state :ui :editor :mode)))
    (let [
           code             (cond
                              (= (get-in @app-state [:ui :editor :mode])  "text")
                              (js/getCodeMirrorValue)

                              (= (get-in @app-state [:ui :editor :mode])  "blockly")
                              (js/getBlocklyValue))

           blockly-xml      (if (= (get-in @app-state [:ui :editor :mode])  "blockly")
                              (js/getBlocklyXml))

           app-session-id   (str (js/getappsessionid) )
           ]

      (if
        (= (get-in @app-state [:ui :editor :mode])  "text")
        (do
          (remote !savecode {:id                 app-id
                             :code               (subs code 0 2000)
                             :app-session-id     app-session-id})

          (remote !savecode2 {:id               app-id
                              :code             (subs code 2000 4000)
                              :app-session-id   app-session-id})))


      (if blockly-xml
        (do
          (remote !savecode {:id                 app-id
                             :code               (subs code 0 2000)
                             :app-session-id     app-session-id})

          (remote !savecode2 {:id               app-id
                              :code             (subs code 2000 4000)
                              :app-session-id   app-session-id})
          (remote !saveblockly {:id                 app-id
                             :code               (subs blockly-xml 0 2000)
                             :app-session-id     app-session-id})

          (remote !saveblockly2 {:id               app-id
                              :code             (subs blockly-xml 2000 4000)
                              :app-session-id   app-session-id})
          )


        )

      (swap! ns-counter inc)
      ;(js/alert (str  @ns-counter))
      (touch [:ui])
      (js/sendcode  (str (start) code (end))
                    calling-app-id
                    ;"reevalapp"
                    (clj->js "REV first one"
                             )
                    )
      (reset! in-eval false)
      ))))














(defn evalapp [app-id   calling-app-id   args]
  (if (not @in-eval)
  (go
    (reset! in-eval true)
    ;(js/alert (str app-id))
    (let [
           app-session-id   (str (js/getappsessionid))
           app-code-resp    (remote  !getfilecontents  {:calling-from-application-id          calling-app-id
                                                        :running-application-id               app-id
                                                        :app-session-id                       app-session-id})
           code-format      (:code-format   app-code-resp)

           app-code-value         (cond
                                    (= code-format "blockly")
                                    (do
                                      (js/setBlocklyXml  (:blockly app-code-resp))
                                      (js/getBlocklyValue))

                                    :else
                                    (:value app-code-resp))

           user-can-edit-app (:value (remote  !user-can-edit-app?  {:id             app-id
                                                                    :session-id     (:session-id @client-session-atom)}))

           ]
      ;(js/alert (pr-str "HOST SESSION ID: "   (:session-id @client-session-atom)))
      ;(js/alert (pr-str "CLIENT SESSION ID: " (js/getappsessionid)))
      ;(js/alert (pr-str "Code format: " code-format))
      ;(js/alert (pr-str "app-code-value: " js/Blockly.ClojureScript.valueToCode ))

      (cond
        (= code-format "blockly")
        (do
          (swap! app-state assoc-in [:ui :editor :mode] "blockly")
          ;(js/alert  (str (:blockly app-code-resp)))
          (js/setBlocklyXml (str (:blockly app-code-resp)))
          (js/populateEditor app-code-value)
          )

        :else
        (do
          (swap! app-state assoc-in [:ui :editor :mode] "text")
          (js/populateEditor app-code-value)

          (if user-can-edit-app
            (js/setCodeMirrorOption "readOnly" false)
            (js/setCodeMirrorOption "readOnly" true)))

        )

      (reset! can-use-interfaces (:can-use-interfaces app-code-resp))
      (js/callresetclientstate    app-session-id)
      (swap! ns-counter inc)
      (js/sendcode (str (start)
                         app-code-value
                        (end))
                   calling-app-id
                   (clj->js ;["a" "b" {:d 1 :r "sfs"}]
                            "first one"
                            )
                   )
      (reset! in-eval false)
      (show-blocks  "Basic"  basic-blocks)

      ))))








(defn-ui-component     view-app-component   [app]
  {:on-mount
   (do  (go
          ;(remote !loadapp {:id (read-ui app [:app-id] )})
          ;(js/alert "loaded")
          ))}

  (iframe {:id "appframe" :frameBorder "0"
           :style {:display "inline-block"}
           :src
           (str (cond
                  @debug-mode
                  "http://127.0.0.1:3449"

                  :else
                  "http://appshare.co/appshare")
                "/devclient.html") :width "600" :height "800"}))










(defn-ui-component     edit-app-glyph-component   [app]
  (div {:style {:margin "20px"}}
       (map-many
         #(span {:style {:margin "10px"}
                 :className (str "glyphicon " %1)
                 :aria-hidden "true"
                 :onClick     (fn [x]
                                (go
                                  (remote  !saveappglyph  {:id (get-in @app-state [:app-id])  :glyph (str %1)})
                                  (swap! app-state assoc-in [:mode] "edit")))
                } "")
         glyphs)


       (button {:style { :marginRight "30px" :marginBottom "10px" :marginTop "20px"}
                :className "btn-lg btn-default"
                :onClick     #(go
                                (swap! app-state assoc-in [:mode] "edit"))}
               "Cancel")

       ))








(defn  ^:export refreshapp []
  (do
    (reeval (get @app-state :app-id) nil)
    ;(show-blocks  "Basic"  basic-blocks)
    ))



