 (ns frontend.send
-  (:require [cljs.core.async :refer [chan]]
+  (:require [cljs-time.coerce :as time-coerce]
+            [cljs-time.core :as time]
+            [cljs.core.async :refer [chan]]
             [clojure.spec :as s :include-macros true]
+            [clojure.string :as string]
             [clojure.test.check.generators :as gen]
             [frontend.api :as api]
             [frontend.utils.vcs-url :as vcs-url]
             [om.next :as om-next]
-            [om.util :as om-util]
-            [clojure.string :as string])
+            [om.util :as om-util])
   (:require-macros [cljs.core.async.macros :as am :refer [go-loop]]))
 
 ;; These spec should find a better place to live, but data generation in `send`

                      (let [ident-vals (second (om-util/join-key expr))]
                        (gen/generate
                         (s/gen :workflow/entity
                                {:run/branch-name #(gen/fmap (partial string/join "-") (gen/vector latin-word 1 7))
                                 [:workflow/name] #(gen/return (:workflow/name ident-vals))
                                 [:workflow/project :project/name] #(gen/return (:project/name ident-vals))
                                 [:workflow/project :project/organization :organization/name] #(gen/return (:organization/name ident-vals))
                                 [:workflow/project :project/organization :organization/vcs-type] #(gen/return (:organization/vcs-type ident-vals))})))})
            ui-query)

        :else (throw (str "No clause found for " (pr-str expr)))))))

(s/def :run/started-at inst?)
(s/def :run/stopped-at inst?)
(s/def :run/branch-name string?)
(s/def :run/commit-sha (s/with-gen
                         (s/and string?
                                #(= 40 (count %))
                                (partial re-matches #"^[0-9a-f]*$"))
                         #(gen/fmap string/join
                                    (gen/vector
                                     (gen/fmap char
                                               (gen/one-of [(gen/choose 48 57)
                                                            (gen/choose 97 102)]))
                                     40))))
(s/def :run/entity (s/and
                    (s/keys :req [:run/id
                                  :run/status
                                  :run/started-at
                                  :run/stopped-at
                                  :run/branch-name
                                  :run/commit-sha])
                    #(< (:run/started-at %)
                        (:run/stopped-at %))))


;; via https://github.com/paraseba/faker/blob/master/srfaker/lorem_data.clj
(def latin-word
  (gen/elements
   ["alias" "consequatur" "aut" "perferendis" "sit" "voluptatem" "accusantium" "doloremque" "aperiam" "eaque" "ipsa" "quae" "ab" "illo" "inventore" "veritatis" "et" "quasi" "architecto" "beatae" "vitae" "dicta" "sunt" "explicabo" "aspernatur" "aut" "odit" "aut" "fugit" "sed" "quia" "consequuntur" "magni" "dolores" "eos" "qui" "ratione" "voluptatem" "sequi" "nesciunt" "neque" "dolorem" "ipsum" "quia" "dolor" "sit" "amet" "consectetur" "adipisci" "velit" "sed" "quia" "non" "numquam" "eius" "modi" "tempora" "incidunt" "ut" "labore" "et" "dolore" "magnam" "aliquam" "quaerat" "voluptatem" "ut" "enim" "ad" "minima" "veniam" "quis" "nostrum" "exercitationem" "ullam" "corporis" "nemo" "enim" "ipsam" "voluptatem" "quia" "voluptas" "sit" "suscipit" "laboriosam" "nisi" "ut" "aliquid" "ex" "ea" "commodi" "consequatur" "quis" "autem" "vel" "eum" "iure" "reprehenderit" "qui" "in" "ea" "voluptate" "velit" "esse" "quam" "nihil" "molestiae" "et" "iusto" "odio" "dignissimos" "ducimus" "qui" "blanditiis" "praesentium" "laudantium" "totam" "rem" "voluptatum" "deleniti" "atque" "corrupti" "quos" "dolores" "et" "quas" "molestias" "excepturi" "sint" "occaecati" "cupiditate" "non" "provident" "sed" "ut" "perspiciatis" "unde" "omnis" "iste" "natus" "error" "similique" "sunt" "in" "culpa" "qui" "officia" "deserunt" "mollitia" "animi" "id" "est" "laborum" "et" "dolorum" "fuga" "et" "harum" "quidem" "rerum" "facilis" "est" "et" "expedita" "distinctio" "nam" "libero" "tempore" "cum" "soluta" "nobis" "est" "eligendi" "optio" "cumque" "nihil" "impedit" "quo" "porro" "quisquam" "est" "qui" "minus" "id" "quod" "maxime" "placeat" "facere" "possimus" "omnis" "voluptas" "assumenda" "est" "omnis" "dolor" "repellendus" "temporibus" "autem" "quibusdam" "et" "aut" "consequatur" "vel" "illum" "qui" "dolorem" "eum" "fugiat" "quo" "voluptas" "nulla" "pariatur" "at" "vero" "eos" "et" "accusamus" "officiis" "debitis" "aut" "rerum" "necessitatibus" "saepe" "eveniet" "ut" "et" "voluptates" "repudiandae" "sint" "et" "molestiae" "non" "recusandae" "itaque" "earum" "rerum" "hic" "tenetur" "a" "sapiente" "delectus" "ut" "aut" "reiciendis" "voluptatibus" "maiores" "doloribus" "asperiores" "repellat"]))

(def inst-in-last-day
  #(s/gen (s/inst-in (time-coerce/to-date (time/minus (time/now) (time/days 1)))
                     (time-coerce/to-date (time/now)))))

(def
  ^{:doc
    "Generator overrides that make nice looking data. Good for mock data
     displays; bad for property testing, which wants to hit edge cases."}
  dummy-data-overrides
  {:run/branch-name #(gen/fmap (partial string/join "-")
                               (gen/vector latin-word 1 7))
   :run/started-at inst-in-last-day
   :run/stopped-at inst-in-last-day})



