package wow.doge.mygame

import java.util.concurrent.TimeoutException

import cats.Show
import cats.kernel.Eq
import monix.bio.IO
import wow.doge.mygame.utils.wrappers.jme.AssetManager
import wow.doge.mygame.utils.wrappers.jme.CollisionShapeFactory
import wow.doge.mygame.utils.wrappers.jme.NodeWrapper2

sealed trait AppError
object AppError {
  final case class TimeoutError(reason: String) extends AppError
  object TimeoutError {
    def from: PartialFunction[Throwable, IO[TimeoutError, Nothing]] = {
      case ex: TimeoutException => IO.raiseError(TimeoutError(ex.getMessage))
    }
  }
  final case class AssetManagerError(error: AssetManager.Error) extends AppError
  final case class AppNodeError(error: NodeWrapper2.Error) extends AppError
  final case class CollisionShapeCreationFailed(
      err: CollisionShapeFactory.Error
  ) extends AppError

  def fromThrowable: PartialFunction[Throwable, IO[AppError, Nothing]] = {
    case ex: TimeoutException => IO.raiseError(TimeoutError(ex.getMessage))

  }

  implicit val show = Show.fromToString[AppError]
  implicit val eq = Eq.fromUniversalEquals[AppError]
}
