(ns civitas.models
  (:require [clojure.math :as math]))

(def col
  {:white   "#FFFFFF"
   :dgreen  "#62B132"
   :dblue   "#5881D8"
   :lgreen  "#91DC47"
   :lblue   "#8FB5FE"
   :cred    "#F26767"
   :gyellow "#FFCD52"
   :cbrown  "#A86F40"
   :lgray   "#E0E0E0"
   :mgray   "#808080"
   :ddblue  "#2F4179"
   :sstone  "#D6D6D6"
   :swhite  "#F0F4F8"})

(defn circle-verts [r n]
  (let [angle-step (/ (* 2 math/PI) n)
        camera (- math/PI (/ math/PI 4))]
    (vec (for [i (range n)]
           [(* r (math/cos (+ camera (* i angle-step))))
            (* r (math/sin (+ camera (* i angle-step))))
            0]))))

(defn translate-verts [vs translation]
  (mapv (fn [pts]
          (mapv + pts translation))
        vs))

(defn aligned-faces [n offset]
  (for [i (range n)]
    [(+ offset i)
     (+ offset (mod (inc i) n))
     (+ offset n (mod (inc i) n))
     (+ offset n i)]))

(defn circle-mesh [r n color role]
  {:role     role
   :vertices (circle-verts r n)
   :faces    [(vec (range n))]
   :colors   (repeat n color)})

(defn cylinder-data [r h n color]
  (let [bottom (circle-verts r n)
        top (translate-verts bottom [0 0 h])]
    {:vertices (into bottom top)
     :faces (aligned-faces n 0)
     :colors (repeat n color)}))

(defn obelisk-data [r h rh n]
  (let [bottom (circle-verts r n)
        top (translate-verts (circle-verts (* 0.5 r) n) [0 0 h])]
    {:vertices (conj (into bottom top) [0 0 (+ h rh)])
     :faces (concat [(vec (range n))]
                    (aligned-faces n 0)
                    (for [i (range n)] [(+ n i) (+ n (mod (inc i) n)) (* 2 n)]))
     :colors (cons (:ddblue col) (repeat (* 2 n) (:swhite col)))}))

(defn cart-data []
  (let [wheel (mapv (fn [[x y _]] [0 y (- x)]) (circle-verts 1 10))
        wheel-height (- (apply min (map last wheel)))
        left-wheel (translate-verts wheel [-1 0 wheel-height])
        right-wheel (translate-verts wheel [1 0 wheel-height])
        base [[-1 -2 wheel-height] [1 -2 wheel-height]
              [1 2 wheel-height] [-1 2 wheel-height]]
        pole [[-0.1 2 0] [0 4 0] [0.1 2 0]]
        left-pole (translate-verts pole [-0.9 0 wheel-height])
        right-pole (translate-verts pole [0.9 0 wheel-height])]
    {:vertices (vec (concat left-wheel base right-wheel left-pole right-pole))
     :faces [(vec (range 0 10)) (vec (range 10 14)) (vec (range 14 24))
             (vec (range 24 27)) (vec (range 27 30))]
     :colors (repeat 5 (:cbrown col))}))

(defn star-ring [r height]
  (mapv (fn [i [x y _]]
          (let [scale (if (even? i) 1 0.8)]
            [(* x scale) (* y scale) height]))
        (range 12)
        (circle-verts r 12)))

(defn cone-data [r bottom height color]
  (let [n 12
        ring (star-ring r bottom)]
    {:vertices (conj ring [0 0 (+ bottom height)])
     :faces (cons (vec (reverse (range n)))
                  (for [i (range n)] [i (mod (inc i) n) n]))
     :colors (repeat (inc n) color)}))

(defn rounded-canopy-data [r bottom color]
  (let [n 12
        rings (concat (star-ring (* r 0.65) (+ bottom (* r 0.3)))
                      (star-ring r (+ bottom r))
                      (star-ring (* r 0.65) (+ bottom (* r 1.7))))
        bottom-index (* 3 n)
        top-index (inc bottom-index)
        faces (concat (for [i (range n)] [bottom-index (mod (inc i) n) i])
                      (aligned-faces n 0)
                      (aligned-faces n n)
                      (for [i (range n)] [(+ (* 2 n) i)
                                          (+ (* 2 n) (mod (inc i) n))
                                          top-index]))]
    {:vertices (vec (concat rings [[0 0 bottom] [0 0 (+ bottom (* 2 r))]]))
     :faces faces
     :colors (repeat (* 4 n) color)}))

