package io.muoncore.example.event.guide;

import io.muoncore.Muon;
import io.muoncore.MuonBuilder;
import io.muoncore.config.AutoConfiguration;
import io.muoncore.config.MuonConfigBuilder;
import io.muoncore.protocol.event.Event;
import io.muoncore.protocol.event.client.DefaultEventClient;
import io.muoncore.protocol.event.client.EventClient;
import io.muoncore.protocol.event.client.EventReplayMode;
import reactor.rx.broadcast.Broadcaster;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class UserAggregationService {

    public static void main(String[] args) {

        AutoConfiguration config = MuonConfigBuilder.withServiceIdentifier("user-service").build();

        Muon muon = MuonBuilder.withConfig(config).build();

        muon.getDiscovery().blockUntilReady();

        EventClient evclient = new DefaultEventClient(muon);

        Set<String> usernames = new HashSet<>();

        Broadcaster<Event<Map>> sub = Broadcaster.create();
        sub.consume(msg -> {
            try {
                if (msg.getEventType().equals("UserRegistered")) {
                    usernames.add((String) msg.getPayload().get("username"));
                } else {
                    usernames.remove(msg.getPayload().get("username"));
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            System.out.println("Processed event, user list is now : " + usernames);
        });

        evclient.replay("users", EventReplayMode.REPLAY_THEN_LIVE, Map.class, sub);

    }

    static class UserRegisteredEvent {
        private String username;
        private String firstname;
        private String lastname;

        public UserRegisteredEvent(String username, String firstname, String lastname) {
            this.username = username;
            this.firstname = firstname;
            this.lastname = lastname;
        }

        public String getUsername() {
            return username;
        }

        public String getFirstname() {
            return firstname;
        }

        public String getLastname() {
            return lastname;
        }
    }

    static class UserDeletedEvent {
        private String username;

        public UserDeletedEvent(String username) {
            this.username = username;
        }

        public String getUsername() {
            return username;
        }
    }
}
@@ -1563,858 +1563,6 @@
    %7D
-%0A%0A    static class UserRegisteredEvent %7B%0A        private String username;%0A        private String firstname;%0A        private String lastname;%0A%0A        public UserRegisteredEvent(String username, String firstname, String lastname) %7B%0A            this.username = username;%0A            this.firstname = firstname;%0A            this.lastname = lastname;%0A        %7D%0A%0A        public String getUsername() %7B%0A            return username;%0A        %7D%0A%0A        public String getFirstname() %7B%0A            return firstname;%0A        %7D%0A%0A        public String getLastname() %7B%0A            return lastname;%0A        %7D%0A    %7D%0A%0A    static class UserDeletedEvent %7B%0A        private String username;%0A%0A        public UserDeletedEvent(String username) %7B%0A            this.username = username;%0A        %7D%0A%0A        public String getUsername() %7B%0A            return username;%0A        %7D%0A    %7D
 %0A%7D

Remove unused types