package net.northking.cctp.element.internal.protocol.socket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.northking.cctp.element.internal.protocol.ResponseData;

/**
 * SocketResponseData
 *
 * @author fengxiang.zhang
 * @since 1.0
 */
public class SocketResponseData implements ResponseData {

    private String responseBody;
    private long time;

    public SocketResponseData body(String body) {
        this.responseBody = body;
        return this;
    }

    public SocketResponseData time(long time) {
        this.time = time;
        return this;
    }

    @Override
    public String getResponseHeaders() {
        return null;
    }

    @Override
    public String getResponseBody() {
        return this.responseBody;
    }

    @Override
    public Object toJson() {
        JSONObject response = new JSONObject(true);
        Object body = this.responseBody;
        try {
            body = JSON.parse(body.toString());
        } catch (Exception e) {
        }
        response.put("context", body);
        response.put("time", time);
        return response;
    }

    @Override
    public Long getResponseTime() {
        return this.time;
    }
}
