package com.r11.tools.controller;

import com.google.gson.Gson;
//import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.r11.tools.controller.internal.GlobalControllerManifest;
import com.r11.tools.controller.internal.HandlerType;
import com.r11.tools.controller.internal.RestController;
import com.r11.tools.controller.internal.ScopedControllerManifest;
import spark.Request;
import spark.Response;
import org.apache.logging.log4j.Logger;

@GlobalControllerManifest(path = "/json")
public class JsonController implements RestController {

    private final Logger logger;

    private final Gson prettyGson; 

    private final Gson gson;

    public JsonController(Gson prettyGson, Gson jsongson,Logger logger) {
            this.logger = logger;
            this.prettyGson = prettyGson;
            this.gson = jsongson;
    }

    @ScopedControllerManifest(method = HandlerType.POST, path = "/formatting")
    public void formatting(Request request, Response response) {
        long startProcess = System.currentTimeMillis();
        JsonObject responseJson = new JsonObject();

        try {
            JsonObject requestJson = this.gson.fromJson(request.body(), JsonObject.class);

            response.status(200);

            responseJson.addProperty("data", this.prettyGson.toJson(requestJson));
            responseJson.addProperty("time", System.currentTimeMillis() - startProcess);

            
        } catch (Exception e) {
            this.logger.error("Error on formatting Json " + e);
            Throwable cause = e.getCause();

            response.status(400);

            responseJson.addProperty("data", cause == null ? e.getMessage() : cause.getMessage());
            responseJson.addProperty("time", System.currentTimeMillis() - startProcess);

            
        }   
        this.logger.info("Json processed in " + responseJson.get("time").toString() + " ms.");
            response.body(this.prettyGson.toJson(responseJson));
    }

    @ScopedControllerManifest(method = HandlerType.POST, path = "/minimize")
    public void minimize(Request request, Response response) {
        long startProcess = System.currentTimeMillis();
        JsonObject responseJson = new JsonObject();

        try {
            JsonObject requestJson = this.prettyGson.fromJson(request.body(), JsonObject.class);

            response.status(200);

            responseJson.addProperty("data", this.gson.toJson(requestJson));
            responseJson.addProperty("time", System.currentTimeMillis() - startProcess);

            response.body(this.gson.toJson(responseJson));
        } catch (Exception e) {
            this.logger.error("Error on minimizeing Json " + e);
            Throwable cause = e.getCause();

            response.status(400);

            responseJson.addProperty("data", cause == null ? e.getMessage() : cause.getMessage());
            responseJson.addProperty("time", System.currentTimeMillis() - startProcess);

            response.body(this.prettyGson.toJson(responseJson));
        }
        this.logger.info("Json processed in " + responseJson.get("time").toString() + " ms.");
    }
}
