package cc.smtweb.framework.core.container.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Akmm at 2011-1-12 9:45:04
 * 插件服务方法的返回参数
 */
@SuppressWarnings("UnusedDeclaration")
public class SwReturn {
    /**
     * 是否执行成功
     */
    private boolean isSuccess;
    /**
     * 提示信息列表
     */
    private List<String> messages = null;
    /**
     * 执行结果参数
     */
    private Map<String, Object> map = new HashMap<>();
    private Object singleResult;
    public <T> T  getSingleResult() {
        return (T)singleResult;
    }

    public void setSingleResult(Object singleResult) {
        this.singleResult = singleResult;
    }
    /**
     * @param isSuccess
     * @param message
     */
    public SwReturn(boolean isSuccess, String message) {
        this.isSuccess = isSuccess;
        this.addMessage(message);
    }

    public SwReturn(Integer errCode, String message) {
        this.isSuccess = false;
        this.addMessage(message);
        this.setParameter("errCode", errCode);
    }

    public SwReturn(String errCode, String message) {
        this.isSuccess = false;
        this.addMessage(message);
        this.setParameter("errCode", errCode);
    }

    /**
     */
    public SwReturn() {
        this(true, null);
    }

    /**
     * 设置参数
     *
     * @param name
     * @param value
     * @pdOid 590f34f1-b661-4a64-8dbb-b1c9a4ec931e
     */
    public void setParameter(String name, Object value) {
        map.put(name, value);
    }

    /**
     * 获取参数
     *
     * @param name
     * @pdOid a0df6724-734d-4c5d-b156-e96010436ee8
     */
    @SuppressWarnings({"unchecked"})
    public <T> T getParameter(String name) {
        return (T) map.get(name);
    }

    public boolean containsParameter(String name) {
        return map.containsKey(name);
    }

    /**
     * 是否执行成功
     */
    public boolean isSuccess() {
        return isSuccess;
    }

    /**
     * @param success
     * @pdOid bf4fdbfe-a134-43b7-9496-578fb61ab106
     */
    public void setSuccess(boolean success) {
        isSuccess = success;
    }

    /**
     * 得到提示信息，字符串格式，有多个提示，则以回车符分隔
     */
    public String getMessageStr() {
        if (this.messages == null) return null;
        StringBuilder sb = new StringBuilder(32);
        for (String s : messages) {
            sb.append("\n").append(s);
        }
        return sb.substring(1);
    }

    /**
     * 得到结果参数Map
     */
    public Map<String, Object> getMap() {
        return map;
    }

    /**
     * 增加一条提示信息
     *
     * @param msg
     */
    public void addMessage(String msg) {
        if (this.messages == null)
            this.messages = new ArrayList<>();
        if (msg != null) this.messages.add(msg);
    }

    /**
     * 设置消息
     *
     * @param msg
     */
    public void setMessage(String msg) {
        clearMessage();
        addMessage(msg);
    }

    /**
     * 清空所有提示信息
     */
    public void clearMessage() {
        if (this.messages != null) this.messages.clear();
    }

    /**
     * 得到提示信息列表
     *
     * @return
     */
    public List<String> getMessages() {
        return messages;
    }

    /**
     * 设置为执行失败，且增加错误信息
     *
     * @param errmsg
     */
    public void setError(String errmsg) {
        this.isSuccess = false;
        addMessage(errmsg);
    }
    public int getErrorCode() {
        if (!isSuccess() && containsParameter("errCode")) {
            return (Integer) getParameter("errCode");
        }
        return 0;
    }
}
