(ns games.crow.mesh-utils)

(defn create-indexed-mesh-from-data
  "Create an updatable mesh with shared vertices and one color per vertex."
  [name {:keys [vertices faces colors]} scene]
  (let [positions (into-array (mapcat (fn [[x y z]] [x z y]) vertices))
        indices (into-array (mapcat identity faces))
        normals (array)
        vertex-colors (into-array
                        (mapcat (fn [color]
                                  (let [c (js/BABYLON.Color3.FromHexString color)]
                                    [(.-r c) (.-g c) (.-b c) 1]))
                                colors))
        vertex-data (js/BABYLON.VertexData.)
        mesh (js/BABYLON.Mesh. name scene)
        material (js/BABYLON.StandardMaterial. (str name "-mat") scene)]
    (js/BABYLON.VertexData.ComputeNormals positions indices normals)
    (set! (.-positions vertex-data) positions)
    (set! (.-indices vertex-data) indices)
    (set! (.-normals vertex-data) normals)
    (set! (.-colors vertex-data) vertex-colors)
    (.applyToMesh vertex-data mesh true)
    (set! (.-backFaceCulling material) false)
    (set! (.-material mesh) material)
    mesh))

(defn create-mesh-from-data
  "Convert mesh data (vertices, faces, colors) to a Babylon.js mesh.
   mesh-data: {:vertices [...], :faces [...], :colors [...]}
   Returns a Babylon.js mesh."
  [name mesh-data scene]
  (let [vertices (:vertices mesh-data)
        faces (:faces mesh-data)
        colors (:colors mesh-data)
        vertex-data (js/BABYLON.VertexData.)
        ;; Duplicate vertices per face so neighboring faces can have different colors.
        face-vertices (mapcat (fn [face] (map #(nth vertices %) face)) faces)
        positions (into-array (mapcat (fn [[x y z]] [x z y]) face-vertices))
        offsets (reductions + 0 (map count faces))
        indices (into-array
                  (mapcat (fn [face offset]
                            (mapcat (fn [i]
                                      [offset (+ offset i) (+ offset i 1)])
                                    (range 1 (dec (count face)))))
                          faces offsets))
        normals (array)
        mesh (js/BABYLON.Mesh. name scene)]

    (js/BABYLON.VertexData.ComputeNormals positions indices normals)
    (set! (.-positions vertex-data) positions)
    (set! (.-indices vertex-data) indices)
    (set! (.-normals vertex-data) normals)
    (when colors
      (set! (.-colors vertex-data)
            (into-array
              (mapcat (fn [face color]
                        (let [c (js/BABYLON.Color3.FromHexString color)]
                          (mapcat (fn [_] [(.-r c) (.-g c) (.-b c) 1]) face)))
                      faces colors))))
    (.applyToMesh vertex-data mesh)

    (let [material (js/BABYLON.StandardMaterial. (str name "-mat") scene)]
      (set! (.-backFaceCulling material) false)
      (set! (.-material mesh) material))

    mesh))

(defn hex-color->babylon
  "Convert hex color string to Babylon.js Color3.
   e.g. '#FF0000' -> Color3(1, 0, 0)"
  [hex-str]
  (let [hex (if (= (first hex-str) "#") (subs hex-str 1) hex-str)
        r (js/parseInt (subs hex 0 2) 16)
        g (js/parseInt (subs hex 2 4) 16)
        b (js/parseInt (subs hex 4 6) 16)]
    (js/BABYLON.Color3. (/ r 255.0) (/ g 255.0) (/ b 255.0))))
