package com.atlassian.bitbucket.mesh.repository;

import com.atlassian.bitbucket.mesh.partition.Partition;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcDataStoreShard;
import com.google.common.collect.ImmutableList;

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

import static java.util.Objects.requireNonNull;

public class ShardInfo {

    private final List<String> partitions;
    private final String path;
    private final long usableSpace;

    private ShardInfo(Builder builder) {
        path = builder.path;
        partitions = builder.builder.build();
        usableSpace = builder.usableSpace;
    }

    @Nonnull
    public List<String> getPartitions() {
        return partitions;
    }

    public RpcDataStoreShard toShard() {
        return RpcDataStoreShard.newBuilder()
                .addAllPartitions(partitions)
                .setPath(path)
                .setUsableSpace(usableSpace)
                .build();
    }

    public static class Builder {

        private final ImmutableList.Builder<String> builder;
        private final String path;
        private final long usableSpace;

        public Builder(@Nonnull String path, long usableSpace) {
            this.path = requireNonNull(path, "path");
            this.usableSpace = usableSpace;
            builder = ImmutableList.builder();
        }

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

        @Nonnull
        public Builder partitions(@Nonnull List<Partition> partitions) {
            requireNonNull(partitions, "partitions");
            partitions.forEach(part -> builder.add(part.getHex()));
            return this;
        }
    }
}
