 (ns asols.client.stats
-  (:require [om.core :as om]
+  (:require [clojure.string :as str]
+            [om.core :as om]
             [om.dom :refer [render-to-str]]
             [om-tools.core :refer-macros [defcomponent]]
             [sablono.core :refer-macros [html]]
             [asols.client.solvings :refer [solving-block mutation-view]]
             [asols.client.widgets :as widgets]
-            [asols.client.utils :refer [debug format]]))
+            [asols.client.utils :refer [debug format log]]))
 
 (defn- iter-count-serie
   [solvings]

             (widgets/progress-bar (:value progress))]])

         [:.row
          [:.col-md-12
           (om/build cost-chart {:solvings solvings
                                 :train-values train-cost-serie
                                 :test-values test-cost-serie})]]
         [:.row
          [:.col-md-12
           (om/build ca-chart {:solvings solvings
                               :train-values train-ca-serie
                               :test-values test-ca-serie})]]]]))))
     [x y])))

(defn- costs-chart-config
  [train-serie test-serie static-train-serie static-test-serie]
  {:chart       {:type   "scatter"
                 :height "400"}
   :title       {:text "Cost"}
   :xAxis       {:allowDecimals false}
   :yAxis       {:title      {:text "Cost"}
                 :maxPadding 0}
   :plotOptions {:scatter {:lineWidth 1
                           :marker    {:enabled false}}}
   :tooltip     {:shared       true
                 :useHTML      true
                 :headerFormat "<b>{point.key}</b><br/>"
                 :pointFormat  "<span style=\"color:{point.color}\">\u25CF</span> {series.name}: <b>{point.y}</b><br/>"}
   :series      [{:name             "Static train cost"
                  :data             static-train-serie
                  :color            "red"
                  :allowPointSelect true}
                 {:name             "Static test cost"
                  :data             static-test-serie
                  :color            "red"
                  :dashStyle        "Dash"
                  :allowPointSelect true}
                 {:name   "Train cost"
                  :data   train-serie
                  :color  "blue"
                  :marker {:enabled true :symbol "square"}}
                 {:name      "Test cost"
                  :data      test-serie
                  :color     "blue"
                  :dashStyle "Dash"
                  :marker    {:enabled true :symbol "square"}}]})

(defn- ca-chart-config
  [train-serie test-serie static-train-serie static-test-serie]
  {:chart       {:type   "scatter"
                 :height "400"}
   :title       {:text "CA"}
   :xAxis       {:allowDecimals false}
   :yAxis       {:title      {:text "CA"}
                 :maxPadding 0}
   :plotOptions {:scatter {:lineWidth 1
                           :marker    {:enabled false}}}
   :tooltip     {:shared       true
                 :useHTML      true
                 :headerFormat "<b>{point.key}</b><br/>"
                 :pointFormat  "<span style=\"color:{point.color}\">\u25CF</span> {series.name}: <b>{point.y}</b><br/>"}
   :series      [{:name  "Static train CA"
                  :data  static-train-serie
                  :color "red"}
                 {:name      "Static test CA"
                  :data      static-test-serie
                  :color     "red"
                  :dashStyle "Dash"}
                 {:name   "Train CA"
                  :data   train-serie
                  :color  "blue"
                  :marker {:enabled true :symbol "square"}}
                 {:name      "Test CA"
                  :data      test-serie
                  :color     "blue"
                  :dashStyle "Dash"
                  :marker    {:enabled true :symbol "square"}}]})

(defn- operations-chart-config
  [solvings]
  (let [operations (mapcat
                     (fn [solving]
                       (let [m (:mutation (:best-case solving))
                             o (:operation m)]
                         (if (= :asols.mutations/combined o)
                          (map :operation (:mutations m))
                          [o])))
                     solvings)
        series (for [[operation ops] (group-by identity operations)]
                 [(name operation) (count ops)])]
    {:chart       {:plotBackgroundColor nil
                   :plotBorderWidth     nil
                   :plotShadow          false
                   :height 300}
     :title       {:text "Operations"}
     :plotOptions {:pie {:allowPointSelect true
                         :showInLegend     true}}
     :series      [{:type "pie"
                    :name "Operations used"
                    :data series}]}))

(defcomponent cost-chart [[s1 s2 s3 s4]]
  (render [_]
    (om/build widgets/highchart (costs-chart-config s1 s2 s3 s4))))

(defcomponent ca-chart [[s1 s2 s3 s4]]
  (render [_]
    (om/build widgets/highchart (ca-chart-config s1 s2 s3 s4))))

(defcomponent operations-chart [{:keys [solvings]}]
  (render [_]
    (om/build widgets/highchart (operations-chart-config solvings))))

(defn- series->csv
  [series]
  (str/join "\n\n" (for [serie series]
                   (str (mapv first serie) "\n" (mapv second serie)))))

(defcomponent stats-panel [{:keys [progress solvings metrics]}]
  (render [_]
    (let [[train-cost-data test-cost-data train-ca-data test-ca-data] metrics]
      (html
       [:.panel.panel-success
        [:.panel-heading "Stats"]
