package net.northking.cctp.mobile.entity;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

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

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

    public UpperWSRequest requestId(String requestId){
        this.requestId = requestId;
        return this;
    }

    public UpperWSRequest cmd(String cmd){
        this.cmd = cmd;
        return this;
    }

    public UpperWSRequest serial(String serial){
        this.serial = serial;
        return this;
    }

    public UpperWSRequest 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 UpperWSRequest withData(String key, Object val){
        if (null == data){
            data = new HashMap<>();
        }
        data.put(key,val);
        return this;
    }

    public static UpperWSRequest buildRequest(String serial,String cmd,String requestId){
        return UpperWSRequest.newRequest()
                .serial(serial)
                .cmd(cmd)
                .requestId(requestId);
    }

    public static UpperWSRequest buildRequest(String serial,String cmd,String requestId,Map<String,Object> data){
        return UpperWSRequest.newRequest()
                .serial(serial)
                .cmd(cmd)
                .requestId(requestId)
                .withData(data);
    }

    public String getSerial() {
        return serial;
    }

    public void setSerial(String serial) {
        this.serial = serial;
    }

    public String getCmd() {
        return cmd;
    }

    public Object getDataValue(String key){
        if(StringUtils.isBlank(key) || null == data){
            return null;
        }
        return  data.get(key);
    }

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

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

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

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