Imports System.Xml
Imports System.IO
Imports DD_LIB_Standards

Public Class ClassWindowLocation

    Public Shared Sub LoadFormLocationSize(ByRef form As Form, Optional Prefix As String = "")
        Try
            Dim LayoutPath As String
            LayoutPath = Path.Combine(Application.UserAppDataPath(), Prefix & form.Name & "-PositionSize.xml")

            Dim layout As ClassLayout = New ClassLayout(LayoutPath)
            Dim settings As System.Collections.Generic.List(Of ClassSetting)
            settings = layout.Load()

            If settings.Count = 0 Then
                settings.Add(New ClassSetting("PositionX", form.Location.X))
                settings.Add(New ClassSetting("PositionY", form.Location.Y))
                settings.Add(New ClassSetting("Width", form.Size.Width))
                settings.Add(New ClassSetting("Height", form.Size.Height))
                layout.Save(settings)
            End If

            For Each s As ClassSetting In settings
                Dim x, y, w, h As Integer

                Select Case s._name
                    Case "PositionX"
                        x = Integer.Parse(s._value)
                    Case "PositionY"
                        y = Integer.Parse(s._value)
                    Case "Width"
                        w = Integer.Parse(s._value)
                    Case "Height"
                        h = Integer.Parse(s._value)
                End Select

                If x = 5000 Then
                    form.WindowState = FormWindowState.Maximized
                Else
                    If x > 0 Then
                        form.Location = New Point(x, y)
                        form.Size = New Size(w, h)

                    End If
                End If

            Next
        Catch notFoundEx As System.IO.FileNotFoundException
            Logger.Info("Window Position & Size added for Form " & form.Name)
        Catch ex As Exception
            MsgBox("Error while loading Window Position!" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Public Shared Sub SaveFormLocationSize(ByRef form As Form, Optional Prefix As String = "")
        Try
            Dim _path As String
            _path = Path.Combine(Application.UserAppDataPath(), Prefix & form.Name & "-PositionSize.xml")
            Dim layout As ClassLayout = New ClassLayout(_path)
            Dim settings As System.Collections.Generic.List(Of ClassSetting) = New System.Collections.Generic.List(Of ClassSetting)
            Dim width As Integer
            Dim height As Integer
            Dim x As Integer
            Dim y As Integer
            If form.WindowState = FormWindowState.Maximized Then
                width = 5000
                height = 5000
                x = 5000
                y = 5000
            Else
                width = form.Size.Width
                height = form.Size.Height
                x = form.Location.X
                y = form.Location.Y
            End If

            settings.Add(New ClassSetting("PositionX", x))
            settings.Add(New ClassSetting("PositionY", y))
            settings.Add(New ClassSetting("Width", width))
            settings.Add(New ClassSetting("Height", height))

            layout.Save(settings)
        Catch notFoundEx As System.IO.FileNotFoundException
            Logger.Info("Window Position & Size added for Form " & form.Name)
        Catch ex As Exception
            MsgBox("Error while saving Window Position!" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try


    End Sub

End Class


'-------------------------------------------------------------------

Public Class ClassSetting
    Public _name As String
    Public _value As String

    Public Sub New(name As String, value As String)
        _name = name
        _value = value
    End Sub
End Class

Public Class ClassLayout
    Private _filename As String
    Private _reader As XmlReader
    Private _settings As XmlWriterSettings

    Public Sub New(filename As String)
        _filename = filename

        _settings = New XmlWriterSettings()
        _settings.Encoding = System.Text.Encoding.UTF8
        _settings.Indent = True
    End Sub

    Public Sub Save(settings As System.Collections.Generic.List(Of ClassSetting))
        Dim w = XmlTextWriter.Create(_filename, _settings)

        w.WriteStartDocument()
        w.WriteStartElement("Settings")

        For Each setting As ClassSetting In settings
            w.WriteStartElement("Setting")
            w.WriteAttributeString("name", setting._name)
            w.WriteAttributeString("value", setting._value.ToString())
            w.WriteEndElement()
        Next

        w.WriteEndElement()
        w.WriteEndDocument()

        w.Dispose()
        w.Close()
    End Sub

    Public Function Load() As System.Collections.Generic.List(Of ClassSetting)
        Dim Result As List(Of ClassSetting) = New List(Of ClassSetting)()

        If Not File.Exists(_filename) Then
            Return Result
        End If

        _reader = XmlReader.Create(_filename)

        While _reader.Read()
            If _reader.IsStartElement() Then
                If _reader.Name = "Setting" Then
                    Dim name As String = _reader("name")
                    ' Dim value As Integer = Integer.Parse(_reader("value"))
                    Dim setting As ClassSetting = New ClassSetting(name, _reader("value")) 'value)
                    Result.Add(setting)
                End If
            End If
        End While

        _reader.Dispose()
        _reader.Close()

        Return Result
    End Function
End Class
