package com.atlassian.bitbucket.internal.mirroring.upstream;

import com.atlassian.bitbucket.mirroring.upstream.MirrorServer;

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

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

public class SimpleMirrorServer implements MirrorServer {
    private final String baseUrl;
    private final boolean enabled;
    private final String id;
    private final Date lastSeenDate;
    private final String name;
    private final String productType;
    private final String productVersion;

    private SimpleMirrorServer(Builder builder) {
        baseUrl = checkNotNull(builder.baseUrl, "baseUrl");
        id = checkNotNull(builder.id, "id");
        lastSeenDate = checkNotNull(builder.lastSeenDate, "lastSeenDate");
        name = checkNotNull(builder.name, "name");
        productType = checkNotNull(builder.productType, "productType");
        productVersion = checkNotNull(builder.productVersion, "productVersion");
        enabled = checkNotNull(builder.enabled, "enabled");
    }

    @Nonnull
    @Override
    public String getBaseUrl() {
        return baseUrl;
    }

    @Nonnull
    @Override
    public String getId() {
        return id;
    }

    @Nonnull
    @Override
    public Date getLastSeenDate() {
        return lastSeenDate;
    }

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

    @Nonnull
    @Override
    public String getProductType() {
        return productType;
    }

    @Nonnull
    @Override
    public String getProductVersion() {
        return productVersion;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public static class Builder {
        private String baseUrl;
        private boolean enabled;
        private String id;
        private Date lastSeenDate;
        private String name;
        private String productType;
        private String productVersion;

        public Builder() {
        }

        public Builder(MirrorServer other) {
            baseUrl = other.getBaseUrl();
            enabled = other.isEnabled();
            id = other.getId();
            lastSeenDate = other.getLastSeenDate();
            name = other.getName();
            productType = other.getProductType();
            productVersion = other.getProductVersion();
        }

        public Builder baseUrl(String value) {
            baseUrl = checkNotNull(value, "baseUrl");
            return this;
        }

        public SimpleMirrorServer build() {
            return new SimpleMirrorServer(this);
        }

        public Builder mirror(String id, String name) {
            this.id = checkNotNull(id, "id");
            this.name = checkNotNull(name, "name");
            return this;
        }

        public Builder lastSeenDate(Date value) {
            lastSeenDate = checkNotNull(value, "lastSeenDate");
            return this;
        }

        public Builder product(String type, String version) {
            productType = checkNotNull(type, "type");
            productVersion = checkNotNull(version, "version");
            return this;
        }

        public Builder enabled(boolean value) {
            enabled = value;
            return this;
        }
    }
}
