Imports System.Xml
Imports System.IO

Public Class ParserMarks
    Private _path As String
    Private _marks As New List(Of Mark)

    Public Sub New()

    End Sub

#Region "Parse Results of Search"
    Public Function ReadMarkXML(doc As XmlDocument) As List(Of Mark)
        Using reader As XmlTextReader = New XmlTextReader(New StringReader(doc.OuterXml))
            While reader.Read()
                If reader.IsStartElement() Then
                    If reader.Name = "Entries" Then
                        Dim mark As Mark = CreateMark(reader.ReadSubtree())
                        _marks.Add(mark)
                    End If
                End If
            End While
        End Using

        Return _marks
    End Function

    Public Function ReadMarkXMLFile(path As String) As List(Of Mark)
        Using reader As XmlReader = XmlReader.Create(path)
            While reader.Read()
                If reader.IsStartElement() Then
                    If reader.Name = "Entries" Then
                        Dim mark As Mark = CreateMark(reader.ReadSubtree())
                        _marks.Add(mark)
                    End If
                End If
            End While
        End Using

        Return _marks
    End Function

    Private Function CreateMark(r As XmlReader) As Mark
        Dim mark As New Mark()
        While r.Read()
            If r.IsStartElement() Then
                If r.NodeType = XmlNodeType.Element Then
                    'Console.WriteLine("Name: {0} - Value: {1} - HasValue: {2}", r.Name, r.Value, r.HasValue)
                    Select Case r.Name
                        Case "Applicant"
                            mark.Applicant = r.ReadInnerXml()
                        Case "ApplicationNumber"
                            mark.ApplicationNumber = r.ReadInnerXml()
                        Case "ApplicationDate"
                            mark.ApplicationDate = r.ReadInnerXml()
                        Case "MarkFeature"
                            mark.MarkFeature = r.ReadInnerXml()
                        Case "MarkVerbalElementText"
                            mark.MarkVerbalElementText = r.ReadInnerXml()
                        Case "RegistrationOfficeCode"
                            mark.RegistrationOfficeCode = r.ReadInnerXml()
                        Case "RegistrationDate"
                            mark.RegistrationDate = r.ReadInnerXml()
                        Case "Representative"
                            mark.Representative = r.ReadInnerXml()
                        Case "Classification"
                            mark.Classification = r.ReadInnerXml()
                    End Select
                End If
            End If
        End While

        Return mark
    End Function
#End Region

