package com.jscape.util.event;

/**
 * @author Alex Usun
 */
public interface Request<H extends Request.RequestHandler> {

    /**
     * @param handler handler
     * @param context context
     *
     * @return is accepted
     *
     * @throws RequestException RequestException
     */
    boolean accept(H handler, EventContext context)
            throws RequestException;

    void onNotHandled()
            throws RequestException;

    interface RequestHandler {
    }

    class RequestException
            extends Exception {

        public final Request request;

        public static RequestException wrap(Request request, Throwable e) {
            return e instanceof RequestException ? (RequestException) e : new RequestException(request, e);
        }

        public RequestException(Request request) {
            this.request = request;
        }

        public RequestException(Request request, String message) {
            super(message);
            this.request = request;
        }

        public RequestException(Request request, String message, Throwable cause) {
            super(message, cause);

            this.request = request;
        }

        public RequestException(Request request, Throwable cause) {
            this(request, cause.getMessage() != null ? cause.getMessage() : cause.toString(), cause);
        }

        @Override
        public String toString() {
            return "RequestException {"
                    + "request=" + this.request
                    + "} " + super.toString();
        }
    }

    class RequestNotHandledException
            extends RequestException {

        public RequestNotHandledException(Request request) {
            super(request);
        }

        @Override
        public String toString() {
            return "RequestNotHandledException {}" + super.toString();
        }
    }

}