Imports System.IO
Imports System.Xml

Public Class ImporterMarks
    Private Shared dpmaMarks As DPMAConnectMarks
    Private Shared parserMarks As New ParserMarks()
    Private Shared marks As New List(Of Mark)

    Public Shared Function FromDPMA()
        Dim query = QuerybuilderMarks.Build(config, database)
        Dim mainDoc As XmlDocument
        Dim markDoc As XmlDocument

        logger.Info("Sending request with query: \n\n {0}", query)
        logger.Info("Search Type: Marks")

        dpmaMarks = New DPMAConnectMarks(config.username, config.password, config.searchType)

        mainDoc = dpmaMarks.Search(query)
        mainDoc.Save(MainPathMarks)

        ' Ergebnis auslesen und Marken-Klassen erstellen
        marks = parserMarks.ReadMarkXMLFile(MainPathMarks)

        logger.Info("Importing Marks..")

        For Each mark As Mark In marks
            Dim akz As String = mark.ApplicationNumber
            Dim file As String = Path.Combine(DataDir, $"Mark-Data-{akz}.xml")

            logger.Debug($"Getting Register Info for Mark {akz}")

            markDoc = dpmaMarks.GetRegisterInfo(akz)
            markDoc.Save(file)

            parserMarks.ReadMarkRegisterInfoXMLFile(file, mark)
        Next

        logger.Info("{0} Marks imported", marks.Count)

        Return marks
    End Function
End Class
