/*
 * Copyright 2019, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.server.model.declare;

import com.google.common.collect.ImmutableSet;
import com.google.protobuf.Message;
import io.spine.base.CommandMessage;
import io.spine.base.EventMessage;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;

/**
 * A wrapper around method's return type which knows how to get emitted messages (events and
 * commands) of the method.
 */
public enum ReturnType {

    VOID(void.class) {
        @Override
        protected ImmutableSet<Class<? extends Message>> emittedMessages(Method method) {
            return ImmutableSet.of();
        }
    },

    COMMAND_MESSAGE(CommandMessage.class) {
        @Override
        protected ImmutableSet<Class<? extends Message>> emittedMessages(Method method) {
            return null;
        }
    },

    EVENT_MESSAGE(EventMessage.class) {
        @Override
        protected ImmutableSet<Class<? extends Message>> emittedMessages(Method method) {
            return null;
        }
    },

    OPTIONAL(Optional.class) {
        @Override
        protected ImmutableSet<Class<? extends Message>> emittedMessages(Method method) {
            return null;
        }
    },

    ITERABLE(Iterable.class) {
        @Override
        protected ImmutableSet<Class<? extends Message>> emittedMessages(Method method) {
            return null;
        }
    };

    private final Class<?> returnType;

    ReturnType(Class<?> type) {
        returnType = type;
    }

    boolean matches(Class<?> methodReturnType) {
        return returnType.isAssignableFrom(methodReturnType);
    }

    protected abstract ImmutableSet<Class<? extends Message>> emittedMessages(Method method);

    static Optional<ReturnType> findMatching(Method method, Collection<ReturnType> types) {
        Class<?> returnType = method.getReturnType();
        Optional<ReturnType> result = types
                .stream()
                .filter(type -> type.matches(returnType))
                .findFirst();
        return result;
    }
}

@@ -1200,16 +1200,54 @@
 Message;
+%0Aimport io.spine.server.model.Nothing;
 %0A%0Aimport
@@ -1277,33 +1277,8 @@
 od;%0A
-import java.util.Arrays;%0A
 impo
@@ -1668,24 +1668,217 @@
   %7D%0A    %7D,%0A%0A
+    NOTHING(Nothing.class) %7B%0A        @Override%0A        protected ImmutableSet%3CClass%3C? extends Message%3E%3E emittedMessages(Method method) %7B%0A            return ImmutableSet.of();%0A        %7D%0A    %7D,%0A%0A
     COMMAND_
@@ -2028,36 +2028,49 @@
          return 
-null
+ImmutableSet.of()
 ;%0A        %7D%0A    
@@ -2101,32 +2101,41 @@
 entMessage.class
+, NOTHING
 ) %7B%0A        @Ove
@@ -2241,36 +2241,49 @@
          return 
-null
+ImmutableSet.of()
 ;%0A        %7D%0A    
@@ -2436,36 +2436,49 @@
          return 
-null
+ImmutableSet.of()
 ;%0A        %7D%0A    
@@ -2643,12 +2643,25 @@
 urn 
-null
+ImmutableSet.of()
 ;%0A  
@@ -2714,16 +2714,73 @@
 urnType;
+%0A    private final ImmutableSet%3CReturnType%3E specialCases;
 %0A%0A    Re
@@ -2801,16 +2801,44 @@
 %3C?%3E type
+, ReturnType... specialCases
 ) %7B%0A    
@@ -2864,22 +2864,93 @@
 pe;%0A    
-%7D%0A%0A
    
+ this.specialCases = ImmutableSet.copyOf(specialCases);%0A    %7D%0A%0A    private
  boolean
@@ -3293,24 +3293,368 @@
 turnType();%0A
+        Optional%3CReturnType%3E matchingSpecialCase = types%0A                .stream()%0A                .flatMap(type -%3E type.specialCases.stream())%0A                .filter(specialCase -%3E specialCase.matches(returnType))%0A                .findFirst();%0A%0A        if (matchingSpecialCase.isPresent()) %7B%0A            return matchingSpecialCase;%0A        %7D%0A
         Opti

Add special case `Nothing` to event message return type