(defn tree-data [canopy]
  (let [trunk (cone-data 0.8 0 5 (:cbrown col))
        offset (count (:vertices trunk))]
    {:vertices (into (:vertices trunk) (:vertices canopy))
     :faces (concat (:faces trunk) (map #(mapv (partial + offset) %) (:faces canopy)))
     :colors (concat (:colors trunk) (:colors canopy))}))

(defn house-mesh [w d h rh]
  {:role     "house"
   :vertices [[0 0 0] [w 0 0] [w d 0] [0 d 0]        ; bottom
              [0 0 h] [w 0 h] [w d h] [0 d h]        ; top of walls
              [0 (/ d 2) (+ h rh)] [w (/ d 2) (+ h rh)]] ; roof ridge
   :faces    [[0 1 2 3]                              ; floor
              [3 0 4 7]                              ; back side wall NS
              [2 3 7 6]                              ; back wall EW
              [0 1 5 4]                              ; front wall EW
              [1 2 6 5]                              ; front side wall NS

              ;; roof faces
              [7 4 8]                                ; back-left triangle
              [6 7 8 9]                              ; back roof
              [4 5 9 8]                              ; front roof
              [5 6 9]                                ; front-right triangle
              ]
   :colors   [(:ddblue col)
              (:gyellow col)
              (:gyellow col)
              (:gyellow col)
              (:gyellow col)
              (:cred col)
              (:cred col)
              (:cred col)
              (:cred col)]})

(defn granary-mesh [r h rh n]
  (let [bottom-verts (circle-verts r n)
        top-verts (-> (circle-verts r n)
                      (translate-verts [0 0 h]))
        cone-top [0 0 (+ h rh)]
        side-faces (aligned-faces n 0)
        bottom-face (vec (range n))
        cone-faces (for [i (range n)]
                     [(+ i n) (+ (mod (inc i) n) n) (+ n n)])]
    {:role     "granary"
     :vertices (vec (concat bottom-verts top-verts [cone-top]))
     :faces    (vec (concat [bottom-face] side-faces cone-faces))
     :colors   (vec (concat
                      [(:ddblue col)]
                      (repeat n (:gyellow col))
                      (repeat n (:cred col))))}))

(defn colosseum-mesh [r h n]
  (let [outer-bottom (circle-verts r n)
        outer-top (translate-verts (circle-verts r n) [0 0 h])
        seating-top (translate-verts (circle-verts r n) [0 0 (* 0.7 h)])
        seating-mid (translate-verts (circle-verts (* 0.5 r) n) [0 0 (* 0.3 h)])
        inner-bottom (circle-verts (* 0.5 r) n)
        half-n (quot n 2)
        [outer-faces-far outer-faces-near] (split-at half-n (aligned-faces n 0))
        seat-faces (aligned-faces n (* 2 n))
        inner-faces (aligned-faces n (* 3 n))
        bottom-face (vec (reverse (range n)))]
    {:role     "colosseum"
     :vertices (vec (concat outer-bottom outer-top seating-top seating-mid inner-bottom))
     :faces    (vec (concat [bottom-face] outer-faces-far
                            inner-faces seat-faces outer-faces-near))
     :colors   (vec (concat [(:cbrown col)]
                            (repeat half-n (:sstone col))
                            (repeat n (:sstone col))
                            (repeat n "url(#cobble)")
                            (repeat (- n half-n) (:swhite col))))}))

(defn fire-mesh [color]
  (let [n 6
        bottom-verts (translate-verts (circle-verts 3 n) [0 0 1])
        cone-faces (for [i (range n)]
                     [i (mod (inc i) n) n])]
    {:role     "fire"
     :vertices (conj bottom-verts [0 0 10])
     :faces    (vec (concat [(vec (range n))] cone-faces))
     :colors   (vec (repeat (inc n) color))}))

(defn aqueduct-mesh [length width height arch-width n]
  (let [segment-len (/ length n)
        ;; arch vertices
        pillar-verts
        (vec (mapcat
               (fn [i]
                 (let [x0 (+ (* i segment-len) (* 0.5 segment-len))
                       x1 (+ x0 arch-width)
                       ;; front leg
                       v0 [x0 0 0] v1 [x1 0 0] v2 [x1 0 height] v3 [x0 0 height]
                       ;; back leg
                       v4 [x0 width 0] v5 [x1 width 0] v6 [x1 width height] v7 [x0 width height]]
                   [v0 v1 v2 v3 v4 v5 v6 v7]))
               (range n)))
        ;; top beam vertices
        top-verts [[0 0 height] [length 0 height] [length width height] [0 width height]
                   [0 0 (+ height 1.0)] [length 0 (+ height 1.0)] [length width (+ height 1.0)] [0 width (+ height 1.0)]]
        vertices (into pillar-verts top-verts)
        ;; helper to offset indices
        offset-pillar (fn [i] (* i 8))
        ;; faces for arches
        pillar-faces (vec (mapcat
                            (fn [i]
                              (let [o (offset-pillar i)]
                                [[o (+ o 1) (+ o 2) (+ o 3)]
                                 [(+ o 3) o (+ o 4) (+ o 7)]
                                 [(+ o 1) (+ o 2) (+ o 6) (+ o 5)]
                                 [(+ o 4) (+ o 5) (+ o 6) (+ o 7)]]))
                            (range n)))
        ;; top beam face
        top-index (* n 8)
        o top-index
        top-face [[o (+ o 1) (+ o 2) (+ o 3)]               ;; bottom of gutter
                  [o (+ o 1) (+ o 5) (+ o 4)]               ;; side wall
                  [(+ o 2) (+ o 3) (+ o 7) (+ o 6)]         ;; side wall
                  ]]
    {:role     "aqueduct"
     :vertices vertices
     :faces    (into pillar-faces top-face)
     :colors   (vec (concat
                      (repeat (* 2 n) (:gyellow col)) ;; arch legs
                      (repeat (* 2 n) (:gyellow col)) ;; arch legs
                      [(:ddblue col)]
                      (repeat (* 2 n) (:gyellow col)) ;; arch legs
                      ))}))

(defn aqueduct-waterfall-mesh [width bottom top color]
  {:role "aqueduct-waterfall"
   :vertices [[0 0 bottom] [0 width bottom] [0 width top] [0 0 top]]
   :faces [[0 1 2 3]]
   :colors [color]})

(defn oblong-mesh [x y z w d h color role]
  (let [half-w (/ w 2)
        half-d (/ d 2)
        vertices [[(- x half-w) (- y half-d) z] [(+ x half-w) (- y half-d) z]
                  [(+ x half-w) (+ y half-d) z] [(- x half-w) (+ y half-d) z]
                  [(- x half-w) (- y half-d) (+ z h)] [(+ x half-w) (- y half-d) (+ z h)]
                  [(+ x half-w) (+ y half-d) (+ z h)] [(- x half-w) (+ y half-d) (+ z h)]]
        faces [[0 1 2 3]
               [0 1 5 4]
               [3 0 4 7]
               [4 5 6 7]
               [2 3 7 6]
               [1 2 6 5]]
        colors (repeat 6 color)]
    {:role     role
     :vertices vertices
     :faces    faces
     :colors   colors}))

(defn roof-mesh [x y z w d rh color role]
  ;; x, y, z = center of roof base
  ;; w = width (x), d = depth (y), rh = ridge height
  (let [hx (/ w 2)
        hy (/ d 2)
        ;; base corners
        bl [(- x hx) (- y hy) z]                            ;; back-left
        br [(+ x hx) (- y hy) z]                            ;; back-right
        fr [(+ x hx) (+ y hy) z]                            ;; front-right
        fl [(- x hx) (+ y hy) z]                            ;; front-left
        ;; ridge vertices along depth
        ridge-left [(- x hx) 0 (+ z rh)]
        ridge-right [(+ x hx) 0 (+ z rh)]
        vertices [bl br fr fl ridge-left ridge-right]
        faces [[0 3 4]                                      ;; back-left triangle
               [1 0 4 5]                                    ;; back roof quad
               [3 2 5 4]                                    ;; front roof quad
               [2 1 5]                                      ;; front-right triangle
               ]]
    {:role     role
     :vertices vertices
     :faces    faces
     :colors   (repeat 4 color)}))
