package eu.xeppaka.scalalearn

import akka.actor.{ActorSystem, Props}
import akka.pattern.ask
import akka.stream.ActorMaterializer
import akka.util.Timeout
import eu.xeppaka.scalalearn.CacheUser.{TestMsg, TestMsgSuccess}

import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._
import scala.util.Success

object Main2 {
  def main(args: Array[String]): Unit = {
    implicit val actorSystem: ActorSystem = ActorSystem()
    implicit val materializer: ActorMaterializer = ActorMaterializer()
    implicit val timeout: Timeout = 1.second
    implicit val ec: ExecutionContext = actorSystem.dispatcher

    val cache = actorSystem.actorOf(Props(classOf[CardCacheActor]))
    val cacheApi = new CardCacheApi(cache)
    val cacheUser = actorSystem.actorOf(CacheUser.props(cacheApi, cache))

    (cacheUser ? TestMsg)
      .flatMap { vvv =>
        println(vvv)
        vvv match {
          case TestMsgSuccess(Some(msg)) =>
            println(msg)
            Future.successful("success")
        }
      }
      .onComplete(vvv => println(vvv))
  }
}
