package com.atlassian.bitbucket.test.rest.commit;

import com.atlassian.bitbucket.test.rest.RestPagedRequest;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;

public class RestChangesRequest extends RestPagedRequest {

    private final Long pullRequestId;
    private final String since;
    private final Boolean withComments;

    private RestChangesRequest(Builder builder) {
        super(builder);

        pullRequestId = builder.pullRequestId;
        since = builder.since;
        withComments = builder.withComments;
    }

    @Nullable
    public Long getPullRequestId() {
        return pullRequestId;
    }

    @Nullable
    public String getSince() {
        return since;
    }

    @Nullable
    public Boolean getWithComments() {
        return withComments;
    }

    @Nonnull
    @Override
    public Map<String, Object> toQueryParams() {
        Map<String, Object> params = super.toQueryParams();
        if (pullRequestId != null) {
            params.put("pullRequestId", pullRequestId);
        }
        if (since != null) {
            params.put("since", since);
        }
        if (withComments != null) {
            params.put("withComments", withComments);
        }

        return params;
    }

    public static class Builder extends AbstractPagedRequestBuilder<Builder, RestChangesRequest> {

        private Long pullRequestId;
        private String since;
        private Boolean withComments;

        public Builder() {
        }

        public Builder(@Nonnull RestChangesRequest request) {
            super(request);
        }

        @Nonnull
        @Override
        public RestChangesRequest build() {
            return new RestChangesRequest(this);
        }

        @Nonnull
        public Builder pullRequestId(@Nullable Long value) {
            pullRequestId = value;

            return self();
        }

        @Nonnull
        public Builder since(@Nullable String value) {
            since = StringUtils.trimToNull(value);

            return self();
        }

        @Nonnull
        public Builder withComments(@Nullable Boolean value) {
            withComments = value;

            return self();
        }

        @Nonnull
        @Override
        protected Builder self() {
            return this;
        }
    }
}
