package net.northking.cctp.mobile.entity;

import net.northking.cctp.mobile.constants.MobileConnectionConstants;
import net.northking.cctp.mobile.util.JsonUtils;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class WSResponse implements Serializable {
    private String sessionId;
    private String cmd;
    private Map<String,Object> data;

    public static WSResponse buildResponse(String cmd, String sessionId,Map<String,Object> data) {
        return WSResponse.newResponse()
                .cmd(cmd)
                .sessionId(sessionId)
                .withData(data);
    }

    public static WSResponse buildResponse(String sessionId, UpperWSResponse response){
        return WSResponse.newResponse()
                .sessionId(sessionId)
                .requestId(response.getRequestId())
                .finish(response.isFinish())
                .success(response.isSuccess())
                .withResult(response.getData())
                .msg(response.getMessage());
    }

    public static WSResponse buildResponse(WSRequest request,boolean finish,boolean success,String msg){
        return WSResponse.newResponse()
                .sessionId(request.getSessionId())
                .requestId(request.getRequestId())
                .finish(finish)
                .success(success)
                .msg(msg);
    }

    public static WSResponse buildResponse(WSRequest request,boolean finish,boolean success,Map<String,Object> data,String msg){
        return WSResponse.newResponse()
                .sessionId(request.getSessionId())
                .requestId(request.getRequestId())
                .finish(finish)
                .success(success)
                .withData(data)
                .msg(msg);
    }

    public static WSResponse newResponse(){
        return new WSResponse().cmd(MobileConnectionConstants.ReplyConst.KEY_CMD_REPLY);
    }
    public WSResponse sessionId(String sessionId){
        this.sessionId = sessionId;
        return  this;
    }
    public WSResponse cmd(String cmd){
        this.cmd = cmd;
        return  this;
    }

    public WSResponse withData(String key, Object val){
        if (null == data){
            data = new HashMap<>();
        }
        data.put(key,val);
        return this;
    }
    public WSResponse withResult(String key, String val){
        if (null == data){
            data = new HashMap<>();
        }
        String  ret = data.get(MobileConnectionConstants.ReplyConst.KEY_RESULT).toString();
        Map<String,String> result = null;
        if(!StringUtils.isEmpty(ret)){
            result = JsonUtils.readMap(ret,String.class);
        }
        if (null == result){
            result  = new HashMap<>();
        }
        result.put(key,val);
        data.put(MobileConnectionConstants.ReplyConst.KEY_RESULT, JsonUtils.toJson(result));
        return this;
    }

    public WSResponse withResult(Map<String,Object> resultData){
        if (null == resultData){
            return this;
        }
        if (null == data){
            data = new HashMap<>();
        }
        //已封装result，无需封装
        if (resultData.containsKey(MobileConnectionConstants.ReplyConst.KEY_RESULT)) {
            data.putAll(resultData);
        }else {
            data.put(MobileConnectionConstants.ReplyConst.KEY_RESULT, resultData);
        }
        return this;
    }
    public WSResponse requestId(String requestId){
        return withData(MobileConnectionConstants.ReplyConst.KEY_REQUEST_ID,requestId);
    }
    public WSResponse result(String result){
        return withData(MobileConnectionConstants.ReplyConst.KEY_RESULT,result);
    }
    public WSResponse msg(String msg){
        return withData(MobileConnectionConstants.ReplyConst.KEY_MSG,msg);
    }
    public WSResponse finish(boolean done){
        return withData(MobileConnectionConstants.ReplyConst.KEY_FINISH,done);
    }
    public WSResponse success(boolean success){
        return withData(MobileConnectionConstants.ReplyConst.KEY_SUCCESS,success);
    }

    public WSResponse withData(Map<String,Object> data){
        if (null == data){
            return this;
        }
        if (null == this.data){
            this.data = new HashMap<>();
        }
        this.data.putAll(data);
        return this;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public String getCmd() {
        return cmd;
    }

    public void setCmd(String cmd) {
        this.cmd = cmd;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }
}
