' Copyright (C) 2025  P4pillon.org
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU Affero General Public License as
' published by the Free Software Foundation, either version 3 of the
' License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Affero General Public License for more details.
'
' You should have received a copy of the GNU Affero General Public License
' along with this program.  If not, see <https://www.gnu.org/licenses/>.
Imports System.Xml
Public Class Config


    Public Sub load_config()
        Dim xmldoc = New XmlDocument
        xmldoc.Load(Application.StartupPath & "\Config.xml")
        Dim Config_Node = xmldoc.ChildNodes.Item(1)

        For Each Item As XmlNode In Config_Node.ChildNodes
            If Item.Name = "Nettoyage_Fichiers" Then
                Nettoyage_Fichiers_Items = New List(Of Nettoyage_Fichier_Item_type)
                For r = 0 To Item.ChildNodes.Count - 1
                    Dim data As XmlElement = Item.ChildNodes.Item(r)
                    Dim Nettoyage_Fichier_Item As New Nettoyage_Fichier_Item_type
                    Nettoyage_Fichier_Item.Rep = data.GetAttribute("Rep")
                    Nettoyage_Fichier_Item.Fichiers = data.GetAttribute("Fichiers")
                    Nettoyage_Fichier_Item.Jours = data.GetAttribute("Jours")
                    Nettoyage_Fichiers_Items.Add(Nettoyage_Fichier_Item)
                Next

            ElseIf Item.Name = "FTP" Then
                FTP.Serveur = Item.SelectSingleNode("Serveur").InnerText
                FTP.user = Item.SelectSingleNode("user").InnerText
                FTP.password = Item.SelectSingleNode("password").InnerText

            ElseIf Item.Name = "CEPS" Then
                CEPS.CEPS_dir_IN = Item.SelectSingleNode("CEPS_dir_IN").InnerText
                CEPS.CEPS_dir_OUT = Item.SelectSingleNode("CEPS_dir_OUT").InnerText
                CEPS.CEPS_MENSUEL = Item.SelectSingleNode("CEPS_MENSUEL").InnerText
                CEPS.CEPS_Quotidien = Item.SelectSingleNode("CEPS_Quotidien").InnerText

            ElseIf Item.Name = "LOI" Then
                LOI.LOI_version = Item.SelectSingleNode("LOI_version").InnerText
                LOI.LOI_dir_IN = Item.SelectSingleNode("LOI_dir_IN").InnerText
                LOI.LOI_dir_OUT = Item.SelectSingleNode("LOI_dir_OUT").InnerText

            ElseIf Item.Name = "LPP" Then
                LPP.LPP_version = Item.SelectSingleNode("LPP_version").InnerText
                LPP.LPP_dir_IN = Item.SelectSingleNode("LPP_dir_IN").InnerText
                LPP.LPP_dir_OUT = Item.SelectSingleNode("LPP_dir_OUT").InnerText

            ElseIf Item.Name = "EXE" Then
                EXE.EXE_version = Item.SelectSingleNode("EXE_version").InnerText
                EXE.EXE_dir_IN = Item.SelectSingleNode("EXE_dir_IN").InnerText
                EXE.EXE_dir_OUT = Item.SelectSingleNode("EXE_dir_OUT").InnerText
                EXE.OfficineLib_version = Item.SelectSingleNode("OfficineLib_version").InnerText
            End If
        Next

        Dim Nettoyage_Fichiers_Node = Config_Node.ChildNodes.Item(0)
    End Sub

    Public Sub update_config(ByVal section As String, ByVal item As String, ByVal value As String)
        Dim xmldoc = New XmlDocument
        xmldoc.Load(Application.StartupPath & "\Config.xml")
        Dim Config_Node = xmldoc.ChildNodes.Item(1)
        For Each section_node As XmlNode In Config_Node.ChildNodes
            If section_node.Name = section Then
                section_node.SelectSingleNode(item).InnerText = value
                Exit For
            End If
        Next
        xmldoc.Save(Application.StartupPath & "\Config.xml")
        load_config()
    End Sub

    Public Structure Nettoyage_Fichier_Item_type
        Dim Rep As String
        Dim Fichiers As String
        Dim Jours As Integer
    End Structure
    Public Nettoyage_Fichiers_Items As List(Of Nettoyage_Fichier_Item_type)

    Public Structure FTP_type
        Dim Serveur, user, password As String
    End Structure
    Public FTP As FTP_type

    Public Structure CEPS_type
        Dim CEPS_dir_IN, CEPS_dir_OUT, CEPS_MENSUEL, CEPS_Quotidien As String
    End Structure
    Public CEPS As CEPS_type

    Public Structure LOI_type
        Dim LOI_version, LOI_dir_IN, LOI_dir_OUT As String
    End Structure
    Public LOI As LOI_type

    Public Structure LPP_type
        Dim LPP_version, LPP_dir_IN, LPP_dir_OUT As String
    End Structure
    Public LPP As LPP_type

    Public Structure EXE_type
        Dim EXE_version, EXE_dir_IN, EXE_dir_OUT, OfficineLib_version As String

    End Structure
    Public EXE As EXE_type
End Class