/*
 * 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.code.gen.java.field;

import io.spine.code.proto.FieldDeclaration;
import io.spine.type.MessageType;

import java.util.List;

import static com.google.common.collect.Lists.newLinkedList;

final class NestedFieldScanner {

    private final MessageType messageType;

    NestedFieldScanner(MessageType messageType) {
        this.messageType = messageType;
    }

    List<MessageType> scan() {
        List<MessageType> result = newLinkedList();
        int index = -1;
        while (index < result.size()) {
            if (index == -1) {
                addMessageFields(this.messageType, result);
            } else {
                MessageType messageType = result.get(index);
                addMessageFields(messageType, result);
            }
            index++;
        }
        return result;
    }

    private static void addMessageFields(MessageType messageType, List<MessageType> result) {
        messageType.fields()
                   .stream()
                   .filter(FieldSpec::shouldExposeNestedFields)
                   .map(FieldDeclaration::messageType)
                   .filter(type -> !result.contains(type))
                   .forEach(result::add);
    }
}

@@ -1597,16 +1597,24 @@
 eFields(
+result, 
 this.mes
@@ -1617,32 +1617,24 @@
 .messageType
-, result
 );%0A         
@@ -1739,16 +1739,24 @@
 eFields(
+result, 
 messageT
@@ -1762,16 +1762,8 @@
 Type
-, result
 );%0A 
@@ -1877,16 +1877,21 @@
 eFields(
+List%3C
 MessageT
@@ -1885,34 +1885,43 @@
 List%3CMessageType
- m
+%3E result, M
 essageType, List
@@ -1918,16 +1918,10 @@
 Type
-, List%3CM
+ m
 essa
@@ -1926,24 +1926,16 @@
 sageType
-%3E result
 ) %7B%0A    
@@ -2147,23 +2147,16 @@
 -%3E !
-result.
 contains
 (typ
@@ -2151,17 +2151,41 @@
 contains
-(
+TypeWithSameName(result, 
 type))%0A 
@@ -2230,11 +2230,288 @@
 );%0A    %7D
+%0A%0A    private static boolean containsTypeWithSameName(List%3CMessageType%3E result, MessageType type) %7B%0A        return result.stream()%0A                     .map(MessageType::simpleJavaClassName)%0A                     .anyMatch(name -%3E name.equals(type.simpleJavaClassName()));%0A    %7D
 %0A%7D%0A

Update the nested field scan to gather only types with unique names