package com.atlassian.bitbucket.mesh.git.ref;

import com.atlassian.bitbucket.mesh.git.AbstractParameters;
import com.atlassian.bitbucket.mesh.git.exception.InvalidRefNameException;
import com.atlassian.bitbucket.mesh.repository.Repository;
import com.atlassian.bitbucket.mesh.util.Person;

import javax.annotation.Nonnull;

import static com.atlassian.bitbucket.mesh.rpc.v1.RpcInvalidRefName.Reason.BLANK;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang3.StringUtils.isBlank;

public class AbstractDeleteRefParameters extends AbstractParameters {

    private final Person author;
    private final String name;
    private final Repository repository;

    protected AbstractDeleteRefParameters(AbstractDeleteBuilder<?, ?> builder) {
        super(builder);

        author = requireNonNull(builder.author, "author");
        name = builder.name;
        repository = builder.repository;
    }

    @Nonnull
    public Person getAuthor() {
        return author;
    }

    @Nonnull
    public String getName() {
        return name;
    }

    @Nonnull
    public Repository getRepository() {
        return repository;
    }

    public abstract static class AbstractDeleteBuilder<B extends AbstractDeleteBuilder<B, P>, P extends AbstractDeleteRefParameters>
            extends AbstractBuilder<B, P> {

        private final String name;
        private final Repository repository;

        private Person author;

        protected AbstractDeleteBuilder(@Nonnull Repository repository, @Nonnull String name) {
            this.repository = requireNonNull(repository, "repository");

            if (isBlank(name)) {
                throw new InvalidRefNameException(BLANK, name);
            }
            this.name = name;
        }

        @Nonnull
        public B author(@Nonnull Person value) {
            author = requireNonNull(value, "author");

            return self();
        }
    }
}
