/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.template.soy.shared.internal;

import com.google.common.collect.ImmutableMap;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.multibindings.Multibinder;
import com.google.template.soy.shared.restricted.ApiCallScopeBindingAnnotations.LocaleString;
import com.google.template.soy.shared.restricted.SoyFunction;
import com.google.template.soy.shared.restricted.SoyPrintDirective;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import javax.inject.Singleton;

/**
 * Guice module for users that want to contribute plugins to Soy via Guice.
 *
 * <p>Important: Do not use outside of Soy code (treat as superpackage-private).
 *
 */
public final class SharedModule extends AbstractModule {

  public SharedModule() {}

  @Override
  protected void configure() {
    // Create empty multibinders so we can inject user-supplied ones.
    Multibinder.newSetBinder(binder(), SoyFunction.class);
    Multibinder.newSetBinder(binder(), SoyPrintDirective.class);

    bind(SoyScopedData.class).toInstance(new SoySimpleScope());
  }

  // Unused by Soy, but provided because user plugins currently inject this.
  @Provides
  @LocaleString
  String provideLocaleString(SoyScopedData data) {
    return data.getLocale();
  }

  /**
   * Builds and provides the map of all installed SoyFunctions (name to function).
   *
   * @param soyFunctionsSet The installed set of SoyFunctions (from Guice Multibinder).
   */
  @Provides
  @Singleton
  ImmutableMap<String, ? extends SoyFunction> provideSoyFunctionsMap(
      Set<SoyFunction> soyFunctionsSet) {
    Map<String, SoyFunction> mapBuilder = new LinkedHashMap<>();
    for (SoyFunction function : soyFunctionsSet) {
      SoyFunction old = mapBuilder.put(function.getName(), function);
      if (old != null && !old.getClass().getName().equals(function.getClass().getName())) {
        throw new IllegalStateException(
            "Found two functions with the same name: '"
                + function.getName()
                + "' and different implementations: "
                + function
                + " vs "
                + old);
      }
    }
    return ImmutableMap.copyOf(mapBuilder);
  }

  /**
   * Builds and provides the map of all installed SoyPrintDirectives (name to directive).
   *
   * @param soyDirectivesSet The installed set of SoyPrintDirectives (from Guice Multibinder).
   */
  @Provides
  @Singleton
  ImmutableMap<String, ? extends SoyPrintDirective> provideSoyDirectivesMap(
      Set<SoyPrintDirective> soyDirectivesSet) {
    Map<String, SoyPrintDirective> mapBuilder = new LinkedHashMap<>();
    for (SoyPrintDirective directive : soyDirectivesSet) {
      SoyPrintDirective old = mapBuilder.put(directive.getName(), directive);
      if (old != null && !old.getClass().getName().equals(directive.getClass().getName())) {
        throw new IllegalStateException(
            "Found two print directives with the same name: '"
                + directive.getName()
                + "' and different implementations: "
                + directive
                + " vs "
                + old);
      }
    }
    return ImmutableMap.copyOf(mapBuilder);
  }

  @Override
  public boolean equals(Object other) {
    return other != null && this.getClass().equals(other.getClass());
  }

  @Override
  public int hashCode() {
    return this.getClass().hashCode();
  }
}

@@ -643,55 +643,8 @@
 l;%0A%0A
-import com.google.common.collect.ImmutableMap;%0A
 impo
@@ -680,16 +680,16 @@
 Module;%0A
+
 import c
@@ -994,115 +994,8 @@
 ive;
-%0Aimport java.util.LinkedHashMap;%0Aimport java.util.Map;%0Aimport java.util.Set;%0Aimport javax.inject.Singleton;
 %0A%0A/*
@@ -1487,16 +1487,124 @@
 lass);%0A%0A
+    // TODO(user): Once LocaleString is dead, eliminate this and construct it directly in%0A    // SoyFileSet%0A
     bind
@@ -1816,24 +1816,24 @@
 ata data) %7B%0A
+
     return d
@@ -1858,1928 +1858,8 @@
  %7D%0A%0A
-  /**%0A   * Builds and provides the map of all installed SoyFunctions (name to function).%0A   *%0A   * @param soyFunctionsSet The installed set of SoyFunctions (from Guice Multibinder).%0A   */%0A  @Provides%0A  @Singleton%0A  ImmutableMap%3CString, ? extends SoyFunction%3E provideSoyFunctionsMap(%0A      Set%3CSoyFunction%3E soyFunctionsSet) %7B%0A    Map%3CString, SoyFunction%3E mapBuilder = new LinkedHashMap%3C%3E();%0A    for (SoyFunction function : soyFunctionsSet) %7B%0A      SoyFunction old = mapBuilder.put(function.getName(), function);%0A      if (old != null && !old.getClass().getName().equals(function.getClass().getName())) %7B%0A        throw new IllegalStateException(%0A            %22Found two functions with the same name: '%22%0A                + function.getName()%0A                + %22' and different implementations: %22%0A                + function%0A                + %22 vs %22%0A                + old);%0A      %7D%0A    %7D%0A    return ImmutableMap.copyOf(mapBuilder);%0A  %7D%0A%0A  /**%0A   * Builds and provides the map of all installed SoyPrintDirectives (name to directive).%0A   *%0A   * @param soyDirectivesSet The installed set of SoyPrintDirectives (from Guice Multibinder).%0A   */%0A  @Provides%0A  @Singleton%0A  ImmutableMap%3CString, ? extends SoyPrintDirective%3E provideSoyDirectivesMap(%0A      Set%3CSoyPrintDirective%3E soyDirectivesSet) %7B%0A    Map%3CString, SoyPrintDirective%3E mapBuilder = new LinkedHashMap%3C%3E();%0A    for (SoyPrintDirective directive : soyDirectivesSet) %7B%0A      SoyPrintDirective old = mapBuilder.put(directive.getName(), directive);%0A      if (old != null && !old.getClass().getName().equals(directive.getClass().getName())) %7B%0A        throw new IllegalStateException(%0A            %22Found two print directives with the same name: '%22%0A                + directive.getName()%0A                + %22' and different implementations: %22%0A                + directive%0A                + %22 vs %22%0A                + old);%0A      %7D%0A    %7D%0A    return ImmutableMap.copyOf(mapBuilder);%0A  %7D%0A%0A
   @O

Delete the ImmutableMap bindings for soy plugins