Option Strict On
Option Explicit On

Imports System.Collections.Generic
Imports System.Globalization

Namespace Nexamas.UI.Localization

    ''' <summary>
    ''' Official Friend-owned gateway for Phase 6 Localization / RTL product-layer inspection after final public API lockdown.
    ''' </summary>
    ''' <remarks>
    ''' The gateway is intentionally small. It exposes immutable product facts only to Nexamas UI internals and keeps culture
    ''' catalog, formatting preview, sample translations, and readiness logic internal to the system.
    ''' </remarks>
    Friend NotInheritable Class MASLocalization
        Private Sub New()
        End Sub

        ''' <summary>
        ''' Creates an immutable snapshot containing supported cultures, direction facts, formatting previews, and string expansion samples.
        ''' </summary>
        Friend Shared Function CreateSnapshot() As MASLocalizationSnapshot
            Return MASLocalizationSnapshotBuilder.Build()
        End Function

        ''' <summary>
        ''' Returns the Phase 6 supported product culture profiles.
        ''' </summary>
        Friend Shared Function GetCultures() As IReadOnlyList(Of MASLocalizationCultureEntry)
            Return MASLocalizationCultureCatalog.CreateCultures()
        End Function

        ''' <summary>
        ''' Resolves a supported product culture profile. Unknown values fall back to the English profile.
        ''' </summary>
        Friend Shared Function ResolveCulture(cultureName As String) As MASLocalizationCultureEntry
            Return MASLocalizationCultureCatalog.ResolveCulture(cultureName)
        End Function


        ''' <summary>
        ''' Returns the current Friend-owned product string catalog. Runtime consumers use ResolveText instead of reading catalogs directly.
        ''' </summary>
        Friend Shared Function GetStrings() As IReadOnlyList(Of MASLocalizationStringEntry)
            Return MASLocalizationStringCatalog.CreateStrings()
        End Function

        ''' <summary>
        ''' Resolves a product-owned localizable string through the official runtime text gateway.
        ''' Unknown keys return the supplied fallback text, then the key, without throwing or mutating culture.
        ''' </summary>
        Friend Shared Function ResolveText(key As String, cultureName As String, Optional fallbackText As String = Nothing) As String
            Return MASLocalizationStringCatalog.ResolveText(key, cultureName, fallbackText)
        End Function

        ''' <summary>
        ''' Resolves the current UI culture through the official Localization / RTL product-layer gateway.
        ''' </summary>
        Friend Shared Function ResolveCurrentCulture() As MASLocalizationCultureEntry
            Dim snapshot As MASLocalizationRuntimeEnvironmentSnapshot = GetRuntimeEnvironmentSnapshot()
            Return ResolveCulture(If(snapshot Is Nothing, CultureInfo.CurrentUICulture.Name, snapshot.CultureName))
        End Function

        ''' <summary>
        ''' Returns the current right-to-left fact through the Localization-owned runtime snapshot.
        ''' </summary>
        Friend Shared Function IsCurrentRightToLeft() As Boolean
            Try
                Dim snapshot As MASLocalizationRuntimeEnvironmentSnapshot = GetRuntimeEnvironmentSnapshot()
                If snapshot IsNot Nothing Then Return snapshot.IsRightToLeft

                Dim productCulture As MASLocalizationCultureEntry = ResolveCurrentCulture()
                If productCulture IsNot Nothing AndAlso productCulture.IsRightToLeft Then Return True
                Return CultureInfo.CurrentCulture.TextInfo.IsRightToLeft OrElse CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft
            Catch ex As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowInputBoundary(ex, "MASLocalization.IsCurrentRightToLeft")
                Return False
            End Try
        End Function

        ''' <summary>
        ''' Returns the current Localization-owned runtime culture / RTL snapshot without opening a public API.
        ''' </summary>
        Friend Shared Function GetRuntimeEnvironmentSnapshot() As MASLocalizationRuntimeEnvironmentSnapshot
            Return MASLocalizationRuntimeEnvironmentCoordinator.GetCurrentSnapshot()
        End Function

        ''' <summary>
        ''' Returns retained Localization/RTL fallback diagnostics for internal proof gates and official diagnostics surfaces.
        ''' </summary>
        Friend Shared Function GetRtlResolutionDiagnosticsReport() As MASLocalizationRtlResolutionDiagnosticReport
            Return MASLocalizationRtlResolutionDiagnostics.GetReport()
        End Function

        ''' <summary>
        ''' Clears Localization/RTL fallback diagnostics for deterministic internal proof gates only.
        ''' </summary>
        Friend Shared Sub ResetRtlResolutionDiagnosticsForTests()
            MASLocalizationRtlResolutionDiagnostics.ResetForTests()
        End Sub

        ''' <summary>
        ''' Synchronizes the Friend-owned runtime culture snapshot with the current thread culture without mutating thread culture.
        ''' </summary>
        Friend Shared Function SynchronizeRuntimeEnvironmentWithCurrentThread(reason As String,
                                                                             raiseNotification As Boolean) As MASLocalizationRuntimeEnvironmentSnapshot
            Return MASLocalizationRuntimeEnvironmentCoordinator.SynchronizeWithCurrentThreadCulture(reason, raiseNotification)
        End Function

        ''' <summary>
        ''' Internal product-host entry for explicitly switching the MAS-owned runtime culture.
        ''' This does not mutate Thread.CurrentCulture or Thread.CurrentUICulture.
        ''' </summary>
        Friend Shared Function SetRuntimeCulture(cultureName As String,
                                                 reason As String) As MASLocalizationRuntimeEnvironmentSnapshot
            Return MASLocalizationRuntimeEnvironmentCoordinator.SetRuntimeCulture(cultureName, reason)
        End Function

        ''' <summary>
        ''' Returns Localization runtime ownership to the ambient thread-culture observer.
        ''' </summary>
        Friend Shared Function UseCurrentThreadCulture(reason As String) As MASLocalizationRuntimeEnvironmentSnapshot
            Return MASLocalizationRuntimeEnvironmentCoordinator.UseCurrentThreadCulture(reason)
        End Function

        Friend Shared Custom Event RuntimeEnvironmentChanged As EventHandler(Of MASLocalizationRuntimeEnvironmentChangedEventArgs)
            AddHandler(value As EventHandler(Of MASLocalizationRuntimeEnvironmentChangedEventArgs))
                AddHandler MASLocalizationRuntimeEnvironmentCoordinator.RuntimeChanged, value
            End AddHandler
            RemoveHandler(value As EventHandler(Of MASLocalizationRuntimeEnvironmentChangedEventArgs))
                RemoveHandler MASLocalizationRuntimeEnvironmentCoordinator.RuntimeChanged, value
            End RemoveHandler
            RaiseEvent(sender As Object, e As MASLocalizationRuntimeEnvironmentChangedEventArgs)
            End RaiseEvent
        End Event

        ''' <summary>
        ''' Evaluates whether the Localization / RTL foundation is ready for product-owned consumption.
        ''' </summary>
        Friend Shared Function EvaluateReadiness() As MASLocalizationReadinessReport
            Return MASLocalizationReadinessGate.Evaluate()
        End Function
    End Class

End Namespace
