/*
 * Copyright (C) 2020 The Dagger Authors.
 *
 * 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 dagger.hilt.android.processor.internal.bindvalue;

import static com.google.common.base.CaseFormat.LOWER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static java.util.Comparator.comparing;

import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeSpec;
import dagger.Module;
import dagger.Provides;
import dagger.hilt.android.processor.internal.bindvalue.BindValueMetadata.BindValueElement;
import dagger.hilt.processor.internal.ClassNames;
import dagger.hilt.processor.internal.Components;
import dagger.hilt.processor.internal.Processors;
import dagger.multibindings.ElementsIntoSet;
import dagger.multibindings.IntoMap;
import dagger.multibindings.IntoSet;
import java.io.IOException;
import javax.annotation.Generated;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Modifier;

/**
 * Generates a SINGLETON module for all {@code BindValue} annotated fields in a test class.
 */
final class BindValueGenerator {
  private static final String SUFFIX = "_BindValueModule";

  private final ProcessingEnvironment env;
  private final BindValueMetadata metadata;
  private final ClassName testClassName;
  private final ClassName className;

  BindValueGenerator(ProcessingEnvironment env, BindValueMetadata metadata) {
    this.env = env;
    this.metadata = metadata;
    testClassName = ClassName.get(metadata.testElement());
    className = Processors.append(testClassName, SUFFIX);
  }

  //  @Module
  //  @InstallIn(ApplicationComponent.class)
  //  public final class FooTest_BindValueModule {
  //     // providesMethods ...
  //  }
  void generate() throws IOException {
    TypeSpec.Builder builder =
        TypeSpec.classBuilder(className)
            .addOriginatingElement(metadata.testElement())
            .addAnnotation(Processors.getOriginatingElementAnnotation(metadata.testElement()))
            .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
            .addAnnotation(
                AnnotationSpec.builder(Generated.class)
                    .addMember("value", "$S", getClass().getName())
                    .build())
            .addAnnotation(Module.class)
            .addAnnotation(
                Components.getInstallInAnnotationSpec(
                    ImmutableSet.of(ClassNames.APPLICATION_COMPONENT)))
            .addMethod(providesTestMethod());

    metadata.bindValueElements().stream()
        .map(this::providesMethod)
        .sorted(comparing(MethodSpec::toString))
        .forEachOrdered(builder::addMethod);

    JavaFile.builder(className.packageName(), builder.build())
        .build()
        .writeTo(env.getFiler());
  }

  // @Provides
  // static FooTest providesFooTest(@ApplicationContext Context context) {
  //   return (FooTest)
  //       ((TestApplicationComponentManager)
  //           ((TestApplicationComponentManagerHolder) context).componentManager())
  //               .getTestInstance();
  // }
  private MethodSpec providesTestMethod() {
    String methodName = "provides" + testClassName.simpleName();
    MethodSpec.Builder builder =
        MethodSpec.methodBuilder(methodName)
            .addAnnotation(Provides.class)
            .addModifiers(Modifier.STATIC)
            .addParameter(
                ParameterSpec.builder(ClassNames.CONTEXT, "context")
                    .addAnnotation(ClassNames.APPLICATION_CONTEXT)
                    .build())
            .returns(testClassName)
            .addStatement(
                "return ($T) (($T) (($T) context).componentManager()).getTestInstance()",
                testClassName,
                ClassNames.TEST_APPLICATION_COMPONENT_MANAGER,
                ClassNames.TEST_APPLICATION_COMPONENT_MANAGER_HOLDER);
    return builder.build();
  }

  // @Provides
  // @BarQualifier
  // static Bar providesBarQualifierBar(FooTest test) {
  //   return test.bar;
  // }
  private MethodSpec providesMethod(BindValueElement bindValue) {
    // We only allow fields in the Test class, which should have unique variable names.
    String methodName = "provides"
        + LOWER_CAMEL.to(UPPER_CAMEL, bindValue.variableElement().getSimpleName().toString());
    MethodSpec.Builder builder =
        MethodSpec.methodBuilder(methodName)
            .addAnnotation(Provides.class)
            .addModifiers(Modifier.STATIC)
            .addParameter(testClassName, "test")
            .returns(ClassName.get(bindValue.variableElement().asType()))
            .addStatement("return test.$L", bindValue.variableElement().getSimpleName());

    ClassName annotationClassName = bindValue.annotationName();
    if (BindValueMetadata.BIND_VALUE_INTO_MAP_ANNOTATIONS.contains(annotationClassName)) {
      builder.addAnnotation(IntoMap.class);
      // It is safe to call get() on the Optional<AnnotationMirror> returned by mapKey()
      // because a @BindValueIntoMap is required to have one and is checked in
      // BindValueMetadata.BindValueElement.create().
      builder.addAnnotation(AnnotationSpec.get(bindValue.mapKey().get()));
    } else if (BindValueMetadata.BIND_VALUE_INTO_SET_ANNOTATIONS.contains(annotationClassName)) {
      builder.addAnnotation(IntoSet.class);
    } else if (BindValueMetadata.BIND_ELEMENTS_INTO_SET_ANNOTATIONS.contains(annotationClassName)) {
      builder.addAnnotation(ElementsIntoSet.class);
    }
    bindValue.qualifier().ifPresent(q -> builder.addAnnotation(AnnotationSpec.get(q)));
    return builder.build();
  }
}

@@ -5184,46 +5184,179 @@
    .
-addParameter(testClassName, %22test%22)%0A  
+returns(ClassName.get(bindValue.variableElement().asType()));%0A%0A    if (bindValue.variableElement().getModifiers().contains(Modifier.STATIC)) %7B%0A      builder.addStatement(%0A
     
@@ -5357,33 +5357,44 @@
 (%0A          
-.
+%22
 return
-s(
+ $T.$L%22, test
 ClassName.ge
@@ -5382,37 +5382,34 @@
 %22, testClassName
-.get(
+, 
 bindValue.variab
@@ -5424,21 +5424,100 @@
 t().
-asTyp
+getSimpleNam
 e())
-)
+;
 %0A  
+  %7D else %7B%0A      builder%0A          .addParameter(testClassName, %22test%22)%0A
     
@@ -5595,24 +5595,30 @@
 mpleName());
+%0A    %7D
 %0A%0A    ClassN

Stop BindValueGenerator referencing static members via instances.