package com.example;

import akka.actor.typed.Behavior;
import akka.actor.typed.javadsl.*;

public class GreeterBot extends AbstractBehavior<Greeter.Greeted> {


    public static Behavior<Greeter.Greeted> create() {
        return Behaviors.setup(GreeterBot::new);
    }

    public int greetingCounter;

    private GreeterBot(ActorContext<Greeter.Greeted> context) {
        super(context);
    }

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

    private Behavior<Greeter.Greeted> onGreeted(Greeter.Greeted message) {
        greetingCounter++;
        getContext().getLog().info("Greeting {} for {}", greetingCounter, message.whom());
        message.replyTo().tell(new Greeter.Greet(message.whom(), getContext().getSelf()));
        return this;
    }
}
