using System.Collections.Generic;
using StellaOps.Excititor.Worker.Scheduling;

namespace StellaOps.Excititor.Worker.Options;

public sealed class VexWorkerOptions
{
    public TimeSpan DefaultInterval { get; set; } = TimeSpan.FromHours(1);

    public TimeSpan OfflineInterval { get; set; } = TimeSpan.FromHours(6);

    public TimeSpan DefaultInitialDelay { get; set; } = TimeSpan.FromMinutes(5);

    public bool OfflineMode { get; set; }

    public IList<VexWorkerProviderOptions> Providers { get; } = new List<VexWorkerProviderOptions>();

    internal IReadOnlyList<VexWorkerSchedule> ResolveSchedules()
    {
        var schedules = new List<VexWorkerSchedule>();
        foreach (var provider in Providers)
        {
            if (!provider.Enabled)
            {
                continue;
            }

            var providerId = provider.ProviderId?.Trim();
            if (string.IsNullOrWhiteSpace(providerId))
            {
                continue;
            }

            var interval = provider.Interval ?? (OfflineMode ? OfflineInterval : DefaultInterval);
            if (interval <= TimeSpan.Zero)
            {
                continue;
            }

            var initialDelay = provider.InitialDelay ?? DefaultInitialDelay;
            if (initialDelay < TimeSpan.Zero)
            {
                initialDelay = TimeSpan.Zero;
            }

            schedules.Add(new VexWorkerSchedule(providerId, interval, initialDelay));
        }

        return schedules;
    }
}

public sealed class VexWorkerProviderOptions
{
    public string ProviderId { get; set; } = string.Empty;

    public bool Enabled { get; set; } = true;

    public TimeSpan? Interval { get; set; }

    public TimeSpan? InitialDelay { get; set; }
}
