using SharepointToolbox.Localization;
using System.ComponentModel;
using System.Globalization;

namespace SharepointToolbox.Tests.Localization;

[Trait("Category", "Unit")]
public class TranslationSourceTests : IDisposable
{
    private readonly CultureInfo _originalCulture;

    public TranslationSourceTests()
    {
        _originalCulture = TranslationSource.Instance.CurrentCulture;
        // Reset to EN before each test
        TranslationSource.Instance.CurrentCulture = new CultureInfo("en-US");
    }

    public void Dispose()
    {
        // Restore original culture after each test to prevent test pollution
        TranslationSource.Instance.CurrentCulture = _originalCulture;
    }

    [Fact]
    public void Instance_IsSameInstance_OnMultipleAccesses()
    {
        var instance1 = TranslationSource.Instance;
        var instance2 = TranslationSource.Instance;
        Assert.Same(instance1, instance2);
    }

    [Fact]
    public void Indexer_ReturnsEnString_ForEnCulture()
    {
        TranslationSource.Instance.CurrentCulture = new CultureInfo("en-US");
        var result = TranslationSource.Instance["app.title"];
        Assert.Equal("SharePoint Toolbox", result);
    }

    [Fact]
    public void Indexer_ReturnsFrOrFallback_AfterSwitchToFrFR()
    {
        TranslationSource.Instance.CurrentCulture = new CultureInfo("fr-FR");
        var result = TranslationSource.Instance["app.title"];
        // FR stub uses EN text — at minimum should not be null or empty
        Assert.NotNull(result);
        Assert.NotEmpty(result);
        Assert.DoesNotContain("[", result); // Should not be missing-key placeholder
    }

    [Fact]
    public void Indexer_ReturnsBracketedKey_ForMissingKey()
    {
        var result = TranslationSource.Instance["key.does.not.exist"];
        Assert.Equal("[key.does.not.exist]", result);
    }

    [Fact]
    public void ChangingCurrentCulture_FiresPropertyChanged_WithEmptyPropertyName()
    {
        string? capturedPropertyName = null;
        TranslationSource.Instance.PropertyChanged += (sender, args) =>
            capturedPropertyName = args.PropertyName;

        TranslationSource.Instance.CurrentCulture = new CultureInfo("fr-FR");

        Assert.Equal(string.Empty, capturedPropertyName);
    }

    [Fact]
    public void SettingSameCulture_DoesNotFirePropertyChanged()
    {
        TranslationSource.Instance.CurrentCulture = new CultureInfo("en-US");
        int fireCount = 0;
        TranslationSource.Instance.PropertyChanged += (sender, args) => fireCount++;

        // Set the exact same culture
        TranslationSource.Instance.CurrentCulture = new CultureInfo("en-US");

        Assert.Equal(0, fireCount);
    }
}
