using System.Diagnostics.CodeAnalysis;

namespace NeonSprawl.Server.Game.Gathering;

/// <summary>Adapter over <see cref="ResourceNodeCatalog"/> (NEO-59).</summary>
public sealed class ResourceNodeDefinitionRegistry(ResourceNodeCatalog catalog) : IResourceNodeDefinitionRegistry
{
    /// <inheritdoc />
    public bool TryGetDefinition(string? nodeDefId, [NotNullWhen(true)] out ResourceNodeDefRow? definition)
    {
        if (nodeDefId is null)
        {
            definition = null;
            return false;
        }

        if (catalog.NodesById.TryGetValue(nodeDefId, out var row))
        {
            definition = row;
            return true;
        }

        definition = null;
        return false;
    }

    /// <inheritdoc />
    public bool TryGetYield(string? nodeDefId, [NotNullWhen(true)] out ResourceYieldRow? yield)
    {
        if (nodeDefId is null)
        {
            yield = null;
            return false;
        }

        if (catalog.YieldsByNodeDefId.TryGetValue(nodeDefId, out var row))
        {
            yield = row;
            return true;
        }

        yield = null;
        return false;
    }

    /// <inheritdoc />
    public IReadOnlyList<ResourceNodeDefRow> GetDefinitionsInIdOrder() =>
        catalog.NodesById
            .OrderBy(kv => kv.Key, StringComparer.Ordinal)
            .Select(kv => kv.Value)
            .ToList();
}
