/*
 * 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.core;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.Immutable;
import io.spine.code.proto.MessageType;
import io.spine.code.proto.ref.TypeRef;
import io.spine.type.KnownTypes;

import java.io.Serializable;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Streams.stream;
import static io.spine.core.Events.isRejection;

/**
 * A set of combined event and rejection classes.
 */
@Immutable
public final class EventClassSet implements Serializable {

    private static final long serialVersionUID = 0L;

    private final ImmutableSet<EventClass> eventClasses;
    private final ImmutableSet<RejectionClass> rejectionClasses;

    private EventClassSet(ImmutableSet<EventClass> eventClasses,
                          ImmutableSet<RejectionClass> rejectionClasses) {
        this.eventClasses = eventClasses;
        this.rejectionClasses = rejectionClasses;
    }

    public static EventClassSet empty() {
        return new EventClassSet(ImmutableSet.of(), ImmutableSet.of());
    }

    public static EventClassSet of(ImmutableSet<EventClass> eventClasses,
                                   ImmutableSet<RejectionClass> rejectionClasses) {
        checkNotNull(eventClasses);
        checkNotNull(rejectionClasses);
        return new EventClassSet(ImmutableSet.copyOf(eventClasses),
                                 ImmutableSet.copyOf(rejectionClasses));
    }

    public static EventClassSet parse(TypeRef typeRef) {
        ImmutableSet<MessageType> types = KnownTypes.instance()
                                                    .findAll(typeRef);
        return new EventClassSet(events(types), rejections(types));
    }

    private static ImmutableSet<EventClass> events(ImmutableSet<MessageType> types) {
        return types.stream()
                    .filter(MessageType::isEvent)
                    .map(EventClass::of)
                    .collect(toImmutableSet());
    }

    private static ImmutableSet<RejectionClass> rejections(ImmutableSet<MessageType> types) {
        return types.stream()
                    .filter(MessageType::isRejection)
                    .map(RejectionClass::of)
                    .collect(toImmutableSet());
    }

    public boolean containsAnyOf(Iterable<Event> events) {
        return containsAnyEvent(events) || containsAnyRejection(events);
    }

    private boolean containsAnyEvent(Iterable<Event> events) {
        Optional<EventClass> matchedEvent =
                stream(events)
                        .filter(event -> !isRejection(event))
                        .map(EventClass::from)
                        .filter(eventClasses::contains)
                        .findAny();
        return matchedEvent.isPresent();
    }

    private boolean containsAnyRejection(Iterable<Event> events) {
        Optional<RejectionClass> matchedRejection =
                stream(events)
                        .filter(Events::isRejection)
                        .map(RejectionClass::from)
                        .filter(rejectionClasses::contains)
                        .findAny();
        return matchedRejection.isPresent();
    }
}

@@ -1293,84 +1293,48 @@
 til.
-Optional;%0A%0Aimport static com.google.common.base.Preconditions.checkNotNul
+Collection;%0Aimport java.util.Optiona
 l;%0A
+%0A
 impo
@@ -2173,390 +2173,8 @@
  %7D%0A%0A
-    public static EventClassSet of(ImmutableSet%3CEventClass%3E eventClasses,%0A                                   ImmutableSet%3CRejectionClass%3E rejectionClasses) %7B%0A        checkNotNull(eventClasses);%0A        checkNotNull(rejectionClasses);%0A        return new EventClassSet(ImmutableSet.copyOf(eventClasses),%0A                                 ImmutableSet.copyOf(rejectionClasses));%0A    %7D%0A%0A
     
@@ -2487,28 +2487,26 @@
  events(
-ImmutableSet
+Collection
 %3CMessage
@@ -2755,28 +2755,26 @@
 ections(
-ImmutableSet
+Collection
 %3CMessage

Remove unused factory method in `EventClassSet`