Imports Microsoft.VisualBasic
Imports System.Xml


Namespace CUAHSI
    Public Class GetValuesResponse
        'Dim rawxml As String
        ' instances of subclasses
        Public QueryInfo As QInfo
        Public TimeSeries As TSeries

        Public Sub New(ByVal xml As String)
            'rawxml = xml
            Dim stringreader As System.IO.StringReader = New System.IO.StringReader(xml)
            Dim doc As XmlDocument = New XmlDocument()
            doc.Load(stringreader)
            Dim nodes As XmlNodeList = doc.FirstChild.ChildNodes
            Dim node As XmlNode
            node = doc.FirstChild
            If node.Name = "error" Then Throw New Exception(node.InnerText)
            For Each node In nodes
                If node.Name.ToLower = "queryinfo" Then
                    QueryInfo = New QInfo(node)
                ElseIf node.Name.ToLower = "timeseries" Then
                    TimeSeries = New TSeries(node)
                End If
            Next

        End Sub

        Public Class QInfo
            Public creationtime As DateTime = DateTime.Now
            Public queryurl As String = ""
            Public locationparam As String = ""
            Public variableparam As String = ""
            Public begindatetime As DateTime = Nothing
            Public enddatetime As DateTime = Nothing
            Public notes As System.Collections.ArrayList = New System.Collections.ArrayList()
            Public Sub New(ByVal node As XmlNode)
                If node.HasChildNodes Then
                    Dim nodes As XmlNodeList = node.ChildNodes
                    For Each node In nodes
                        If node.Name = "creationTime" Then
                            creationtime = DateTime.Parse(node.InnerText)
                        ElseIf node.Name = "queryURL" Then
                            queryurl = node.InnerText
                        ElseIf node.Name.ToLower = "criteria" Then
                            If node.HasChildNodes Then
                                Dim cnodes As XmlNodeList = node.ChildNodes
                                Dim cnode As XmlNode
                                For Each cnode In cnodes
                                    If cnode.Name.ToLower = "locationparam" Then
                                        locationparam = cnode.InnerText
                                    ElseIf cnode.Name.ToLower = "variableparam" Then
                                        variableparam = cnode.InnerText
                                    ElseIf cnode.Name.ToLower = "timeparam" Then
                                        begindatetime = DateTime.Parse(cnode.FirstChild.InnerText)
                                        enddatetime = DateTime.Parse(cnode.LastChild.InnerText)
                                    End If
                                Next
                            End If
                        ElseIf node.Name.ToLower = "note" Then
                            notes.Add(node.InnerText)
                        End If
                    Next
                End If
            End Sub
        End Class
        Public Class TSeries

            Public SourceInfo As SInfo
            Public Variable As variabl
            Public Values As valuedata
            Public Sub New(ByVal node As XmlNode)
                If node.HasChildNodes Then
                    Dim nodes As XmlNodeList = node.ChildNodes
                    For Each node In nodes
                        If node.Name.ToLower = "sourceinfo" Then
                            SourceInfo = New SInfo(node)
                        ElseIf node.Name.ToLower = "variable" Then
                            Variable = New variabl(node)
                        ElseIf node.Name.ToLower = "values" Then
                            Values = New valuedata(node)
                        End If

                    Next
                End If
            End Sub

            Public Class SInfo ' siteinfo node
                Public sitename As String = ""
                Public sitecode As String = ""
                Public latitude As Double = 0
                Public longitude As Double = 0
                Public localx As Double = 0
                Public localy As Double = 0
                Public local_srs As String = ""
                Public geographic_srs As String
                Public vertdatum As String = ""
                Public notes As System.Collections.ArrayList = New System.Collections.ArrayList()
                Public Sub New(ByVal node As XmlNode)
                    Dim nodes As XmlNodeList = node.ChildNodes
                    For Each node In nodes
                        If node.Name.ToLower = "sitename" Then
                            sitename = node.InnerText
                        ElseIf node.Name.ToLower = "sitecode" Then
                            sitecode = node.InnerText
                        ElseIf node.Name = "verticalDatum" Then
                            vertdatum = node.InnerText
                        ElseIf node.Name.ToLower = "note" Then
                            notes.Add(node.InnerText)
                        ElseIf (node.Name.ToLower = "geolocation") Then
                            Dim i As Integer
                            For i = 0 To node.ChildNodes.Count - 1
                                If node.ChildNodes(i).Name.ToLower = "geoglocation" Then
                                    If Not node.ChildNodes(i).Attributes("srs") Is Nothing Then geographic_srs = node.ChildNodes(i).Attributes("srs").Value
                                    latitude = Double.Parse(node.ChildNodes(i).FirstChild.InnerText)
                                    longitude = Double.Parse(node.ChildNodes(i).LastChild.InnerText)
                                ElseIf node.ChildNodes(i).Name = "localSiteXY" Then
                                    If Not node.ChildNodes(i).Attributes("projectionInformation") Is Nothing Then local_srs = node.ChildNodes(i).Attributes("projectionInformation").Value
                                    localx = Double.Parse(node.ChildNodes(i).FirstChild.InnerText)
                                    localy = Double.Parse(node.ChildNodes(i).LastChild.InnerText)
                                End If
                            Next
                        End If
                    Next

                End Sub
            End Class
            Public Class variabl ' variable node
                Public valuetype As String = ""
                Public samplemedium As String = ""
                Public datatype As String = ""
                Public generalcategory As String = ""
                Public variablecode As String = ""
                Public vocabulary As String = ""
                Public variableName As String = ""
                Public units As String = ""
                Public unitscode As String = ""
                Public unitsabbr As String = ""
                Public nodataval As Double = -9999
                Public timesupportisregular As Boolean
                Public timeunitdescription As String = ""
                Public timeunitabbr As String = ""
                Public Sub New(ByVal node As XmlNode)
                    Dim nodes As XmlNodeList = node.ChildNodes
                    For Each node In nodes
                        If node.Name = "valueType" Then
                            valuetype = node.InnerText
                        ElseIf node.Name = "sampleMedium" Then
                            samplemedium = node.InnerText
                        ElseIf node.Name = "dataType" Then
                            datatype = node.InnerText
                        ElseIf node.Name = "generalCategory" Then
                            generalcategory = node.InnerText
                        ElseIf node.Name = "variableCode" Then
                            variablecode = node.InnerText
                            vocabulary = node.Attributes("vocabulary").Value
                        ElseIf node.Name = "variableName" Then
                            variableName = node.InnerText
                        ElseIf node.Name = "units" Then
                            units = node.InnerText
                            unitscode = node.Attributes("unitsCode").Value
                            unitsabbr = node.Attributes("unitsAbbreviation").Value
                        ElseIf node.Name = "NoDataValue" Then
                            nodataval = Double.Parse(node.InnerText)
                        ElseIf node.Name = "timeSupport" Then
                            timesupportisregular = node.Attributes("isRegular").Equals("true")
                            ' todo more time stuff..
                        End If
                    Next
                End Sub

            End Class
            Public Class ValueData
                Public ValueCount As Long
                Public methods As System.Collections.ArrayList = New System.Collections.ArrayList()
                Public sources As System.Collections.ArrayList = New System.Collections.ArrayList()
                Public values As System.Collections.ArrayList = New System.Collections.ArrayList()
                Public Sub New(ByVal node As XmlNode)
                    ValueCount = Integer.Parse(node.Attributes("count").Value)
                    Dim nodes As XmlNodeList = node.ChildNodes
                    For Each node In nodes
                        If node.Name = "value" Then
                            values.Add(New Value(node))
                        ElseIf node.Name = "method" Then
                            methods.Add(New Method(node))
                        ElseIf node.Name = "source" Then
                            sources.Add(New Source(node))
                        End If
                    Next


                End Sub
                Public Class Value
                    Public value As Double = 0
                    Public date_time As DateTime
                    Public attribs As New System.Collections.Hashtable()
                    Public Sub New(ByVal node As XmlNode)
                        value = Double.Parse(node.InnerText)
                        Dim i As Integer
                        For i = 0 To node.Attributes.Count - 1
                            If node.Attributes(i).Name = "dateTime" Then
                                date_time = DateTime.Parse(node.Attributes(i).Value)
                            Else
                                attribs.Add(node.Attributes(i).Name, node.Attributes(i).Value)
                            End If
                        Next
                    End Sub
                End Class
                Public Class Source
                    Public sourceid As String = ""
                    Public organization As String = ""
                    Public srcdesc As String = ""
                    Public srclink As String = ""

                    Public contactinfo As System.Collections.Hashtable = New System.Collections.Hashtable
                    Public Sub New(ByVal node As XmlNode)
                        sourceid = node.Attributes("sourceID").Value
                        Dim nodes As XmlNodeList = node.ChildNodes
                        For Each node In nodes
                            If node.Name = "Organization" Then
                                organization = node.InnerText
                            ElseIf node.Name = "SourceDescription" Then
                                srcdesc = node.InnerText
                            ElseIf node.Name = "SourceLink" Then
                                srclink = node.InnerText
                            ElseIf node.Name = "ContactInformation" Then
                                Dim i As Integer
                                For i = 0 To node.ChildNodes.Count - 1
                                    contactinfo.Add(node.ChildNodes(i).Name, node.ChildNodes(i).InnerText)
                                Next
                            End If
                        Next
                    End Sub
                End Class
                Public Class Method
                    Public methodid As String
                    Public methoddescription As String
                    Public Sub New(ByVal node As XmlNode)
                        methodid = node.Attributes("methodID").Value
                        methoddescription = node.FirstChild.Value
                    End Sub
                End Class

            End Class

        End Class
    End Class
End Namespace