/*
 * Copyright (C) 2018 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.internal.codegen;

import dagger.Module;
import dagger.Provides;
import dagger.internal.codegen.SourceFileGeneratorsModule.ComponentModule;
import dagger.internal.codegen.SourceFileGeneratorsModule.MembersInjectionModule;
import dagger.internal.codegen.SourceFileGeneratorsModule.ProductionModule;
import dagger.internal.codegen.SourceFileGeneratorsModule.ProvisionModule;
import dagger.internal.codegen.base.SourceFileGenerator;
import dagger.internal.codegen.binding.BindingGraph;
import dagger.internal.codegen.binding.MembersInjectionBinding;
import dagger.internal.codegen.binding.ProductionBinding;
import dagger.internal.codegen.binding.ProvisionBinding;
import dagger.internal.codegen.compileroption.CompilerOptions;
import dagger.internal.codegen.writing.FactoryGenerator;
import dagger.internal.codegen.writing.HjarSourceFileGenerator;
import dagger.internal.codegen.writing.MembersInjectorGenerator;
import dagger.internal.codegen.writing.ModuleConstructorProxyGenerator;
import dagger.internal.codegen.writing.ModuleGenerator;
import dagger.internal.codegen.writing.ProducerFactoryGenerator;
import javax.lang.model.element.TypeElement;

@Module(
    includes = {
      ProvisionModule.class,
      ProductionModule.class,
      MembersInjectionModule.class,
      ComponentModule.class
    })
interface SourceFileGeneratorsModule {
  @Module
  abstract class GeneratorModule<T, G extends SourceFileGenerator<T>> {
    @Provides
    SourceFileGenerator<T> generator(G generator, CompilerOptions compilerOptions) {
      return compilerOptions.headerCompilation()
          ? HjarSourceFileGenerator.wrap(generator)
          : generator;
    }
  }

  @Module
  class ProvisionModule extends GeneratorModule<ProvisionBinding, FactoryGenerator> {}

  @Module
  class ProductionModule extends GeneratorModule<ProductionBinding, ProducerFactoryGenerator> {}

  @Module
  class MembersInjectionModule
      extends GeneratorModule<MembersInjectionBinding, MembersInjectorGenerator> {}

  @Module
  class ComponentModule extends GeneratorModule<BindingGraph, ComponentGenerator> {}

  // the abstract module is not available because we're using a qualifier
  @Provides
  @ModuleGenerator
  static SourceFileGenerator<TypeElement> generator(
      ModuleConstructorProxyGenerator generator, CompilerOptions compilerOptions) {
    return compilerOptions.headerCompilation()
        ? HjarSourceFileGenerator.wrap(generator)
        : generator;
  }
}

@@ -681,316 +681,8 @@
 es;%0A
-import dagger.internal.codegen.SourceFileGeneratorsModule.ComponentModule;%0Aimport dagger.internal.codegen.SourceFileGeneratorsModule.MembersInjectionModule;%0Aimport dagger.internal.codegen.SourceFileGeneratorsModule.ProductionModule;%0Aimport dagger.internal.codegen.SourceFileGeneratorsModule.ProvisionModule;%0A
 impo
@@ -1465,166 +1465,23 @@
 dule
-(%0A    includes = %7B%0A      ProvisionModule.class,%0A      ProductionModule.class,%0A      MembersInjectionModule.class,%0A      ComponentModule.class%0A    %7D)%0Ainterface
+%0Aabstract class
  Sou
@@ -1510,140 +1510,117 @@
 e %7B%0A
+%0A
   @
-Module%0A  abstract class GeneratorModule%3CT, G extends SourceFileGenerator%3CT%3E%3E %7B%0A    @Provides%0A    SourceFileGenerator%3CT%3E g
+Provides%0A  static SourceFileGenerator%3CProvisionBinding%3E provisionBindingGenerator(%0A      FactoryG
 enerator
 (G g
@@ -1619,10 +1619,8 @@
 ator
-(G
  gen
@@ -1670,126 +1670,147 @@
     
-  
 return 
-compilerOptions.headerCompilation()%0A          ? HjarSourceFileGenerator.wrap(generator)%0A          : g
+hjarWrapper(generator, compilerOptions);%0A  %7D%0A%0A  @Provides%0A  static SourceFileGenerator%3CProductionBinding%3E productionBindingG
 enerator
 ;%0A  
@@ -1809,395 +1809,409 @@
 ator
-;
+(
 %0A    
-%7D%0A  %7D%0A%0A  @Module%0A  class ProvisionModule extends GeneratorModule%3CProvisionBinding, FactoryGenerator%3E %7B%7D%0A%0A  @Module%0A  class ProductionModule extends GeneratorModule%3CProductionBinding, ProducerFactoryGenerator%3E %7B%7D%0A%0A  @Module%0A  class MembersInjectionModule%0A      extends GeneratorModule%3CMembersInjectionBinding, MembersInjectorGenerator%3E %7B%7D%0A%0A  @Module%0A  class ComponentModule extends 
+  ProducerFactoryGenerator generator, CompilerOptions compilerOptions) %7B%0A    return hjarWrapper(generator, compilerOptions);%0A  %7D%0A%0A  @Provides%0A  static SourceFileGenerator%3CMembersInjectionBinding%3E membersInjectionBindingGenerator(%0A      MembersInjectorGenerator generator, CompilerOptions compilerOptions) %7B%0A    return hjarWrapper(generator, compilerOptions);%0A  %7D%0A%0A  @Provides%0A  static SourceFile
 Gene
@@ -2215,22 +2215,16 @@
 enerator
-Module
 %3CBinding
@@ -2232,107 +2232,160 @@
 raph
-, ComponentGenerator%3E %7B%7D%0A%0A  // the abstract module is not available because we're using a qualifier
+%3E bindingGraphGenerator(%0A      ComponentGenerator generator, CompilerOptions compilerOptions) %7B%0A    return hjarWrapper(generator, compilerOptions);%0A  %7D%0A
 %0A  @
@@ -2454,17 +2454,28 @@
 lement%3E 
-g
+moduleProxyG
 enerator
@@ -2552,32 +2552,221 @@
 pilerOptions) %7B%0A
+    return hjarWrapper(generator, compilerOptions);%0A  %7D%0A%0A  private static %3CT%3E SourceFileGenerator%3CT%3E hjarWrapper(%0A      SourceFileGenerator%3CT%3E generator, CompilerOptions compilerOptions) %7B%0A
     return compi

Refactor SourceFileGeneratorsModule to remove inner modules.