package com.atlassian.stash.internal.pull.rescope;

import com.atlassian.bitbucket.pull.PullRequestRef;
import com.atlassian.bitbucket.scm.pull.MinimalPullRequest;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.stash.internal.pull.InternalPullRequest;

import javax.annotation.Nonnull;
import java.util.Date;
import java.util.Objects;

/**
 * @since 4.5
 */
public class SimpleMinimalPullRequest implements MinimalPullRequest {

    private final ApplicationUser author;
    private final long id;
    private final PullRequestRef fromRef;
    private final long globalId;
    private final Date rescopeDate;
    private final PullRequestRef toRef;
    private final int version;

    public SimpleMinimalPullRequest(InternalPullRequest pullRequest) {
        author = pullRequest.getCreatedBy();
        id = pullRequest.getId();
        fromRef = pullRequest.getFromRef();
        globalId = pullRequest.getGlobalId();
        rescopeDate = pullRequest.getRescopedDate();
        toRef = pullRequest.getToRef();
        version = pullRequest.getVersion();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        SimpleMinimalPullRequest that = (SimpleMinimalPullRequest) o;
        return Objects.equals(id, that.id) &&
                Objects.equals(globalId, that.globalId) &&
                Objects.equals(version, that.version);
    }

    @Nonnull
    public ApplicationUser getCreatedBy() {
        return author;
    }

    @Nonnull
    @Override
    public PullRequestRef getFromRef() {
        return fromRef;
    }

    public long getGlobalId() {
        return globalId;
    }

    @Override
    public long getId() {
        return id;
    }

    @Nonnull
    public Date getRescopeDate() {
        return rescopeDate;
    }

    @Nonnull
    @Override
    public PullRequestRef getToRef() {
        return toRef;
    }

    @Override
    public int getVersion() {
        return version;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, globalId, version);
    }

    @Override
    public String toString() {
        return toRef.getRepository() + "#" + id;
    }
}
