using System;
using System.Collections.Generic;
using ZB.MOM.WW.MxGateway.Worker.Bootstrap;

namespace ZB.MOM.WW.MxGateway.Worker.Tests.Bootstrap;

/// <summary>
/// In-memory test implementation of the worker environment.
/// </summary>
internal sealed class MemoryWorkerEnvironment : IWorkerEnvironment
{
    private readonly Dictionary<string, string> _values = new();
    private readonly Exception? _exception;

    /// <summary>
    /// Initializes an empty environment.
    /// </summary>
    public MemoryWorkerEnvironment()
    {
    }

    /// <summary>
    /// Initializes an environment that throws when accessed.
    /// </summary>
    /// <param name="exception">Exception to throw on access.</param>
    public MemoryWorkerEnvironment(Exception exception)
    {
        _exception = exception;
    }

    /// <summary>
    /// Sets an environment variable in the in-memory store.
    /// </summary>
    /// <param name="name">Variable name.</param>
    /// <param name="value">Variable value.</param>
    public void Set(string name, string value)
    {
        _values[name] = value;
    }

    /// <inheritdoc />
    public string? GetEnvironmentVariable(string name)
    {
        if (_exception is not null)
        {
            throw _exception;
        }

        return _values.TryGetValue(name, out string value)
            ? value
            : null;
    }
}
