(ns buddho.core.describe
  (:require [buddho.core.state :as s]
            [buddho.core.dhamma :as d]
            [clojure.string :as string]))

(defn description-of [property & [idea]]
  (loop [prop property
         ctx {}]
    (let [desc (s/prop-desc prop idea)]
      (if-not (seq prop)
        ctx
        (recur (vec (butlast prop)) (assoc ctx prop desc))))))

(defn description [idea]
  (->> (s/properties idea)
       (map #(description-of % idea))
       (apply merge)))

(defn describe [idea]
  (doseq [[prop desc] (description idea)]
    (println prop "=>" desc)))

(defn descr-dhamma-fn [idea]
  ;;; TODO things this is an aspect of
  (let [idea (s/value idea)
        aspects (d/aspects-of idea)
        format-singleton #(if (s/has-property? % [:dhamma :singleton]) "{S} " "{T} ")
        format-dh
        (fn [dh] (str "<" (format-singleton dh)
                      (:name (s/value dh)) " "
                      dh ">"))
        asp-strs (map (fn [[prop dhs]]
                        (str prop
                             " -> "
                             (#(if (empty? %1) %2 %1)
                              (string/join ", " (map format-dh dhs))
                              "(none)")))
                      aspects)]
    (str
     "Name:\t\t" (:name idea) "\n"
     (s/dh-desc (:name idea)) "\n"
     "Aspects:\t" (string/join "\n\t\t\t" asp-strs) "\n"
     "Data:\t\t" (:data idea) "\n"
     "Meta:\t\t" (:meta idea) "\n"
     "Transient?:\t" (:transient idea)

     )))

(s/describe-property! [:dhamma] "Idea is a dhamma." descr-dhamma-fn)
"foo"
