package net.northking.cctp.se.debug.bean;

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


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

public class DebuggerRequest {
    /**
     * 会话ID
     */
    private String sessionId;
    /**
     * 请求指令
     */
    private String cmd;

    /**
     * 请求数据
     */
    private Map<String,Object> data;

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

    private String envId;

    public String getUserToken() {
        return userToken;
    }

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

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

    public DebuggerRequest withData(Map<String,Object> data){
        if(null == this.data){
            data = new HashMap<>();
        }
        if (this.data != null) {
            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);
    }
    @JsonIgnore
    public String getProjectId(){
        return null == getDataValue(DebugerConst.KEY_PROJECTID) ? null : (String)getDataValue(DebugerConst.KEY_PROJECTID);
    }

    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 String getEnvId() {
        return envId;
    }

    public void setEnvId(String envId) {
        this.envId = envId;
    }

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