package be.iminds.ilabt.jfed.connectivity_tester;

import be.iminds.ilabt.jfed.util.IOUtils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.concurrent.Callable;

/**
 * User: twalcari
 * Date: 12/19/13
 * Time: 4:24 PM
 */
public interface ConnectivityTest extends Callable<ConnectivityTest.ConnectivityTestResult> {


    String getName();

    enum Status {
        SUCCEEDED, WARNING, FAILED, SKIPPED
    }

    boolean isLoginRequired();


    class ConnectivityTestResult {

        private final Status status;
        private final String message;
        private final Throwable exception;

        public ConnectivityTestResult(Status status, String message, Throwable exception) {
            this.status = status;
            this.message = message;
            this.exception = exception;
        }

        public ConnectivityTestResult(Status status, String message) {
            this.status = status;
            this.message = message;
            this.exception = null;
        }

        public ConnectivityTestResult(Status status, Throwable exception) {
            this.status = status;
            this.message = exception.getMessage();
            this.exception = exception;

        }

        public Status getStatus() {
            return status;
        }

        public String getMessage() {
            return message;
        }

        public Throwable getException() {
            return exception;
        }

        public String getExceptionString() {
            if (exception == null)
                return null;

            StringWriter sw = new StringWriter();
            exception.printStackTrace(new PrintWriter(sw));
            return sw.toString();

        }

        @Override
        public String toString() {
            return "ConnectivityTestResult{" +
                    "status=" + status +
                    ", message='" + message + '\'' +
                    ", exception=" + exception +
                    (exception == null ? "" :
                            ", exception_message=" + exception.getMessage() +
                                    ", exception_class=" + exception.getClass().getName() +
                                    ", exception_stacktrace=" + IOUtils.exceptionToStacktraceString(exception)
                    ) +
                    '}';
        }
    }
}
