package info.mukel.telegrambot4s.examples

import akka.actor.{Actor, ActorRef, Props, Terminated}
import info.mukel.telegrambot4s.actors.ActorBroker
import info.mukel.telegrambot4s.api.{AkkaDefaults, Commands, Polling}
import info.mukel.telegrambot4s.models.{Message, Update}

class PerChatRequestsBot(token: String) extends TestBot(token) with Polling with Commands with PerChatRequests {

  // Commands work as usual.
  on("/hello") { implicit msg => _ =>
    reply("Hello World!")
  }
}

trait PerChatRequests extends ActorBroker with Commands with AkkaDefaults {

  class Broker extends Actor {
    val chatActors = collection.mutable.Map[Long, ActorRef]()

    def receive = {
      case u : Update =>
        u.message.foreach { m =>
          val id = m.chat.id
          val handler = chatActors.getOrElseUpdate(m.chat.id, {
            val worker = system.actorOf(Props(new Worker), s"worker_$id")
            context.watch(worker)
            worker
          })
          handler ! m
        }

      case Terminated(worker) =>
        // This should be faster
        chatActors.find(_._2 == worker).foreach {
          case (k, _) => chatActors.remove(k)
        }

      case _ =>
    }
  }

  // Fo every chat a new worker actor will be spawned.
  // All requests will be routed through this worker actor; allowing to maintain a per-chat state.
  class Worker extends Actor {
    def receive = {
      case m : Message =>
        reply(self.toString)(m)

      case _ =>
    }
  }

  override val broker = Some(system.actorOf(Props(new Broker), "broker"))
}

Add per-chat request actor handler example
Add per-chat request actor handler example
