package wow.doge.mygame.math;

import cats.Show
import cats.kernel.Eq
import cats.syntax.eq._

import math.{abs, pow, sqrt}

case class ImVector3f(x: Float, y: Float, z: Float)
object ImVector3f {
  //format: off
  val Zero  = ImVector3f(0, 0, 0)
  val UnitX = ImVector3f(1, 0, 0)
  val UnitY = ImVector3f(0, 1, 0)
  val UnitZ = ImVector3f(0, 0, 1)
  val Unit  = ImVector3f(1, 1, 1)
  val Max   = ImVector3f(Float.MaxValue, Float.MaxValue, Float.MaxValue)
  val Min   = ImVector3f(Float.MinValue, Float.MinValue, Float.MinValue)
  //format: on

  implicit val show = new Show[ImVector3f] {
    def format(f: Float) = f.formatted("%.2f")
    override def show(t: ImVector3f): String =
      s"ImVector3f(${format(t.x)},${format(t.y)},${format(t.z)})"
  }

  implicit val eq = Eq.fromUniversalEquals[ImVector3f]

  private def squareDiff(f1: Float, f2: Float) =
    pow(f1.toDouble - f2.toDouble, 2)
  // private def squareDiff2(f1: Float, f2: Float) = pow((f1 - f2).toDouble, 2)

  def dst(v1: ImVector3f, v2: ImVector3f): Double =
    if (v1 === v2) 0
    else {
      val total =
        squareDiff(v1.x, v2.x) + squareDiff(v1.y, v2.y) +
          squareDiff(v1.z, v2.z)
      sqrt(total)
    }
  def manhattanDst(v1: ImVector3f, v2: ImVector3f) =
    if (v1 === v2) 0 else abs(v1.x - v2.x) + abs(v1.y - v2.y) + abs(v1.z - v2.z)

}
