namespace Beam.Downloaders;

public sealed class FragmentOptionsBuilder {
    private int? _fragmentSize;
    private bool _downloadInParallel = false;
    private int? _parallelThreads = null;

    public FragmentOptionsBuilder WithFragmentSize(int bytes) {
        if (bytes <= 0) throw new System.ArgumentOutOfRangeException(nameof(bytes));
        _fragmentSize = bytes;
        return this;
    }

    public FragmentOptionsBuilder WithDownloadInParallel(bool value = true) {
        _downloadInParallel = value;
        return this;
    }

    public FragmentOptionsBuilder WithParallelThreads(int? threads) {
        if (threads.HasValue && threads.Value <= 0)
            throw new System.ArgumentOutOfRangeException(nameof(threads));
        _parallelThreads = threads;
        return this;
    }

    public FragmentOptions Build() {
        if (!_fragmentSize.HasValue)
            throw new System.InvalidOperationException("FragmentSize must be provided.");

        return new FragmentOptions {
            FragmentSize = _fragmentSize.Value,
            DownloadInParallel = _downloadInParallel,
            ParallelThreads = _parallelThreads
        };
    }
}