package cz.hsrs.rest.provider;

import io.senslog.core.ApplicationInfo;
import net.sf.json.JSONObject;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import java.util.concurrent.TimeUnit;

import static io.senslog.core.ApplicationInfo.appVersion;
import static io.senslog.core.ApplicationInfo.buildVersion;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

@Path("/info")
public class InfoRest {

    @GET @Produces(APPLICATION_JSON)
    public Response getOverallInfo() {
        long uptimeMillis = ApplicationInfo.uptime();
        String uptime = String.format("%02d min %02d sec",
                TimeUnit.MILLISECONDS.toMinutes(uptimeMillis),
                TimeUnit.MILLISECONDS.toSeconds(uptimeMillis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(uptimeMillis))
        );
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("uptime", uptime);
        jsonObject.put("appVersion", appVersion());
        jsonObject.put("buildVersion", buildVersion());

        return Response
                .status(200)
                .entity(jsonObject.toString())
                .build();
    }
}