package be.iminds.ilabt.jfed.lowlevel;

/**
 * RetrySettings
 */
public class RetrySettings {
    public static final int DEFAULT_TIMEOUT_MAX_RETRIES = 3;
    public static final int DEFAULT_TIMEOUT_MS_BEFORE_RETRY = 5000;
    /**
     *  changed to 100, see FEDIBBTDEV-894
     *  was 25: at least 2 minutes (24 * 5s wait + calls themselves)
     */
    public static final int DEFAULT_BUSY_MAX_RETRIES = 100;
    public static final int DEFAULT_BUSY_MS_BEFORE_RETRY = 5000;
    public static final int DEFAULT_POSSIBLE_TEMPORARY_ERROR_MAX_RETRIES = 1;
    public static final int DEFAULT_POSSIBLE_TEMPORARY_ERROR_MS_BEFORE_RETRY = 1000;
    private final int timeoutMaxRetries;
    private final long timeoutMsBeforeRetry;

    private final int busyMaxRetries;
    private final long busyMsBeforeRetry;

    private final int possibleTemporaryErrorMaxRetries;
    private final long possibleTemporaryErrorMsBeforeRetry;

    public static RetrySettings getNoRetrySettings() { return new RetrySettings(0, 0, 0, 0, 0, 0); }

    public RetrySettings() {
        this.timeoutMaxRetries = DEFAULT_TIMEOUT_MAX_RETRIES;
        this.timeoutMsBeforeRetry = DEFAULT_TIMEOUT_MS_BEFORE_RETRY;

        this.busyMaxRetries = DEFAULT_BUSY_MAX_RETRIES;
        this.busyMsBeforeRetry = DEFAULT_BUSY_MS_BEFORE_RETRY;

        this.possibleTemporaryErrorMaxRetries = DEFAULT_POSSIBLE_TEMPORARY_ERROR_MAX_RETRIES; //only retry once
        this.possibleTemporaryErrorMsBeforeRetry = DEFAULT_POSSIBLE_TEMPORARY_ERROR_MS_BEFORE_RETRY; //wait 1 second
    }

    public RetrySettings(int timeoutMaxRetries, long timeoutMsBeforeRetry,
                         int busyMaxRetries, long busyMsBeforeRetry,
                         int possibleTemporaryErrorMaxRetries, long possibleTemporaryErrorMsBeforeRetry) {
        this.timeoutMaxRetries = timeoutMaxRetries;
        this.timeoutMsBeforeRetry = timeoutMsBeforeRetry;
        this.busyMaxRetries = busyMaxRetries;
        this.busyMsBeforeRetry = busyMsBeforeRetry;

        this.possibleTemporaryErrorMaxRetries = possibleTemporaryErrorMaxRetries;
        this.possibleTemporaryErrorMsBeforeRetry = possibleTemporaryErrorMsBeforeRetry;
    }

    public int getTimeoutMaxRetries() {
        return timeoutMaxRetries;
    }

    public boolean isAutoRetryOnTimeout() {
        return timeoutMaxRetries > 0;
    }

    public int getBusyMaxRetries() {
        return busyMaxRetries;
    }

    public long getTimeoutMsBeforeRetry() {
        return timeoutMsBeforeRetry;
    }

    public long getBusyMsBeforeRetry() {
        return busyMsBeforeRetry;
    }

    public int getPossibleTemporaryErrorMaxRetries() {
        return possibleTemporaryErrorMaxRetries;
    }

    public long getPossibleTemporaryErrorMsBeforeRetry() {
        return possibleTemporaryErrorMsBeforeRetry;
    }

    @Override
    public String toString() {
        return "RetrySettings{" +
                "timeoutMaxRetries=" + timeoutMaxRetries +
                ", timeoutMsBeforeRetry=" + timeoutMsBeforeRetry +
                ", busyMaxRetries=" + busyMaxRetries +
                ", busyMsBeforeRetry=" + busyMsBeforeRetry +
                ", possibleTemporaryErrorMaxRetries=" + possibleTemporaryErrorMaxRetries +
                ", possibleTemporaryErrorMsBeforeRetry=" + possibleTemporaryErrorMsBeforeRetry +
                '}';
    }
}
