using Inspectron.Epson.PrintServer.PrintServices;
using Inspectron.Epson.Queue;
using Microsoft.Extensions.Logging;

namespace Inspectron.Epson.PrintServer;

public class PrintLoop
{
    private readonly global::Inspectron.Epson.Queue.PrintServer _printServer;
    private readonly IPrintService _printService;
    private readonly IPrintJobSource _jobSource;
    
    private readonly ILogger _logger;
    private CancellationTokenSource? _cancellationSource;
    private CancellationToken _cancellationToken;

    public PrintLoop(global::Inspectron.Epson.Queue.PrintServer printServer,IPrintService printService, IPrintJobSource jobSource, ILogger logger)
    {
        _printServer = printServer;
        _printService = printService;
        _jobSource = jobSource;
        
        _logger = logger;
    }

    public Task StartAsync()
    {
        _cancellationSource = new CancellationTokenSource();
        _cancellationToken = _cancellationSource.Token;
        _= Task.Run(Loop);
        return Task.CompletedTask;
    }
    public async Task Loop()
    {
        while (!_cancellationSource!.Token.IsCancellationRequested)
        {
            var job = await _jobSource.GetNextJobAsync(_cancellationToken);
   
            _printServer.SubmitJob(job.IP, job);
        }
    }

    public Task StopAsync()
    {
        _cancellationSource!.Cancel();
        return Task.CompletedTask;
    }
}