using Device;
using EEMSClientCore;
using ServiceBase;
using Universal;

namespace TestSignalRClient;

internal class Program
{
    static void Main(string[] args)
    {
        IClientCaller caller = new ClientCaller();
        IClientProvider provider = new ClientProvider(caller);
        HubBase hubSender = new();
        hubSender.Initialize(provider);
        hubSender.Open("localhost", 50054, "ClientHub");

        DeviceInfo deviceInfo = new()
        {

        };

        Guid guid = caller.RegisterDevice(deviceInfo).Result;
        Console.WriteLine(guid);
        Thread.Sleep(-1);
    }
}
public class ClientProvider(IClientCaller clientCaller) : IClientProvider
{
    Task<bool> IClientProvider.RequestFile(Guid guid, FileType fileType)
    {
        Console.WriteLine($"RequestFile {guid} {fileType}");
        using MemoryStream stream = new();
        Compressor.CompressZipFileDirectory(new(@"E:\Recipes"), stream);
        return SendFile(guid, stream);
    }

    private async Task<bool> SendFile(Guid guid, MemoryStream stream)
    {
        if (!stream.Split(out List<byte[]>? buffer, 8192) || buffer is null)
            return false;

        for (int i = 0; i < buffer.Count; i++)
        {
            if (!await clientCaller.FilePack(guid, buffer[i], i + 1, buffer.Count))
                return false;
        }
        return true;
    }
}
