package wow.doge.mygame
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.ExecutorsModule

trait MainModule extends ExecutorsModule {

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