package com.atlassian.bitbucket.comment;

import com.atlassian.bitbucket.util.BuilderSupport;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Base class for comment creation requests.
 * @deprecated in 4.7 for removal in 5.0.
 * Use one of {@link AbstractAddFileCommentRequest} or {@link AbstractAddLineCommentRequest} instead.
 */
@Deprecated
public abstract class AbstractAddCommentRequest {

    private final String fromHash;
    private final String path;
    private final String srcPath;
    private final String text;
    private final String toHash;

    protected AbstractAddCommentRequest(AbstractBuilder<?, ?> builder) {
        fromHash = StringUtils.trimToNull(builder.fromHash);
        path = checkNotNull(builder.path, "path");
        srcPath = StringUtils.trimToNull(builder.srcPath);
        text = checkNotNull(builder.text, "text");
        toHash = StringUtils.trimToNull(builder.toHash);
    }

    /**
     * @return the {@link DiffCommentAnchor#getFromHash() from hash} to anchor the comment to, or {@code null} to
     *         automatically infer it
     */
    @Nullable
    public String getFromHash() {
        return fromHash;
    }

    /**
     * @return the path of the file to comment on
     */
    @Nonnull
    public String getPath() {
        return path;
    }

    /**
     * @return the path the file to comment on used to have, for copies and moves
     */
    @Nullable
    public String getSrcPath() {
        return srcPath;
    }

    /**
     * @return the comment text
     */
    @Nonnull
    public String getText() {
        return text;
    }

    /**
     * @return the {@link DiffCommentAnchor#getToHash() to hash} to anchor the comment to,
     *         or {@code null} to automatically infer it
     */
    @Nullable
    public String getToHash() {
        return toHash;
    }

    public abstract static class AbstractBuilder<B extends AbstractBuilder<B, R>, R extends AbstractAddCommentRequest>
            extends BuilderSupport {

        private String fromHash;
        private String path;
        private String srcPath;
        private String text;
        private String toHash;

        @Nonnull
        public abstract R build();

        @Nonnull
        public B fromHash(@Nullable String value) {
            fromHash = value;

            return self();
        }

        @Nonnull
        public B path(@Nonnull String value) {
            path = checkNotBlank(value, "path");

            return self();
        }

        @Nonnull
        public B srcPath(@Nullable String value) {
            srcPath = value;

            return self();
        }

        @Nonnull
        public B text(@Nonnull String value) {
            text = checkNotBlank(value, "text");

            return self();
        }

        @Nonnull
        public B toHash(@Nullable String value) {
            toHash = value;

            return self();
        }

        @Nonnull
        protected abstract B self();
    }
}
