package yokohama.unit.translator;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import yokohama.unit.ast.Group;
import yokohama.unit.ast.VariableCheckVisitor;
import yokohama.unit.ast_junit.CompilationUnit;
import yokohama.unit.ast_junit.ExpressionStrategy;
import yokohama.unit.ast_junit.MockStrategy;
import yokohama.unit.ast_junit.MockitoMockStrategy;
import yokohama.unit.ast_junit.OgnlExpressionStrategy;
import yokohama.unit.grammar.YokohamaUnitParser.GroupContext;

public class DocyCompilerImpl implements DocyCompiler {
    DocyParser docyParser = new DocyParserImpl();
    ParseTreeToAstVisitor parseTreeToAstVisitor = new ParseTreeToAstVisitor();
    VariableCheckVisitor variableCheckVisitor = new VariableCheckVisitor();
    AstToJUnitAstFactory astToJUnitAstFactory = new AstToJUnitAstFactory();
    ExpressionStrategy expressionStrategy = new OgnlExpressionStrategy();
    MockStrategy mockStrategy = new MockitoMockStrategy();
    JUnitAstCompiler jUnitAstCompiler = new JUnitAstCompilerImpl(expressionStrategy, mockStrategy);

    @Override
    public boolean compile(
            Path docyPath,
            InputStream ins,
            String className,
            String packageName,
            List<String> classPath,
            Optional<Path> dest,
            List<String> javacArgs
    ) throws IOException {
        List<ErrorMessage> errors = new ArrayList<>();

        // Source to ANTLR parse tree
        GroupContext ctx = docyParser.parse(ins, errors);
        if (errors.size() > 0) return false;

        // ANTLR parse tree to AST
        Group ast = parseTreeToAstVisitor.visitGroup(ctx);

        // Check AST
        List<yokohama.unit.ast.ErrorMessage> errorMessages = variableCheckVisitor.check(ast);
        if (errorMessages.size() > 0) {
            for (yokohama.unit.ast.ErrorMessage errorMessage : errorMessages) {
                System.err.println(errorMessage.getSpan() + ": " + errorMessage.getMessage());
            }
            return false;
        }

        // AST to JUnit AST
        CompilationUnit junit = astToJUnitAstFactory.create(Optional.of(docyPath))
                .translate(className, ast, packageName);

        // JUnit AST to Java code
        return jUnitAstCompiler.compile(junit, className, packageName, classPath, dest, javacArgs);
    }
}

@@ -331,215 +331,8 @@
 t;%0D%0A
-import yokohama.unit.ast_junit.ExpressionStrategy;%0D%0Aimport yokohama.unit.ast_junit.MockStrategy;%0D%0Aimport yokohama.unit.ast_junit.MockitoMockStrategy;%0D%0Aimport yokohama.unit.ast_junit.OgnlExpressionStrategy;%0D%0A
 impo
@@ -390,16 +390,16 @@
 ntext;%0D%0A
+
 %0D%0Apublic
@@ -742,225 +742,186 @@
     
-ExpressionStrategy expressionStrategy = new OgnlExpressionStrategy();%0D%0A    MockStrategy mockStrategy = new MockitoMockStrategy();%0D%0A    JUnitAstCompiler jUnitAstCompiler = new JUnitAstCompilerImpl(expressionStrategy, m
+JUnitAstCompiler jUnitAstCompiler = new JUnitAstCompilerImpl(%0D%0A            new yokohama.unit.ast_junit.OgnlExpressionStrategy(),%0D%0A            new yokohama.unit.ast_junit.MockitoM
 ockS
@@ -927,16 +927,18 @@
 Strategy
+()
 );%0D%0A%0D%0A  

Remove ast_junit strategy fields from DocyCompilerImpl