package wow.doge.mygame.utils.wrappers.jme
import cats.effect.Sync
import com.jme3.{bullet => jmeb}
import com.jme3.{scene => jmes}
import wow.doge.mygame.implicits._

final class PhysicsSpace[F[_]: Sync](space: jmeb.PhysicsSpace) {
  def add(anyObject: Any) = Sync[F].delay(space.add(anyObject))

  def remove(anyObject: Any) =
    Sync[F].delay {
      space.remove(anyObject)
      space
    }

  def addAll(spatial: jmes.Spatial) = Sync[F].delay(space.addAll(spatial))

  def removeAll(spatial: jmes.Spatial) =
    Sync[F].delay {
      space.removeAll(spatial)
      space
    }

  def collisionObservable = space.collisionObservable()
  def physicsTickObservable = space.physicsTickObservable()
}
object PhysicsSpace {
  implicit final class PhysicsSpaceOps[F[_]](private val space: PhysicsSpace[F])
      extends AnyVal {
    def +=(anyObject: Any) = space.add(anyObject)

    def :+(anyObject: Any) = {
      space.add(anyObject)
      space
    }
    def -(anyObject: Any) = {
      space.remove(anyObject)
      space
    }

    def +=(spatial: jmes.Spatial) = space.addAll(spatial)

    def :+(spatial: jmes.Spatial) = {
      space.addAll(spatial)
      space
    }

    def -(spatial: jmes.Spatial) = {
      space.removeAll(spatial)
      space
    }
  }
}
