package wow.doge.mygame.utils.wrappers.jme

import cats.Show
import cats.kernel.Eq
import com.jme3.bullet.collision.shapes.CollisionShape
import com.jme3.bullet.{util => jmebu}
import com.jme3.scene.Spatial
import monix.bio.IO

object CollisionShapeFactory {
  sealed trait Error
  case class WrongArgumentError(reason: String) extends Error

  object Error {
    implicit val show = Show.fromToString[Error]
    implicit val eq = Eq.fromUniversalEquals[Error]
  }

  def createMeshShape(subtree: Spatial): IO[Error, CollisionShape] =
    IO(jmebu.CollisionShapeFactory.createMeshShape(subtree))
      .onErrorHandleWith {
        case ex: IllegalArgumentException
            if (ex.getMessage
              .startsWith("The spatial must either be a Node")) =>
          IO.raiseError(WrongArgumentError(ex.getMessage))
      }
}
