/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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 org.codehaus.groovy.transform;

import groovy.transform.CompilationUnitAware;
import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.AnnotatedNode;
import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ClassCodeExpressionTransformer;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.PropertyNode;
import org.codehaus.groovy.ast.expr.ClosureExpression;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.SwitchExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.ListExpression;
import org.codehaus.groovy.ast.expr.PropertyExpression;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.control.CompilationUnit;
import org.codehaus.groovy.control.CompilePhase;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.syntax.SyntaxException;
import org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport;
import org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor;
import org.codehaus.groovy.transform.stc.StaticTypesMarker;

import java.util.Collections;
import java.util.Map;

import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.propX;

/**
 * Handles the implementation of the {@link groovy.transform.TypeChecked} transformation.
 */
@GroovyASTTransformation(phase = CompilePhase.INSTRUCTION_SELECTION)
public class StaticTypesTransformation implements ASTTransformation, CompilationUnitAware {

    public static final String STATIC_ERROR_PREFIX = "[Static type checking] - ";
    protected CompilationUnit compilationUnit;

    @Override
    public void visit(ASTNode[] nodes, SourceUnit source) {
        AnnotationNode annotationInformation = (AnnotationNode) nodes[0];
        Map<String,Expression> members = annotationInformation.getMembers();
        Expression extensions = members.get("extensions");
        AnnotatedNode node = (AnnotatedNode) nodes[1];
        StaticTypeCheckingVisitor visitor = null;
        if (node instanceof ClassNode classNode) {
            visitor = newVisitor(source, classNode);
            visitor.setCompilationUnit(compilationUnit);
            addTypeCheckingExtensions(visitor, extensions);
            visitor.initialize();
            visitor.visitClass(classNode);
        } else if (node instanceof MethodNode methodNode) {
            visitor = newVisitor(source, methodNode.getDeclaringClass());
            visitor.setCompilationUnit(compilationUnit);
            addTypeCheckingExtensions(visitor, extensions);
            visitor.setMethodsToBeVisited(Collections.singleton(methodNode));
            visitor.initialize();
            visitor.visitMethod(methodNode);
        } else if (!(node instanceof PropertyNode)) {
            source.addError(new SyntaxException(STATIC_ERROR_PREFIX + "Unimplemented node type",
                    node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()));
        }
        if (visitor != null) {
            visitor.performSecondPass();
            resolveEnumConstantsInSwitchCases(node, source);
        }
    }

    /**
     * GROOVY-8444, GROOVY-11614, GROOVY-12190: replaces "CONST" expressions resolved by the
     * type checker in enum switch case labels with "EnumType.CONST" expressions;
     * without static compilation they would otherwise be dynamic property lookups
     * on the enclosing class, failing at runtime.
     */
    private static void resolveEnumConstantsInSwitchCases(final AnnotatedNode node, final SourceUnit source) {
        ClassCodeExpressionTransformer transformer = new ClassCodeExpressionTransformer() {
            @Override
            protected SourceUnit getSourceUnit() {
                return source;
            }

            @Override
            public Expression transform(final Expression expression) {
                if (expression instanceof SwitchExpression se) {
                    se.visit(this);
                    return se;
                }
                if (expression instanceof ClosureExpression) {
                    expression.visit(this);
                    return expression;
                }
                if (expression instanceof VariableExpression) {
                    ClassNode enumType = expression.getNodeMetaData(StaticTypesMarker.SWITCH_CONDITION_EXPRESSION_TYPE);
                    if (enumType != null) {
                        PropertyExpression pe = propX(classX(enumType), expression.getText());
                        setSourcePosition(pe, expression);
                        return pe;
                    }
                    return expression;
                }
                return super.transform(expression);
            }
        };
        if (node instanceof ClassNode classNode) {
            transformer.visitClass(classNode);
        } else if (node instanceof MethodNode methodNode) {
            transformer.visitMethod(methodNode);
        }
    }

    protected void addTypeCheckingExtensions(StaticTypeCheckingVisitor visitor, Expression extensions) {
        if (extensions instanceof ConstantExpression) {
            visitor.addTypeCheckingExtension(new GroovyTypeCheckingExtensionSupport(
                    visitor,
                    extensions.getText(),
                    compilationUnit
            ));
        } else if (extensions instanceof ListExpression list) {
            for (Expression ext : list.getExpressions()) {
                addTypeCheckingExtensions(visitor, ext);
            }
        }
    }

    /**
     * Allows subclasses to provide their own visitor. This is useful for example for transformations relying
     * on the static type checker.
     *
     *
     * @param unit the source unit
     * @param node the current classnode
     * @return a static type checking visitor
     */
    protected StaticTypeCheckingVisitor newVisitor(SourceUnit unit, ClassNode node) {
        return new StaticTypeCheckingVisitor(unit, node);
    }

    @Override
    public void setCompilationUnit(final CompilationUnit unit) {
        this.compilationUnit = unit;
    }
}
