package com.atlassian.bitbucket.mesh.git.builder.tag;

import com.atlassian.bitbucket.mesh.git.ref.Ref;
import com.atlassian.bitbucket.mesh.git.ref.RefManager;
import com.atlassian.bitbucket.mesh.git.ref.ResolveRefsParameters;
import com.atlassian.bitbucket.mesh.git.ref.Tag;
import com.atlassian.bitbucket.mesh.process.handler.AbstractLoggingStdioHandler;
import com.atlassian.bitbucket.mesh.repository.Repository;

public class CreateTagStdioHandler extends AbstractLoggingStdioHandler<Tag> {

    private final RefManager refManager;
    private final String name;
    private final Repository repository;

    public CreateTagStdioHandler(RefManager refManager, String name, Repository repository) {
        this.refManager = refManager;
        this.name = name;
        this.repository = repository;
    }

    @Override
    public Tag getResult() {
        ResolveRefsParameters parameters = new ResolveRefsParameters.Builder(repository)
                .tagId(name)
                .build();
        Ref resolved = refManager.resolveRefs(parameters).get(name);

        return resolved instanceof Tag ? (Tag) resolved : null;
    }
}
