package eu.xeppaka.scalalearn

import akka.actor.{Actor, OneForOneStrategy, PoisonPill, SupervisorStrategy, Timers}
import ActorTest1._

import scala.concurrent.ExecutionContextExecutor
import scala.concurrent.duration._
import akka.actor.SupervisorStrategy._

object ActorTest1 {
  case object Create
  case object Delete
  case object Print
}

class ActorTest1 extends Actor {

  override val supervisorStrategy: SupervisorStrategy = OneForOneStrategy() {
    case _ => Restart
  }

  case class State(
    exist: Boolean = false
  )

  private var state = State()

  override def preStart(): Unit = {
    super.preStart()

    implicit val executionContext: ExecutionContextExecutor = context.dispatcher
    //context.system.scheduler.scheduleOnce(2.seconds, self, PoisonPill)
  }

  override def postStop(): Unit = {
    super.postStop()
    println(s"actor ${self.path.name} stopped.")
  }

  val exist: Receive = {
    case Create =>
    case Delete => state = state.copy(exist = false)
    case Print => println(state)
  }

  val notExist: Receive = {
    case Create => state = state.copy(exist = true)
    case Delete =>
    case Print =>
      println(state)
      //throw new Exception("blah")
    //throw new Throwable("blah")
  }

  override val receive: Receive = {
    case message: Any if state.exist => exist(message)
    case message: Any if !state.exist => notExist(message)
  }
}
