Imports System.IO
Imports Newtonsoft.Json
Imports Pilz.Configuration
Imports Pilz.GeneralEventArgs

Public Class SimpleConfiguration

    <JsonIgnore>
    Public ReadOnly Managers As New ConfigurationManagerList

    Public Sub New()
        AddHandler Managers.GettingParentManager, AddressOf Managers_GettingParentManager
    End Sub

    Private Sub Managers_GettingParentManager(sender As Object, e As GetValueEventArgs(Of SimpleConfiguration))
        If sender Is Managers Then
            e.Value = Me
        End If
    End Sub

    ''' <summary>
    ''' Writes this instance to a string and return it.
    ''' </summary>
    ''' <returns>The content of the configuration instance as string.</returns>
    Public Function WriteToString() As String
        Return ConfigurationSerializer.WriteToString(Me)
    End Function

    ''' <summary>
    ''' Write this instance to a given stream.
    ''' </summary>
    ''' <param name="stream">The stream where the content should be written to.</param>
    Public Sub WriteToStream(stream As Stream)
        ConfigurationSerializer.WriteToStream(Me, stream)
    End Sub

    ''' <summary>
    ''' Writes this instance to the given filePath as text file.
    ''' </summary>
    ''' <param name="filePath">The file path where the content should be written to. The file will be created or overwritten.</param>
    Public Sub WriteToFile(filePath As String)
        ConfigurationSerializer.WriteToFile(Me, filePath)
    End Sub

    ''' <summary>
    ''' Reads a configuratin from the given string and returns an instance of it.
    ''' </summary>
    ''' <typeparam name="T">The type of the configuration class to instance.</typeparam>
    ''' <param name="content">The content of the configuration as string.</param>
    ''' <returns></returns>
    Public Shared Function ReadFromString(Of T As SimpleConfiguration)(content As String) As T
        Return ConfigurationSerializer.ReadFromString(Of T)(content)
    End Function

    ''' <summary>
    ''' Read a configuration from the given string and put them to this instance.
    ''' </summary>
    ''' <param name="content">The content of the configuration as string.</param>
    Public Sub ReadFromString(content As String)
        ConfigurationSerializer.ReadFromString(Me, content)
    End Sub

    ''' <summary>
    ''' Reads a configuratin from the given string and returns an instance of it.
    ''' </summary>
    ''' <typeparam name="T">The type of the configuration class to instance.</typeparam>
    ''' <param name="stream">The stream with the content of the configuration.</param>
    ''' <returns></returns>
    Public Shared Function ReadFromStream(Of T As SimpleConfiguration)(stream As Stream) As T
        Return ConfigurationSerializer.ReadFromStream(Of T)(stream)
    End Function

    ''' <summary>
    ''' Read a configuration from the given string and put them to this instance.
    ''' </summary>
    ''' <param name="stream">The stream with the content of the configuration.</param>
    Public Sub ReadFromStream(stream As Stream)
        ConfigurationSerializer.ReadFromStream(Me, stream)
    End Sub

    ''' <summary>
    ''' Reads a configuratin from the given string and returns an instance of it.
    ''' </summary>
    ''' <typeparam name="T">The type of the configuration class to instance.</typeparam>
    ''' <param name="filePath">The path to the file with the content of the configuration.</param>
    ''' <returns></returns>
    Public Shared Function ReadFromFile(Of T As SimpleConfiguration)(filePath As String) As T
        Return ConfigurationSerializer.ReadFromFile(Of T)(filePath)
    End Function

    ''' <summary>
    ''' Read a configuration from the given string and put them to this instance.
    ''' </summary>
    ''' <param name="filePath">The path to the file with the content of the configuration.</param>
    Public Sub ReadFromFile(filePath As String)
        ConfigurationSerializer.ReadFromFile(Me, filePath)
    End Sub

End Class
