package wow.doge.mygame
import akka.actor.BootstrapSetup
import akka.actor.typed.ActorSystem
import akka.actor.typed.SpawnProtocol
import cats.effect.Resource
import io.odin.Logger
import monix.bio.Task
import wow.doge.mygame.executors.Schedulers.AsyncScheduler

class ActorSystemResource(logger: Logger[Task], scheduler: AsyncScheduler) {
  def get: Resource[Task, ActorSystem[SpawnProtocol.Command]] =
    Resource.make(
      logger.info("Creating Actor System") >> Task(
        ActorSystem(
          SpawnProtocol(),
          name = "GameActorSystem",
          BootstrapSetup().withDefaultExecutionContext(scheduler.value)
        )
      )
    )(sys =>
      for {
        _ <- Task(sys.terminate())
        _ <- Task.deferFuture(sys.whenTerminated)
        _ <- logger.info("Actor System Terminated")
      } yield ()
    )
}
