package dev.dinauer.maven.maven.core;

import dev.dinauer.maven.maven.core.model.*;
import dev.dinauer.maven.maven.core.parser.MavenMetadataUrlParser;
import dev.dinauer.maven.maven.core.parser.MavenUrlParser;
import org.apache.commons.io.FilenameUtils;

import java.nio.file.Path;

public class MavenContextProvider
{
    public static MavenContext parse(String path)
    {
        ExtensionParser.Result result = ExtensionParser.parse(Path.of(path).getFileName().toString());
        switch (result.ext())
        {
            case JAR, POM ->
            {
                MavenUrlParser parser = MavenUrlParser.parse(path);
                String artifact = parser.artifactId();
                Version version = parser.version();
                return new MavenContext(path, parser.groupId(), artifact, version, FileParser.parse(artifact, version.getRaw(), Path.of(path).getFileName().toString()));
            }
            case XML ->
            {
                MavenMetadataUrlParser parser = MavenMetadataUrlParser.parse(path);
                return new MavenContext(path, parser.groupId(), parser.artifactId(), null, new File().setBase("maven-metadata").setType(result.ext()).setHash(result.hashExt()));
            }
        }
        throw new RuntimeException();
    }
}
