package com.lion.lionwebsite.Util;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;


public class Response {
    ObjectNode result;
    public Response(){
        result = CustomUtil.objectMapper.createObjectNode();
    }

    public static Response generateResponse(){
        return new Response();
    }

    public void set(String key, String value){
        result.put(key, value);
    }

    public String get(String key){
        return result.get(key).asText();
    }

    public void setData(String data){
        result.put("data", data);
    }

    public void setResult(String result){
        this.result.put("result", result);
    }

    public Response success(){
        setResult("success");
        return this;
    }

    public Response success(String result){
        success();
        setData(result);
        return this;
    }
    public Response success(JsonNode jsonNode){
        success();
        this.result.set("data", jsonNode);
        return this;
    }

    public void failure(){
        setResult("failure");
    }

    public Response failure(String result){
        failure();
        setData(result);
        return this;
    }

    public String getResult(){
        return result.get("data").asText();
    }

    public boolean isSuccess(){
        return result.get("result").asText().equals("success");
    }


    public static String _failure(String result){
        Response response = generateResponse();
        response.failure(result);
        return response.toJSONString();
    }

    public static String _success(String result){
        Response response = generateResponse();
        response.success(result);
        return response.toJSONString();
    }

    public static String _success(){
        Response response = generateResponse();
        response.success();
        return response.toJSONString();
    }

    public String toJSONString(){
        return result.toString();
    }
}