#Region "Parse Results of GetRegisterInfo"
    Public Sub ReadMarkRegisterInfoXMLFile(path As String, ByRef mark As Mark)
        Using reader As XmlReader = XmlReader.Create(path)
            While reader.Read()
                If reader.IsStartElement() Then
                    If reader.Name = "TradeMark" Then
                        ReadTradeMark(reader.ReadSubtree(), mark)
                    End If
                End If
            End While
        End Using
    End Sub

    Public Sub ReadMarkRegisterInfoXML(doc As XmlDocument, ByRef mark As Mark)
        Using reader As XmlReader = New XmlNodeReader(doc)
            While reader.Read()
                If reader.IsStartElement() Then
                    If reader.Name = "TradeMark" Then
                        ReadTradeMark(reader.ReadSubtree(), mark)
                    End If
                End If
            End While
        End Using
    End Sub

    Private Sub ReadTradeMark(r As XmlReader, ByRef mark As Mark)
        While r.Read()
            If r.IsStartElement() Then
                If r.NodeType = XmlNodeType.Element Then
                    Select Case r.Name
                        Case "ExpiryDate"
                            mark.ExpiryDate = r.ReadInnerXml()
                        Case "RegistrationNumber"
                            mark.RegistrationNumber = r.ReadInnerXml()
                        Case "MarkRecordDetails"
                            ReadMarkRecordDetails(r.ReadSubtree(), mark)
                        Case "GoodsServicesDetails"
                            ReadGoodsDetails(r.ReadSubtree(), mark)
                        Case "PublicationDetails"
                            r.ReadToFollowing("PublicationDate")
                            mark.PublicationDate = r.ReadInnerXml()
                        Case "OppositionPeriodStartDate"
                            mark.OppositionPeriodStartDate = r.ReadInnerXml()
                        Case "OppositionPeriodEndDate"
                            mark.OppositionPeriodEndDate = r.ReadInnerXml()
                        Case "MarkImage"
                            ReadImageDetails(r.ReadSubtree(), mark)
                        Case "MarkCurrentStatusCode"
                            mark.MarkCurrentStatusCode = r.ReadInnerXml()
                    End Select
                End If
            End If
        End While
    End Sub

    Private Sub ReadImageDetails(r As XmlReader, ByRef mark As Mark)
        Dim image As New ImageDetail()

        While r.Read()
            If r.IsStartElement() Then
                If r.NodeType = XmlNodeType.Element Then
                    Select Case r.Name
                        Case "MarkImageBinary"
                            image.BinaryImage = r.ReadInnerXml()
                        Case "Height"
                            image.Height = r.ReadInnerXml()
                        Case "Width"
                            image.Width = r.ReadInnerXml()
                        Case "MarkImageFileFormat"
                            image.Format = r.ReadInnerXml()
                    End Select
                End If
            End If
        End While

        mark.Image = image
    End Sub

    Private Sub ReadGoodsDetails(r As XmlReader, ByRef mark As Mark)
        While r.Read()
            If r.IsStartElement() Then
                If r.NodeType = XmlNodeType.Element Then

                    Select Case r.Name
                        Case "ClassificationKindCode"
                            mark.ClassificationKindCode = r.ReadInnerXml()
                        Case "ClassificationVersion"
                            mark.ClassificationVersion = r.ReadInnerXml()
                        Case "ProposedLeadingClassNumber"
                            mark.ProposedLeadingClassNumber = r.ReadInnerXml()
                        Case "ClassDescription"
                            ReadClassDescription(r.ReadSubtree(), mark)
                        Case "de:GoodsServicesNotClassifiedDescription"
                            mark.GoodsServicesNotClassified = r.ReadInnerXml()
                    End Select

                End If
            End If
        End While
    End Sub

    Private Sub ReadClassDescription(r As XmlReader, ByRef mark As Mark)
        Dim detail As New ClassificationDetail()

        While r.Read()
            If r.IsStartElement() Then
                If r.NodeType = XmlNodeType.Element Then
                    Select Case r.Name
                        Case "ClassNumber"
                            detail.ClassNumber = r.ReadInnerXml()
                        Case "GoodsServicesDescription"
                            detail.GoodsServicesDescription = r.ReadInnerXml()
                    End Select
                End If
            End If
        End While

        mark.ClassificationDetail.Add(detail)
    End Sub

    Private Sub ReadMarkRecordDetails(r As XmlReader, ByRef mark As Mark)
        While r.Read()
            If r.IsStartElement() Then
                If r.NodeType = XmlNodeType.Element Then

                    If r.Name = "BasicRecord" Then
                        ReadMarkRecord(r.ReadSubtree(), mark)
                    End If

                End If
            End If
        End While
    End Sub

    Private Sub ReadMarkRecord(r As XmlReader, ByRef mark As Mark)
        Dim detail As New MarkRecordDetail()

        While r.Read()
            If r.IsStartElement() Then
                If r.NodeType = XmlNodeType.Element Then
                    Select Case r.Name
                        Case "RecordIdentifier"
                            detail.RecordIdentifier = r.ReadInnerXml()
                        Case "BasicRecordKind"
                            detail.BasicRecordKind = r.ReadInnerXml()
                        Case "RecordCurrentStatusCode"
                            detail.RecordCurrentStatusCode = r.ReadInnerXml()
                        Case "RecordCurrentStatusDate"
                            detail.RecordCurrentStatusDate = r.ReadInnerXml()
                        Case "de:RecordProcedureIncomplete"
                            detail.RecordProcedureIncomplete = True
                    End Select
                End If
            End If
        End While

        mark.MarkRecordDetails.Add(detail)
    End Sub

#End Region

End Class
