package cn.axzo.workflow.core.common.exception;

import cn.axzo.framework.domain.ServiceException;
import cn.axzo.framework.domain.web.code.IRespCode;
import com.google.common.annotations.VisibleForTesting;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WorkflowEngineException extends ServiceException {
    private String code;

    public WorkflowEngineException(IRespCode code) {
        super(code.getMessage());
        this.code = code.getRespCode();
    }

    public WorkflowEngineException(IRespCode code, String... params) {
        super(doFormat(code.getCode(), code.getMessage(), params));
        this.code = code.getRespCode();
    }

    @Override
    public String getCode() {
        return this.code;
    }

    /**
     * 将错误编号对应的消息使用 params 进行格式化。
     *
     * @param code           错误编号
     * @param messagePattern 消息模版
     * @param params         参数
     * @return 格式化后的提示
     */
    @VisibleForTesting
    private static String doFormat(String code, String messagePattern, Object... params) {
        StringBuilder sbuf = new StringBuilder(messagePattern.length() + 50);
        int i = 0;
        int j;
        int l;
        for (l = 0; l < params.length; l++) {
            j = messagePattern.indexOf("{}", i);
            if (j == -1) {
                log.error("[doFormat][参数过多：错误码({})|错误内容({})|参数({})", code, messagePattern, params);
                if (i == 0) {
                    return messagePattern;
                } else {
                    sbuf.append(messagePattern.substring(i));
                    return sbuf.toString();
                }
            } else {
                sbuf.append(messagePattern, i, j);
                sbuf.append(params[l]);
                i = j + 2;
            }
        }
        if (messagePattern.indexOf("{}", i) != -1) {
            log.error("[doFormat][参数过少：错误码({})|错误内容({})|参数({})", code, messagePattern, params);
        }
        sbuf.append(messagePattern.substring(i));
        return sbuf.toString();
    }
}
