package net.northking.cctp.debuger.bean;

import lombok.Data;
import net.northking.cctp.debuger.util.JsonUtils;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;
@Data
public class DebugerResponse {
    private String sessionId;
    private String cmd;
    private Map<String,Object> data;

    public static DebugerResponse newRespone(){
        return new DebugerResponse().cmd(DebugerConst.ReplyConst.KEY_CMD_REPLY);
    }
    public DebugerResponse sessionId(String sessionId){
        this.sessionId = sessionId;
        return  this;
    }
    public DebugerResponse cmd(String cmd){
        this.cmd = cmd;
        return  this;
    }

    public  DebugerResponse withData(String key, Object val){
        if (null == data){
            data = new HashMap<>();
        }
        data.put(key,val);
        return this;
    }
    public  DebugerResponse withResult(String key, String val){
        if (null == data){
            data = new HashMap<>();
        }
        String  ret = data.get(DebugerConst.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(DebugerConst.ReplyConst.KEY_RESULT, JsonUtils.toJson(result));
        return this;
    }
    public  DebugerResponse withResult(Map<String,String> resultData){
        if (null == resultData){
            return this;
        }
        if (null == data){
            data = new HashMap<>();
        }
        String  ret = data.get(DebugerConst.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.putAll(resultData);
        data.put(DebugerConst.ReplyConst.KEY_RESULT, JsonUtils.toJson(result));
        return this;
    }
    public  DebugerResponse requestId(String requestId){
        return withData(DebugerConst.ReplyConst.KEY_REQUESTID,requestId);
    }
    public  DebugerResponse result(String result){
        return withData(DebugerConst.ReplyConst.KEY_RESULT,result);
    }
    public  DebugerResponse msg(String msg){
        return withData(DebugerConst.ReplyConst.KEY_MSG,msg);
    }
    public  DebugerResponse finish(boolean done){
        return withData(DebugerConst.ReplyConst.KEY_FINISH,String.valueOf(done));
    }
    public  DebugerResponse success(boolean success){
        return withData(DebugerConst.ReplyConst.KEY_SUCCESS,String.valueOf(success));
    }

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



}
