/*
 * Copyright 2014 Google Inc. All rights reserved.
 *
 * 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.errorprone.refaster;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.auto.value.AutoValue;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.io.CharStreams;
import com.google.errorprone.apply.DescriptionBasedDiff;
import com.google.errorprone.apply.SourceFile;
import com.google.testing.compile.JavaFileObjects;

import com.sun.source.tree.CompilationUnitTree;
import com.sun.tools.javac.api.JavacTaskImpl;
import com.sun.tools.javac.api.JavacTool;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;

import java.io.IOException;
import java.util.Locale;

import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;

/**
 * Test helper for {@link CodeTransformer} implementations.
 *
 * @author [email protected] (Louis Wasserman)
 */
@AutoValue
public abstract class CodeTransformerTestHelper {
  public static CodeTransformerTestHelper create(CodeTransformer transformer) {
    return new AutoValue_CodeTransformerTestHelper(transformer);
  }

  abstract CodeTransformer transformer();

  public JavaFileObject transform(JavaFileObject original) {
    JavaCompiler compiler = JavacTool.create();
    DiagnosticCollector<JavaFileObject> diagnosticsCollector =
        new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager =
        compiler.getStandardFileManager(diagnosticsCollector, Locale.ENGLISH, UTF_8);
    JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(CharStreams.nullWriter(),
        fileManager,
        diagnosticsCollector,
        ImmutableList.<String>of(),
        null,
        ImmutableList.of(original));

    try {
      SourceFile sourceFile = SourceFile.create(original);
      Iterable<? extends CompilationUnitTree> trees = task.parse();
      task.analyze();
      JCCompilationUnit tree =
          FluentIterable.from(trees).filter(JCCompilationUnit.class).getOnlyElement();
      DescriptionBasedDiff diff = DescriptionBasedDiff.create(tree);
      transformer().apply(tree, task.getContext(), diff);
      diff.applyDifferences(sourceFile);

      return JavaFileObjects.forSourceString(
          FluentIterable.from(tree.getTypeDecls())
              .filter(JCClassDecl.class)
              .getOnlyElement()
              .sym.getQualifiedName().toString(),
          sourceFile.getSourceText());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}