/*
 * 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.maven.doxia.module.markdown;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.vladsch.flexmark.ext.wikilink.internal.WikiLinkLinkResolver;
import com.vladsch.flexmark.html.IndependentLinkResolverFactory;
import com.vladsch.flexmark.html.LinkResolver;
import com.vladsch.flexmark.html.renderer.LinkResolverBasicContext;
import com.vladsch.flexmark.html.renderer.LinkStatus;
import com.vladsch.flexmark.html.renderer.LinkType;
import com.vladsch.flexmark.html.renderer.ResolvedLink;
import com.vladsch.flexmark.util.ast.Node;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * The FlexmarkDoxiaLinkResolver rewrites the md, markdown links to html.
 *
 * Sample links it rewrites:
 * - doc.md to doc.html
 * - doc.markdown to doc.html
 * - doc.md#anchor to doc.html#anchor
 * - doc.markdown#anchor to doc.html#anchor
 * - ../doc.markdown#anchor to ../doc.html#anchor
 * - :doc.md to :doc.html
 * - :doc.markdown to :doc.html
 *
 * Sample links it leaves untouched:
 * - http://doc.md
 * - https://doc.markdown
 * - doc.md.badformat
 * - doc.md#bad#format
 * - doc.md#bad.format
 */
public class FlexmarkDoxiaLinkResolver implements LinkResolver {
    final Pattern pattern;

    /**
     * <p>Constructor for FlexmarkDoxiaLinkResolver.</p>
     *
     * @param context a {@link com.vladsch.flexmark.html.renderer.LinkResolverContext} object.
     */
    public FlexmarkDoxiaLinkResolver(@NotNull LinkResolverBasicContext context) {
        this.pattern = Pattern.compile("^(?![^:]+:)((?:\\./)?(?:\\.\\./)*[^\\.]+).(?:"
                + MarkdownParserModule.FILE_EXTENSION
                + "|"
                + MarkdownParserModule.ALTERNATE_FILE_EXTENSION
                + ")(#[^#\\.]*){0,1}$");
    }

    /** {@inheritDoc} */
    @Override
    public @NotNull ResolvedLink resolveLink(
            @NotNull Node node, @NotNull LinkResolverBasicContext context, @NotNull ResolvedLink link) {
        if (link.getLinkType() == LinkType.LINK) {
            Matcher matcher = this.pattern.matcher(link.getUrl());
            if (matcher.matches()) {
                return link.withStatus(LinkStatus.VALID).withUrl(matcher.replaceAll("$1.html$2"));
            }
        }

        return link;
    }

    /**
     * Factory that creates FlexmarkDoxiaLinkResolver objects.
     */
    public static class Factory extends IndependentLinkResolverFactory {
        @Override
        public @Nullable Set<Class<?>> getBeforeDependents() {
            Set<Class<?>> set = new HashSet<>();
            set.add(WikiLinkLinkResolver.Factory.class);
            return set;
        }

        @Override
        public @NotNull LinkResolver apply(@NotNull LinkResolverBasicContext context) {
            return new FlexmarkDoxiaLinkResolver(context);
        }
    }
}
