package leigh.esp.eko;

import leigh.eo.EO;
import leigh.eo.json.JsonOutput;
import leigh.esp.kabe.Service;
import leigh.mecha.http.server.WebTransaction;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;

import java.io.IOException;
import java.time.Instant;

public class StreamingTestService implements Service {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(StreamingTestService.class);

    @Override
    public void handleClientRequest(WebTransaction clientRequest) throws IOException, ClassNotFoundException {
        clientRequest.httpServletResponse.setContentType("application/json");
        try (JsonOutput out = new JsonOutput(clientRequest.httpServletResponse.getOutputStream())) {
            int i = 0;
            while (true) {
                EO msg = new EO();
                msg.setValue("sequence", i);
                logger.info("Writing msg [msg: {}]", msg);
                out.writeEO(msg);
                out.flush();
            }
        }
    }

    @Override
    public Instant lastKnownGood() {
        return Instant.now();
    }

    @Override
    public String getPath() {
        return "/stream";
    }
}
