package com.fri.http.rest.response;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import net.sf.json.JSON;
import org.omg.CORBA.Any;

/**
 * Created by zhangdahui on 4/27/17.
 */


public class YXJSONResponse {

    public long ServerProcessingDur = 0;
    public long ServerProcessingFinishStamp = 0;

    public boolean Error = false;
    public String ReturnCode = "PASS";
    public String ReturnMsg = "PASS_DESC";

    private long start_stamp = 0;

    public JSON RespData = null;

    public YXJSONResponse() {
        this.start_stamp = System.currentTimeMillis();
    }

    public YXJSONResponse outPutSuccess(){
        this.ServerProcessingFinishStamp = System.currentTimeMillis();
        this.ServerProcessingDur = this.ServerProcessingFinishStamp - this.start_stamp;
        return this;
    }

    public YXJSONResponse outPutSuccess(HashMap dataMap){
        this.ServerProcessingFinishStamp = System.currentTimeMillis();
        this.ServerProcessingDur = this.ServerProcessingFinishStamp - this.start_stamp;
        this.RespData = JSONObject.fromObject(dataMap);
        return this;
    }

    public YXJSONResponse outPutSuccess(ArrayList dataList){
        this.ServerProcessingFinishStamp = System.currentTimeMillis();
        this.ServerProcessingDur = this.ServerProcessingFinishStamp - this.start_stamp;
        this.Error = false;
        this.RespData = JSONArray.fromObject(dataList);
        return this;
    }

    public YXJSONResponse outPutSuccess(JSON json){
        this.ServerProcessingFinishStamp = System.currentTimeMillis();
        this.ServerProcessingDur = this.ServerProcessingFinishStamp - this.start_stamp;
        this.Error = false;
        this.RespData = json;
        return this;
    }

    public YXJSONResponse outPutSuccess(String string){
        this.ServerProcessingFinishStamp = System.currentTimeMillis();
        this.ServerProcessingDur = this.ServerProcessingFinishStamp - this.start_stamp;
        this.Error = false;
        HashMap map = new HashMap<String, Any>();
        map.put("string", string);
        this.RespData = JSONObject.fromObject(map);
        return this;
    }

    public YXJSONResponse outPutError(String returnCode, String returnMsg){
        this.ServerProcessingFinishStamp = System.currentTimeMillis();
        this.ServerProcessingDur = this.ServerProcessingFinishStamp - this.start_stamp;
        this.ReturnCode = returnCode;
        this.ReturnMsg = returnMsg;
        this.Error = true;
        return this;
    }

    public JSONObject toJSON(){
        HashMap map = new HashMap<String, Any>();
        map.put("ServerProcessingDur", this.ServerProcessingDur);
        map.put("ServerProcessingFinishStamp", this.ServerProcessingFinishStamp);
        map.put("ReturnCode", this.ReturnCode);
        map.put("ReturnMsg", this.ReturnMsg);
        map.put("Error", this.Error);
        map.put("respData", this.RespData);
        return JSONObject.fromObject(map);
    }

    public String toJSONString(){
        return this.toJSON().toString();
    }


}
