package com.atlassian.stash.internal.project;

import com.atlassian.bitbucket.project.Project;
import com.atlassian.bitbucket.project.ProjectType;
import com.atlassian.bitbucket.project.ProjectVisitor;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;
import javax.persistence.*;

/**
 * @since 2.4
 */
@DiscriminatorValue("0")
@Entity
@PrimaryKeyJoinColumn(name = "project_id", referencedColumnName = "id")
@Table(name = InternalNormalProject.TABLE)
public class InternalNormalProject extends InternalProject {

    public static final String TABLE = "sta_normal_project";

    @Column(name = "is_public", nullable = false)
    private final boolean publiclyAccessible;

    protected InternalNormalProject() {
        publiclyAccessible = false;
    }

    protected InternalNormalProject(Integer id, String namespace, String key, String name, String description, boolean publiclyAccessible) {
        super(id, namespace, key, name, description, ProjectType.NORMAL);

        this.publiclyAccessible = publiclyAccessible;
    }

    @Override
    public <T> T accept(@Nonnull ProjectVisitor<T> visitor) {
        return visitor.visit(this);
    }

    public Builder copy() {
        return new Builder(this);
    }

    @Override
    public boolean isPublic() {
        return publiclyAccessible;
    }

    public static class Builder extends AbstractBuilder<Builder> {

        private String description;
        private String key;
        private String name;
        private boolean publiclyAccessible;

        public Builder() {
        }

        public Builder(@Nonnull Project project) {
            super(project);

            this.description = project.getDescription();
            this.key = project.getKey();
            this.name = project.getName();
            this.publiclyAccessible = project.isPublic();
        }

        @Nonnull
        public InternalNormalProject build() {
            return new InternalNormalProject(id, namespace, key, name, description, publiclyAccessible);
        }

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

            return self();
        }

        @Nonnull
        public Builder key(String value) {
            key = value;

            return self();
        }

        @Nonnull
        public Builder name(String value) {
            name = value;

            return self();
        }

        @Nonnull
        public Builder publiclyAccessible(boolean value) {
            publiclyAccessible = value;

            return self();
        }

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