package eu.xeppaka.scalalearn

import java.util.UUID

import akka.actor.{Actor, ActorRef, Props}
import eu.xeppaka.scalalearn.CacheUser.{CacheRandomCard, CacheRandomCard2, TestMsg, TestMsgSuccess}
import eu.xeppaka.scalalearn.CardCacheActor.{CacheCard, CacheSuccess}

object CacheUser {
  case object CacheRandomCard
  case object CacheRandomCard2
  case object TestMsg

  case class TestMsgSuccess(msg: Option[String])
  case object TestMsgFailure

  def props(cardCacheApi: CardCacheApi, cache: ActorRef): Props = Props(classOf[CacheUser], cardCacheApi, cache)
}

class CacheUser(cardCacheApi: CardCacheApi, cache: ActorRef) extends Actor {
  override def receive: Receive = {
    case CacheRandomCard =>
      println(s"name: ${self.path.name}")
      cardCacheApi.cacheCard(UUID.randomUUID().toString)
    case CacheRandomCard2 =>
      cache ! CacheCard(UUID.randomUUID().toString)
    case TestMsg =>
      println(s"name: ${self.path.toStringWithoutAddress}")
      sender() ! TestMsgSuccess(Some("data"))
    case CacheSuccess =>
      println("cache success!")
  }
}
