/*
 * 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.apache.camel.upgrade.camel41;

import org.apache.camel.upgrade.AbstractCamelJavaVisitor;
import org.apache.camel.upgrade.RecipesUtil;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

import java.util.regex.Pattern;

/**
 * Recipe migrating changes between Camel 4.3 to 4.4, for more details see the
 * <a href="https://camel.apache.org/manual/camel-4x-upgrade-guide-4_4.html#_camel_core" >documentation</a>.
 */
public class CamelCoreRecipe extends Recipe {
    private static final String AWS2_URL_WITH_QUEUE_REGEXP = "(aws2-sns://[a-zA-z]+?.*)queueUrl=https://(.+)";
    private static final Pattern AWS2_URL_WITH_QUEUE_URL = Pattern.compile(AWS2_URL_WITH_QUEUE_REGEXP);

    @Override
    public String getDisplayName() {
        return "Camel Core changes";
    }

    @Override
    public String getDescription() {
        return "Apache Camel Core migration from version 4.0 to 4.1.";
    }

    @Override
    public TreeVisitor<?, ExecutionContext> getVisitor() {

        return RecipesUtil.newVisitor(new AbstractCamelJavaVisitor() {
            @Override
            public J.Literal doVisitLiteral(J.Literal literal, ExecutionContext ctx) {
                J.Literal l = super.doVisitLiteral(literal, ctx);

                //is it possible to precondition that aws2 is present?
                if (JavaType.Primitive.String == l.getType() &&
                        AWS2_URL_WITH_QUEUE_URL.matcher((String) l.getValue()).matches()) {
                    String newUrl
                            = ((String) l.getValue()).replaceFirst(AWS2_URL_WITH_QUEUE_REGEXP, "$1queueArn=arn:aws:sqs:$2");
                    l = RecipesUtil.createStringLiteral(newUrl);
                }

                return l;
            }
        });
    }
}
