/*
 * Copyright (C) 2019 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.processor.internal.aggregateddeps;

import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;
import dagger.hilt.processor.internal.ClassNames;
import java.io.IOException;
import javax.annotation.Generated;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Modifier;

/**
 * Generates a public Dagger module that includes a user's pkg-private module. This allows a user's
 * module to use pkg-private visibility to hide from external packages, but still allows Hilt to
 * install the module when the component is created in another package.
 */
final class PkgPrivateModuleGenerator {
  private final ProcessingEnvironment env;
  private final PkgPrivateMetadata metadata;

  PkgPrivateModuleGenerator(ProcessingEnvironment env, PkgPrivateMetadata metadata) {
    this.env = env;
    this.metadata = metadata;
  }

  // This method creates the following generated code for a pkg-private module, pkg.MyModule:
  //
  // package pkg; //same as module
  //
  // import dagger.Module;
  // import dagger.hilt.InstallIn;
  // import javax.annotation.Generated;
  //
  // @Generated("dagger.hilt.processor.internal.aggregateddeps.PkgPrivateModuleGenerator")
  // @InstallIn(ActivityComponent.class)
  // @Module(includes = MyModule.class)
  // public final class HiltModuleWrapper_MyModule {}
  void generate() throws IOException {

    // generated install_in is exactly the same as the module being processed
    JavaFile.builder(
            metadata.generatedClassName().packageName(),
            TypeSpec.classBuilder(metadata.generatedClassName().simpleName())
                .addOriginatingElement(metadata.getTypeElement())
                .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
                .addAnnotation(
                    AnnotationSpec.builder(Generated.class)
                        .addMember("value", "$S", getClass().getName())
                        .build())
                .addAnnotation(
                    AnnotationSpec.get(metadata.getOptionalInstallInAnnotationMirror().get()))
                .addAnnotation(
                    AnnotationSpec.builder(ClassNames.MODULE)
                        .addMember("includes", "$T.class", metadata.getTypeElement())
                        .build())
                .build())
        .build()
        .writeTo(env.getFiler());
  }
}

@@ -837,62 +837,77 @@
 ort 
-java.io.IOException;%0Aimport javax.annotation.Generated
+dagger.hilt.processor.internal.Processors;%0Aimport java.io.IOException
 ;%0Aim
@@ -2067,165 +2067,38 @@
 n %7B%0A
-%0A
     
-// generated install_in is exactly the same as the module being processed%0A    JavaFile.builder(%0A            metadata.generatedClassName().packageName(),
+TypeSpec.Builder builder =
 %0A   
@@ -2102,20 +2102,16 @@
         
-    
 TypeSpec
@@ -2160,36 +2160,32 @@
 ).simpleName())%0A
-    
             .add
@@ -2238,28 +2238,24 @@
             
-    
 .addModifier
@@ -2305,198 +2305,82 @@
     
-    .addAnnotation(%0A                    AnnotationSpec.builder(Generated.class)%0A                        .addMember(%22value%22, %22$S%22, getClass().getName())%0A                        .build())%0A    
+// generated @InstallIn is exactly the same as the module being processed%0A
     
@@ -2395,36 +2395,32 @@
 .addAnnotation(%0A
-    
                 
@@ -2498,36 +2498,32 @@
 )))%0A            
-    
 .addAnnotation(%0A
@@ -2530,36 +2530,32 @@
                 
-  
-  
 AnnotationSpec.b
@@ -2580,20 +2580,16 @@
 MODULE)%0A
-    
         
@@ -2674,36 +2674,32 @@
                 
-    
 .build())%0A      
@@ -2691,33 +2691,158 @@
 build())
+;%0A
 %0A    
-            
+Processors.addGeneratedAnnotation(builder, env, getClass());%0A%0A    JavaFile.builder(metadata.generatedClassName().packageName(), builder
 .build()

Update PkgPrivateModuleGenerator to use @Generated version based on jdk.