   (:interval app))
 
 
+(defn- add-time [date time]
+  (-> date .getTime (+ time ) (js/Date.)))
 
+
+(defn move-interval [time app]
+  (-> app
+      (update-in [:interval :from-date] add-time time)
+      (update-in [:interval :to-date  ] add-time time)))
+
+
+(defn move-to-now [app]
+  (let [now (js/Date.)
+        int (or (- (.getTime (get-in app [:interval :to-date  ] now))
+                   (.getTime (get-in app [:interval :from-date] now)))
+                (* 1000 60 60 30))]
+    (-> app
+        (assoc-in [:interval :from-date]
+                  (-> now .getTime (- int) (js/Date.)))
+        (assoc-in [:interval :to-date] now))))
 
 
 (defn interval-as-string [from-date to-date]

   [r6p3s.ui.button :as button]
   [r6p3s.ui.glyphicon :as glyphicon]
   [r6p3s.ui.form-group :as form-group]

   [r6p3s.cpt.modal :as modal]
   [r6p3s.cpt.input :as input]



(defn component [app own {:keys [on-selected-fn] :as opts}]
  (letfn [(on-selected-fn-2 []
            (when on-selected-fn (on-selected-fn)))]
    (reify
      om/IRender
      (render [_]
        (let [app-v                       @app
              {:keys [from-date to-date]} (get-selected app-v)
              app-modal                   (app :modal)]
          (dom/div #js {:className "well well-sm"
                        :style     #js {:backgroundColor "inherit"}}


                   (dom/div #js {:className "text-primary"}
                            (dom/div #js{:style #js {:width 20 :float "left"}} "от")
                            (datetime/render from-date)
                            (dom/br nil)
                            (dom/div #js{:style #js {:width 20 :float "left"}} "до")
                            (datetime/render to-date))
                   (dom/b nil "выбрано: ")
                   (interval-as-string from-date to-date)


                   (dom/div
                    #js {:className "btn-toolbar"}

                    (dom/div
                     #js {:className "btn-group"}                     
                     (button/render {:type     :default
                                     :text     (glyphicon/render "calendar")
                                     :on-click (fn []
                                                 (om/transact!
                                                  app-modal
                                                  (fn [app-modal]
                                                    (-> app-modal
                                                        (set-input-datetime-value :from-date from-date)
                                                        (set-input-datetime-value :to-date   to-date))))
                                                 (modal/show app-modal))})
                     (button/render {:type     :default
                                     :text     (glyphicon/render "time")
                                     :on-click (fn []
                                                 (om/transact! app move-to-now)
                                                 (on-selected-fn-2))}))

                    (dom/div
                     #js {:className "btn-group"}
                     (button/render {:type     :default
                                     :text     (glyphicon/render "fast-backward")
                                     :on-click (fn []
                                                 (om/transact! app (partial move-interval (* 1000 60 60 24 -7)))
                                                 (on-selected-fn-2))})
                     (button/render {:type     :default
                                     :text     (glyphicon/render "step-backward")
                                     :on-click (fn []
                                                 (om/transact! app (partial move-interval (* 1000 60 60 24 -1)))
                                                 (on-selected-fn-2))})
                     (button/render {:type     :default
                                     :text     (glyphicon/render "step-forward")
                                     :on-click (fn []
                                                 (om/transact! app (partial move-interval (* 1000 60 60 24  1)))
                                                 (on-selected-fn-2))})
                     (button/render {:type     :default
                                     :text     (glyphicon/render "fast-forward")
                                     :on-click (fn []
                                                 (om/transact! app (partial move-interval (* 1000 60 60 24  7)))
                                                 (on-selected-fn-2))})
                     ))



                   (let [from-date (get-input-datetime-value @app-modal :from-date)
                         to-date   (get-input-datetime-value @app-modal :to-date)]
                     (om/build modal-yes-no/component app-modal
                               {:opts {:label "Указать интервал времени"

                                       :body
                                       (dom/div
                                        #js{:className "row"}
                                        (om/build input-datetime/component-form-group (app-modal :from-date)
                                                  {:opts {:label      "От"
                                                          :spec-input {:type               "datetime-local" :placeholder "yyyy-MM-ddTHH:mm"
                                                                       :onChange-valid?-fn common-input/vldfn-not-empty-date}}})
                                        (om/build input-datetime/component-form-group (app-modal :to-date)
                                                  {:opts {:label      "До "
                                                          :spec-input {:type               "datetime-local" :placeholder "yyyy-MM-ddTHH:mm"
                                                                       :onChange-valid?-fn common-input/vldfn-not-empty-date}}})

                                        (form-group/render
                                         {:label "продолжитльность"
                                          :body  (dom/b #js {:className "text-primary"}
                                                        (interval-as-string from-date to-date))}))

                                       :act-yes-fn
                                       (fn []
                                         (om/update!
                                          app :interval
                                          {:from-date from-date
                                           :to-date   to-date})
                                         (modal/hide app-modal)
                                         (on-selected-fn-2))
                                       }}))))))))



