Option Strict On
Option Explicit On

Imports System
Imports System.IO

''' <summary>
''' Internal LocalAppData-backed persistence for the external Nexamas UI Showcase theme preference.
''' This is a showcase-app preference only; the Nexamas.UI runtime remains the owner of actual theme application.
''' </summary>
Friend NotInheritable Class ShowcaseThemePreferenceStore

    Private Const FolderName As String = "NexamasUIShowcase"
    Private Const FileName As String = "theme-preference.txt"

    Private Sub New()
    End Sub

    Friend Shared Function TryLoadThemeId() As String
        Try
            Dim filePath As String = ResolveFilePath()
            If String.IsNullOrWhiteSpace(filePath) OrElse Not File.Exists(filePath) Then Return String.Empty

            Dim themeId As String = File.ReadAllText(filePath).Trim()
            Return themeId
        Catch ex As Exception
            System.Diagnostics.Debug.WriteLine("ShowcaseThemePreferenceStore.TryLoadThemeId: " & ex.Message)
            Return String.Empty
        End Try
    End Function

    Friend Shared Sub TrySaveThemeId(themeId As String)
        Dim normalized As String = If(themeId, String.Empty).Trim()
        If normalized.Length = 0 Then Return

        Try
            Dim filePath As String = ResolveFilePath()
            If String.IsNullOrWhiteSpace(filePath) Then Return

            Dim folderPath As String = IO.Path.GetDirectoryName(filePath)
            If Not String.IsNullOrWhiteSpace(folderPath) Then
                Directory.CreateDirectory(folderPath)
            End If

            File.WriteAllText(filePath, normalized)
        Catch ex As Exception
            System.Diagnostics.Debug.WriteLine("ShowcaseThemePreferenceStore.TrySaveThemeId: " & ex.Message)
        End Try
    End Sub

    Private Shared Function ResolveFilePath() As String
        Dim baseDir As String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
        If String.IsNullOrWhiteSpace(baseDir) Then
            baseDir = IO.Path.GetTempPath()
        End If

        Return IO.Path.Combine(baseDir, FolderName, FileName)
    End Function
End Class
