package com.example;

import akka.actor.typed.ActorRef;
import akka.actor.typed.Behavior;
import akka.actor.typed.javadsl.AbstractBehavior;
import akka.actor.typed.javadsl.ActorContext;
import akka.actor.typed.javadsl.Behaviors;
import akka.actor.typed.javadsl.Receive;

// #greeter
public class Overseer extends AbstractBehavior<Overseer.Request> {

  public record Request(Greeter.Greet msg, ActorRef<Greeter.Message> replyTo) {}

  public static Behavior<Overseer.Request> create(int max) {
    return Behaviors.setup(context -> new Overseer(context, max));
  }

  private final int max;
  private int current;

  private Overseer(ActorContext<Overseer.Request> context, int max) {
    super(context);
    this.max = max;
  }

  @Override
  public Receive<Request> createReceive() {
    return newReceiveBuilder().onMessage(Request.class, this::onRequest).build();
  }

  private Behavior<Request> onRequest(Request command) {
    this.current++;
    //#greeter-send-message
    command.replyTo().tell(new Greeter.Response(this.current <= this.max, command.msg()));
    //#greeter-send-message
    return this;
  }
}
// #greeter

