/*
 * Copyright 2016, TeamDev Ltd. 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 org.spine3.server.event.enrich;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.spine3.type.TypeName;

import java.util.Map;
import java.util.Properties;
import java.util.Set;

import static org.spine3.io.IoUtil.loadAllProperties;

/**
 * A map from an event enrichment Protobuf type name to the corresponding event to enrich type name.
 * Is needed for event message enrichment.
 *
 * @author Alexander Litus
 */
/* package */ class EventEnrichmentsMap {

    /**
     * A path to the file which contains enrichment and event Protobuf type names.
     * Is generated by Gradle during the build process.
     */
    private static final String PROPS_FILE_PATH = "enrichments.properties";

    /**
     * A map from an event enrichment Protobuf type name to the corresponding event to enrich type name.
     *
     * <p>Example:
     * <p>{@code proto.type.MyEventEnrichment} - {@code proto.type.MyEvent}
     */
    private static final ImmutableMap<TypeName, TypeName> enrichmentsMap = buildEnrichmentsMap();

    private EventEnrichmentsMap() {}

    /**
     * Returns the map instance.
     */
    @SuppressWarnings("ReturnOfCollectionOrArrayField") // is immutable
    public static Map<TypeName, TypeName> getInstance() {
        return enrichmentsMap;
    }

    private static ImmutableMap<TypeName, TypeName> buildEnrichmentsMap() {
        final ImmutableMap.Builder<TypeName, TypeName> builder = ImmutableMap.builder();
        final ImmutableSet<Properties> propertiesSet = loadAllProperties(PROPS_FILE_PATH);
        for (Properties properties : propertiesSet) {
            putTo(builder, properties);
        }
        final ImmutableMap<TypeName, TypeName> result = builder.build();
        return result;
    }

    private static void putTo(ImmutableMap.Builder<TypeName, TypeName> enrichmentsMap, Properties properties) {
        final Set<String> enrichments = properties.stringPropertyNames();
        for (String enrichment : enrichments) {
            final String eventType = properties.getProperty(enrichment);
            enrichmentsMap.put(TypeName.of(enrichment), TypeName.of(eventType));
        }
    }
}

@@ -1183,30 +1183,8 @@
 e;%0A%0A
-import java.util.Map;%0A
 impo
@@ -1208,16 +1208,16 @@
 erties;%0A
+
 import j
@@ -1368,77 +1368,130 @@
 ing 
-event to enrich type name.%0A * Is needed for event message enrichment.
+type name of an event to enrich.%0A *%0A * %3Cp%3EExample:%0A * %3Cp%3E%7B@code proto.type.MyEventEnrichment%7D - %7B@code proto.type.MyEvent%7D
 %0A *%0A
@@ -1800,231 +1800,8 @@
 %22;%0A%0A
-    /**%0A     * A map from an event enrichment Protobuf type name to the corresponding event to enrich type name.%0A     *%0A     * %3Cp%3EExample:%0A     * %3Cp%3E%7B@code proto.type.MyEventEnrichment%7D - %7B@code proto.type.MyEvent%7D%0A     */%0A
     
@@ -1960,16 +1960,26 @@
 rns the 
+immutable 
 map inst
@@ -2072,22 +2072,38 @@
     
-public static 
+/* package */ static Immutable
 Map%3C

Improve docs.