/*
 * Copyright 2020, 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.client;

import com.google.common.collect.ImmutableList;
import io.spine.annotation.GeneratedMixin;
import io.spine.base.EntityState;
import io.spine.core.Version;
import io.spine.protobuf.AnyPacker;
import io.spine.type.UnexpectedTypeException;

import java.util.List;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.spine.protobuf.AnyPacker.unpack;

/**
 * Extends {@link QueryResponse} with useful methods.
 */
@GeneratedMixin
interface QueryResponseMixin extends QueryResponseOrBuilder {

    /**
     * Obtains the size of the response.
     */
    default int size() {
        int result = getMessageList().size();
        return result;
    }

    /**
     * Verifies if the response is empty.
     */
    default boolean isEmpty() {
        boolean result = size() == 0;
        return result;
    }

    /**
     * Obtains immutable list of entity states returned in this query response.
     */
    default List<? extends EntityState> states() {
        ImmutableList<EntityState> result = getMessageList()
                .stream()
                .map(EntityStateWithVersion::getState)
                .map(AnyPacker::unpack)
                .map(EntityState.class::cast)
                .collect(toImmutableList());
        return result;
    }

    /**
     * Obtains immutable list of the queried entity states of the given type.
     *
     * @throws UnexpectedTypeException if the {@code type} does not match the actual result type
     */
    default <S extends EntityState> List<S> states(Class<S> type)
            throws UnexpectedTypeException {
        ImmutableList<S> result = getMessageList()
                .stream()
                .map(EntityStateWithVersion::getState)
                .map(any -> unpack(any, type))
                .collect(toImmutableList());
        return result;
    }

    /**
     * Obtains entity state at the given index.
     *
     * @throws IndexOutOfBoundsException
     *         if the index is out of the range of entities returned by this query response
     */
    default EntityState state(int index) {
        EntityStateWithVersion stateWithVersion = getMessageList().get(index);
        EntityState result = (EntityState) unpack(stateWithVersion.getState());
        return result;
    }

    /**
     * Obtains immutable list of entity states returned in this query response.
     */
    default List<Version> versions() {
        ImmutableList<Version> result = getMessageList()
                .stream()
                .map(EntityStateWithVersion::getVersion)
                .collect(toImmutableList());
        return result;
    }

    /**
     * Obtains the version of the entity at the given index.
     *
     * @throws IndexOutOfBoundsException
     *         if the index is out of the range of entities returned by this query response
     */
    default Version version(int index) {
        EntityStateWithVersion stateWithVersion = getMessageList().get(index);
        Version result = stateWithVersion.getVersion();
        return result;
    }
}

@@ -1977,24 +1977,33 @@
     default 
+Immutable
 List%3C? exten
@@ -2566,16 +2566,25 @@
 yState%3E 
+Immutable
 List%3CS%3E 

Use `ImmutableList` instead of `List` in API