package net.northking.cctp.debuger.bean;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

import java.util.HashMap;
import java.util.Map;

public class DebugerRequest {
    /**
     * 会话ID
     */
    private String sessionId;
    /**
     * 请求指令
     */
    private String cmd;
    /**
     * 请求数据
     */
    private Map<String,Object> data;

    /**
     * 用户token
     */
    private String userToken;

    public String getUserToken() {
        return userToken;
    }

    public void setUserToken(String userToken) {
        this.userToken = userToken;
    }


    public DebugerRequest sessionId(String sessionId){
        this.sessionId = sessionId;
        return this;
    }
    public DebugerRequest cmd(String cmd){
        this.cmd = cmd;
        return this;
    }
    public DebugerRequest requestId(String requestId){
        withData(DebugerConst.KEY_REQUESTID,requestId);
        return this;
    }
    public DebugerRequest withData(String key,Object value){
        if(null == this.data){
            data = new HashMap<>();
        }
        this.data.put(key,value);
        return this;
    }

    public DebugerRequest withData(Map<String,Object> data){
        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 Object getDataValue(String key){
        if(StringUtils.isBlank(key) || null == data){
            return null;
        }
        return  data.get(key);
    }
    @JsonIgnore
    public String getRequestId(){
        return (String)getDataValue(DebugerConst.KEY_REQUESTID);
    }

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

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("sessionId", sessionId)
                .append("cmd", cmd)
                .append("data", data)
                .toString();
    }

    public static DebugerRequest newRequest(){
        return new DebugerRequest();
    }
}
