import { Handlers } from "$fresh/server.ts";
import { SERVER_STATE } from "../../state/serverState.ts";

export enum ManageAction {
  start = "start",
  stop = "stop",
  restart = "restart",
}

export const handler: Handlers = {
  async POST(req) {
    const body: ManageAction & string = await req.text() as ManageAction;
    switch (body) {
      case ManageAction.start:
        SERVER_STATE.startMCServer();
        return new Response("started");
      case ManageAction.stop:
        SERVER_STATE.gracefullyStopMCServer();
        return new Response("stopped");
      case ManageAction.restart:
        SERVER_STATE.restartMCServer();
        return new Response("restarted");
      default:
        SERVER_STATE.sendStdIn(body);
        return new Response("action done");
    }
  },
  GET() {
    return new Response(SERVER_STATE.status);
  }
};
