using Lumi.Companion.Abstractions;

namespace Lumi.Companion.App;

internal sealed class TranscriptionPluginContribution : ICompanionPluginContribution
{
    private readonly CompanionRuntime _runtime;

    public TranscriptionPluginContribution(CompanionRuntime runtime)
    {
        _runtime = runtime;
        Actions =
        [
            new CompanionPluginAction(
                "test",
                () => _runtime.State.TestRunning || _runtime.State.BenchmarkRunning ? "Transcription test running…" : "Run transcription test",
                cancellationToken => _runtime.RunTestAsync(cancellationToken),
                () => !_runtime.State.TestRunning && !_runtime.State.BenchmarkRunning,
                10)
        ];
        _runtime.StateChanged += _ => Changed?.Invoke();
    }

    public CompanionPluginDescriptor Descriptor { get; } = new(
        "transcription",
        "Transcription",
        new Version(0, 1, 0),
        "Captures selected OBS sources and delivers closed captions through Lumi.",
        100);

    public IReadOnlyList<CompanionPluginPage> Pages { get; } =
    [
        new CompanionPluginPage("Transcription", "Capture", 10),
        new CompanionPluginPage("Test", "Test & benchmark", 20)
    ];

    public IReadOnlyList<CompanionPluginAction> Actions { get; }

    public CompanionPluginStatus Status => new(
        _runtime.State.Health switch
        {
            TrayHealth.Ready => CompanionPluginHealth.Healthy,
            TrayHealth.Operating => CompanionPluginHealth.Healthy,
            TrayHealth.Failed => CompanionPluginHealth.Error,
            _ => CompanionPluginHealth.Warning
        },
        _runtime.State.Summary,
        _runtime.State.Detail);

    public event Action? Changed;
}
