using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using System.Text;
using Beam.Models;

namespace Beam.Downloaders;

public record class UnitDownloaderOptions<OutType> {
    public HttpClient Client { get; init; } = new();

    public DownloadTarget Target { get; init; } = DownloadTarget.URL;
    
    public SkipPredicateOptions<OutType>? SkipPredicateOptions { get; init; }
    public FailurePredicateOptions<ByteDocument>? FailurePredicateOptions { get; init; }
    public FragmentOptions? FragmentOptions { get; init; }
    public required AsyncTransformer<ByteDocument, OutType> AsyncTransformer { get; init; }
    
    /// <summary>
    /// The location where the download is stored.
    /// </summary>
    /// <remarks>
    /// If not defined, <c>UnitDownloader.TryDownload()</c> downloads to memory. 
    /// </remarks>
    public string? DownloadFolder { get; init; } = null;
    public int BufferSize { get; init; } = 80 * 1024; // 80kb

    public bool FollowRedirects { get; init; } = true;

    public string GetFileNameForDownload(string url, byte[] additionalData) {
        byte[] bytes = [..Encoding.UTF8.GetBytes(url), ..additionalData];
        var name = Convert.ToBase64String(System.IO.Hashing.XxHash64.Hash(bytes));
        return name.Replace('+', '-').Replace('/', '_').Replace('=', ' ').Trim();
    }
}

// ---------- UnitDownloaderOptions Builder ----------

// ---------- FailurePredicateOptions Builder ----------

// ---------- FragmentOptions Builder ----------