package com.lion.lionwebsite.Util;


import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;

public class Response {
    HashMap<String, String> result;
    public Response(){
        result = new HashMap<>();
    }

    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);
    }

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

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

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

    public void success(String result){
        success();
        setData(result);
    }

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

    public void failure(String result){
        failure();
        setData(result);
    }

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


    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 static String _default(){
        Response response = Response.generateResponse();
        response.failure("参数错误");

        return response.toJSONString();
    }

    public String toJSONString(){
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.valueToTree(result).toString();
    }
}
