package dev.dinauer.maven.maven.core.release.parser;

import dev.dinauer.maven.maven.shared.Parser;
import dev.dinauer.maven.maven.shared.ParserResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Strings;

public class ReleaseFileParser implements Parser<ReleaseFile>
{
    private final String artifactId;
    private final String version;

    public ReleaseFileParser(String artifactId, String version)
    {
        this.version = version;
        this.artifactId = artifactId;
    }

    @Override
    public ParserResult<ReleaseFile> parse(String input)
    {
        String withoutGroupAndArtifact = dropArtifactIdAndVersion(input);
        if (StringUtils.isBlank(withoutGroupAndArtifact))
        {
            return new ParserResult<>(null, new ReleaseFile());
        }
        return new ParserResult<>(null, new ReleaseFile().setClassifier(withoutGroupAndArtifact));
    }

    private String dropArtifactIdAndVersion(String input)
    {
        if (input.equals(String.format("%s-%s", artifactId, version)))
        {
            return "";
        }
        return Strings.CI.removeStart(input, String.format("%s-%s-", artifactId, version));
    }
}