Public Class GoogleSearchPage
    Inherits SearchEnginePage

    Public Sub New()
        MyBase.New()
    End Sub

    'Public Sub New(settings As SearchPageSettings)
    '    MyBase.New(settings)
    'End Sub

    Public Overrides Function ParseFromHTML(ByVal sHTML As String, ByVal adcount As Integer) As Boolean

        Dim z As String                         'Working String
        Dim x As Long                           'Working Variable
        Dim y As Long                           'Working Variable
        Dim s() As String                       'Working String Array
        Dim s2() As String                      'Working String Array

        Dim WorkPageAd As PageAd
        Dim WorkAdWord As GoogleAdWord
        Dim WorkResult As SearchEngineResult

        'Initialize/clear the results collections
        'Me.AdWords.Clear()
        'Me.PageAds.Clear()
        'Me.Results.Clear()

        'Attempt to parse the title for this page
        z = StringFromTo(sHTML, "<title>", "</title>", False, False)

        z = Trim(Replace(z, " - Google Search", ""))
        z = ReplaceExtendedChars(z)
        'z = z.Replace(Settings.GoogleQuryModifier.ToString(), "")
        'If we couldn't get the search term by parsing the title return an error
        If z = "" Then
            Me.ParseErrorDetails = "Unable to Parse <title> tag for search term!"
            ErrorLog(sHTML, "google")
            Return False
        End If

        'Update the private class variable with the original search term
        Me.SearchTerm = z

        'Make sure there are some resulting documents for this term
        If InStr(1, sHTML, "did not match any documents.", vbTextCompare) < 1 Then
            'If there are continue with normal parsing

            'Try to get the real competition count
            z = StringFromTo(sHTML, "<div>About ", " results", False, False)

            z = Replace(z, ",", "")
            z = Replace(z, ".", "")

            If IsNumeric(z) = False Then
                'Try to find it without the about in the text

                z = StringFromTo(sHTML, "</b> of <b>", "</b>", False, False)

                z = Replace(z, ",", "")
                z = Replace(z, ".", "")
            End If

            If z = "" Then
                z = StringFromTo(sHTML, "<div id=resultStats>", "ult", False, False)
                z = StringFromTo(z, "about", "res", False, False)
                z = Replace(z, ",", "")
                z = Replace(z, ".", "")
            End If

            If z = "" Then
                z = StringFromTo(sHTML, "<div id=""resultStats"">", "ult", False, False)
                z = StringFromTo(z, "about", "res", False, False)
                z = Replace(z, ",", "")
                z = Replace(z, ".", "")
            End If

            If z = "" Then
                z = StringFromTo(sHTML, " of about ", " results", False, False)
                z = Replace(z, ",", "")
                z = Replace(z, ".", "")
            End If


            If z = "" Then
                z = StringFromTo(sHTML, "</div><div>Page ", "esults", False, False)
                z = StringFromTo(z, " of", "r", False, False)

                z = Replace(z, ",", "")
                z = Replace(z, ".", "")
            End If

            If z = "" Then
                z = StringFromTo(sHTML, "Advanced search</a></div><div>", "res", False, False)
                z = Replace(z, ",", "")
                z = Replace(z, ".", "")
            End If

            If IsNumeric(z) = False Then
                Me.ParseErrorDetails = "Unable to Parse Total Results Count!"
                ErrorLog(sHTML, "google")
                'Return False
            Else
                'Update the class results variable with the count
                Me.TotalResults = z
            End If

            If Me.TotalResults < 1 Then
                Me.TotalResults = 0
            End If


            ' I don't think this can be parsed, nor does it seem to be needed
            'Try to get the starting result number count
            z = StringFromTo(sHTML, "Results <b>", "</b>", False, False)

            z = Replace(z, ",", "")
            z = Replace(z, ".", "")

            If IsNumeric(z) = False Then
                'Debug.Print("Google - Error parsing starting result number!")
                Me.ParseErrorDetails = "Unable to parse starting result number!"
                'ErrorLog(sHTML, "google")
                'Return False
            Else
                Me.StartingResult = z
            End If

            'Now check the page for PageAd Style ads ("<td id=tpa")
            x = InStr(1, sHTML, "Sponsored Links", vbTextCompare)

            'If there are page ad results then parse them up
            If x > 0 Then
                s = Split(sHTML, "<li>", , vbTextCompare)

                If UBound(s) > 0 Then
                    WorkPageAd = New PageAd
                    Dim positionCounter As Integer = 1
                    For x = 0 To UBound(s)
                        If Not s(x).StartsWith("<h3>") Then
                            Continue For
                        End If

                        'Try to get the destination URL
                        z = Trim(StringFromTo(s(x), "adurl=", " ", False, False))

                        If z = "" Then
                            'Try the alternate parse method
                            z = Trim(StringFromTo(s(x), ";q=", Chr(34), False, False))
                        End If

                        If z = "" Then
                            'Try the alternate parse method
                            z = Trim(StringFromTo(s(x), "&q=", " ", False, False))
                        End If

                        If z = "" Then
                            'Try the alternate parse method
                            z = Trim(StringFromTo(s(x), "adurl=", " ", False, False))
                        End If


                        If z = "" Then
                            Me.ParseErrorDetails = "Unable to parse page ad url!"
                            ErrorLog(sHTML, "google")
                            Return False
                        End If

                        WorkPageAd.DestinationURL = z

                        'Try to get the Ad Title
                        z = Trim(StringFromTo(s(x), """>", "</a>", False, False))

                        If z = "" Then z = Trim(StringFromTo(s(x), """ >", "</a>", False, False))

                        z = StripHTMLTags(z)

                        If z = "" Then
                            Me.ParseErrorDetails = "Unable to parse page Ad Title!"
                            ErrorLog(sHTML, "google")
                            Return False
                        End If

                        WorkPageAd.Title = z

                        'Try to get the Ad Display URL
                        z = Trim(StringFromTo(s(x), "<span class=a>", "</span>", False, False))

                        If z = "" Then
                            z = Trim(StringFromTo(s(x), "<cite>", "</cite>", False, False))
                        End If

                        If z = "" Then
                            Me.ParseErrorDetails = "Unable to parse page Ad Display URL!"
                            ErrorLog(sHTML, "google")
                            Return False
                        End If

                        WorkPageAd.URLLine = z

                        'Try to get the Ad Display Text
                        z = Trim(StringFromTo(s(x), "</h3>", "<cite", False, False))

                        z = StripHTMLTags(z)
                        z = ReplaceExtendedChars(z)

                        If z = "" Then
                            Me.ParseErrorDetails = "Unable to parse page Ad Display Text!"
                            ErrorLog(sHTML, "google")
                            Return False
                        End If

                        WorkPageAd.Text = z
                        WorkPageAd.Position = positionCounter
                        positionCounter += 1

                        'Add this Page Ad to the current page's ads collection
                        Me.PageAds.Add(WorkPageAd)
                    Next x
                End If

            End If

            'Check for adwords results
            x = InStr(1, sHTML, "Sponsored Links</", vbTextCompare)

            If x > 0 Then

                s = Split(sHTML, "<li><h3>", , CompareMethod.Text)

                'z = StringFromTo(sHTML, "<a id=an", "See your ad here...</a>", True, False)

                'If z = "" Then
                '    z = StringFromTo(sHTML, "<a id=an", "<img width=1 height=1", True, False)
                'End If

                'If z = "" Then
                '    z = StringFromTo(sHTML, "<a id=an", "<img width=1 height=1", True, False)
                'End If

                'If z = "" Then
                '    z = StringFromTo(sHTML, "<a id=an", "<div id=res", True, False)
                'End If

                'If z = "" Then
                '    z = StringFromTo(sHTML, "<a id=an", "</div></div>", False, False)
                'End If

                's = Split(z, "<a id=an")

                For x = 1 To UBound(s)

                    If s(x) <> "" Then
                        WorkAdWord = New GoogleAdWord

                        z = StringFromTo(s(x), "adurl=", " ", False, False)

                        If z = "" Then
                            z = StringFromTo(s(x), "q=", Chr(34), False, False)
                        End If

                        If z = "" Then
                            z = StringFromTo(s(x), "q=", "&", False, False)
                        End If

                        If z = "" Then
                            z = StringFromTo(s(x), "q=", " ", False, False)
                        End If

                        If z = "" Then
                            Me.ParseErrorDetails = "Unable to parse AdWord Destination URL"
                            ErrorLog(sHTML, "google")
                            Return False
                        End If

                        WorkAdWord.DestinationURL = z
                        WorkAdWord.Position = x + adcount

                        z = StringFromTo(s(x), "<font size=+0>", "</font>", False, False)
                        If z = "" Then z = StringFromTo(s(x), ">", "</a>", False, False)
                        If z = " " Then z = "No Title"
                        z = StripHTMLTags(z)
                        z = ReplaceExtendedChars(z)

                        If z = "" Then
                            Me.ParseErrorDetails = "Unable to parse AdWord Title!"
                            ErrorLog(sHTML, "google")
                            Return False
                        End If

                        WorkAdWord.Title = z

                        z = StringFromTo(s(x), "</h3>", "<br>", False, False)
                        z = StripHTMLTags(z)
                        z = ReplaceExtendedChars(z)

                        '                If InStr(1, s(x), "affordablep", vbTextCompare) Then
                        '                    Stop
                        '                End If

                        If z = "" Then
                            Me.ParseErrorDetails = "Unable to parse AdWord Line 1!"
                            ErrorLog(sHTML, "google")
                            Return False
                        End If

                        WorkAdWord.TextLine1 = z

                        'If x = UBound(s) Then
                        's(x) = StringFromTo(s(x), "<br>", "pan>", False, False)
                        'Else
                        's(x) = StringFromTo(s(x), "<br>", "<br><br>", False, False)
                        'End If

                        z = StringFromTo(s(x), "<br>", "<br>", False, False)

                        z = StripHTMLTags(z)
                        z = ReplaceExtendedChars(z)

                        If z = "" Then
                            Me.ParseErrorDetails = "Unable to parse AdWord Line 2!"
                            ErrorLog(sHTML, "google")
                            Return False
                        End If

                        WorkAdWord.TextLine2 = z

                        z = StringFromTo(s(x), "<cite>", "</cite>", False, False)
                        y = InStr(1, s(x), z, vbTextCompare) 'Starting position for local info search

                        z = StripHTMLTags(z)
                        z = ReplaceExtendedChars(z)

                        If z = "" Then
                            Me.ParseErrorDetails = "Unable to parse AdWord URL Line!"
                            ErrorLog(sHTML, "google")
                            Return False
                        End If

                        WorkAdWord.URLLine = z

                        y = InStr(CInt(y) + 1, s(x), "<br>")

                        If y > 1 Then
                            'Get the local info
                            y = y + 3
                            z = Trim(Right(s(x), Len(s(x)) - y))

                            WorkAdWord.LocalInfo = z

                        End If

                        'Add the working adword to this page's adword collection
                        ' If it hasn't been added already
                        If Not Me.AdWords.Contains(WorkAdWord) Then
                            Me.AdWords.Add(WorkAdWord)
                        End If
                    End If
                Next x

            End If

            'Parse the Regular individual search results now

            'Narrow the HTML down to just the results section
            '        z = StringFromTo(sHTML, "<div class=g", "<td valign=bottom nowrap>", True, False)
            '
            '        If z = "" Then
            '            z = StringFromTo(sHTML, "<div class=g", "<br clear=all><br>", True, False)
            '        End If
            '
            '        If z = "" Then
            '            z = StringFromTo(sHTML, "<div class=g", "</div><br clear=all>", True, False)
            '        End If
            '
            '        If z = "" Then
            '            z = StringFromTo(sHTML, "<div class=g", "</div><br clear=""all""", True, False)
            '        End If
            '
            '        If z = "" Then
            '            z = StringFromTo(sHTML, "<div class=g", "<br clear=all>", True, False)
            '        End If
            '
            '        If z = "" Then
            '            z = StringFromTo(sHTML, "<div class=g", "id=footer", True, False)
            '        End If
            'Debug.Print Me.SearchTerm & " - " & Me.AdWords.Count

            'If Me.AdWords.Count = 0 Then Stop

            'If z = "" Then
            z = StringFromTo(sHTML, "<li class=g>", "<input type=hidden", True, False)
            'End If

            If z = "" Then
                z = StringFromTo(sHTML, "<li class=""g", "<input type=hidden", True, False)
            End If

            If z = "" Then
                z = StringFromTo(sHTML, "<li class=""g", "</body", True, False)
            End If

            If z = "" Then
                Me.ParseErrorDetails = "Unable to find regular google search results block..."
                ErrorLog(sHTML, "google")
                Return False
            End If

            s = Split(z, "<div class=""g")

            If UBound(s) < 1 Then
                s = Split(z, "<div class=g")
            End If

            If UBound(s) < 1 Then
                Me.ParseErrorDetails = "Unable to find regular google search results block..."
                ErrorLog(sHTML, "google")
                Return False
            End If

            For x = 4 To UBound(s)

                WorkResult = New SearchEngineResult

                If x = 1 And InStr(1, s(x), "Weather</b> for", vbTextCompare) Then
                    If InStr(2, s(x), "<table", vbBinaryCompare) Then
                        'Weather Item (sometimes appears in city name/weather searches)
                        WorkResult.Position = 1
                        WorkResult.Title = "Local Weather Report"
                        WorkResult.Text = "Local Weather Report"
                        WorkResult.URL = "N/A"

                        Me.Results.Add(WorkResult)

                        x = x + 1
                        WorkResult = New SearchEngineResult
                    End If
                End If

                If InStr(1, s(x), "product_result_group", vbTextCompare) Then
                    WorkResult.Position = x
                    WorkResult.Title = "Product Results"
                    WorkResult.Text = "Product Results"
                    WorkResult.URL = "N/A"

                    Me.Results.Add(WorkResult)
                ElseIf InStr(1, s(x), "http://maps.google.com", vbTextCompare) Then
                    WorkResult.Position = x
                    WorkResult.Title = "Maps Result"
                    WorkResult.Text = "Maps Result"
                    WorkResult.URL = "N/A"

                    Me.Results.Add(WorkResult)
                ElseIf InStr(1, s(x), "<div class=rbt>", vbTextCompare) Then
                    WorkResult.Position = x
                    WorkResult.Title = "Scores Result"
                    WorkResult.Text = "Scores Result"
                    WorkResult.URL = "N/A"

                    Me.Results.Add(WorkResult)
                ElseIf InStr(1, s(x), "<div class=med>See results", vbTextCompare) Then
                    WorkResult.Position = x
                    WorkResult.Title = "Alternate Results"
                    WorkResult.Text = "Alternate Results"
                    WorkResult.URL = "N/A"

                    Me.Results.Add(WorkResult)
                Else

                    z = StringFromTo(s(x), "href=" & Chr(34), Chr(34), False, False)
                    z = Replace(z, Chr(34), "")

                    If Left(z, 1) = "/" Then
                        z = StringFromTo(s(x), "q=", "&amp;sa=", False, False)
                    End If

                    If z = "" Then
                        z = StringFromTo(s(x), "q=", Chr(34), False, False)
                    End If

                    If z = "" Then
                        z = StringFromTo(s(x), "<cite>", "</cite>", False, False)
                        z = StripHTMLTags(z)
                    End If

                    If z = "" Then
                        Me.ParseErrorDetails = "Unable to Parse Regular Results URL!"
                        ErrorLog(sHTML, "google")
                        Return False
                    End If

                    z = System.Web.HttpUtility.UrlDecode(z)

                    z = z.Replace("%20", "+") ' make sure spaces are +

                    ' They seem to get double encoded sometimes...
                    If z.Contains("%") Then
                        z = System.Web.HttpUtility.UrlDecode(z)
                    End If

                    WorkResult.URL = z


                    If Len(WorkResult.URL) < 5 Then
                        'Stop
                    End If


                    'If z = "" Then
                    'z = StringFromTo(s(x), "l>", "</a>", False, False)
                    'End If

                    z = ""

                    If z = "" Then
                        z = StringFromTo(s(x), ")"">", "</a>", False, False)
                    End If

                    If z = "" Then
                        z = StringFromTo(s(x), """ >", "</a>", False, False)
                    End If

                    If z = "" Then
                        z = StringFromTo(s(x), """>", "</a>", False, False)
                    End If

                    z = StripHTMLTags(z)

                    If z = "" Then
                        z = StringFromTo(s(x), "class=l>", "</a>", False, False)
                        z = StripHTMLTags(z)
                    End If

                    If z = "" Then
                        z = StringFromTo(s(x), "class=""r"">", "</a>", False, False)
                        z = StripHTMLTags(z)
                    End If


                    If z = "" And InStr(1, s(x), "[PDF]", vbTextCompare) > 1 Then
                        z = "[PDF File]"
                    End If



                    If z = "" Then
                        Me.ParseErrorDetails = "Unable to parse regular results Title!"
                        ErrorLog(sHTML, "google")
                        Return False
                    End If

                    WorkResult.Title = z

                    If Me.StartingResult > 1 Then
                        WorkResult.Position = x + Me.StartingResult - 1
                    Else
                        WorkResult.Position = x
                    End If

                    z = StringFromTo(s(x), "<div class=""s"">", "<br><cite>", False, False)

                    If z = "" Then
                        z = StringFromTo(s(x), "<td class=j><font size=-1>", "<br><span class=a>", False, False)
                    End If

                    If z = "" Then
                        z = StringFromTo(s(x), "<div class=""s", "<br><cite>", True, False)
                        z = Right(z, Len(z) - InStr(1, z, ">", vbTextCompare))
                    End If

                    z = StripHTMLTags(z)

                    If z = "" Then
                        z = "(No Description)"
                        'Continue anyway however, some results don't have a description
                    End If

                    WorkResult.Text = z

                    z = StringFromTo(s(x), "<span class=a>", "</span>", False, False)

                    z = Replace(z, StringFromTo(z, "<span", "</span>", False, False), "")

                    s2 = Split(z, "-")

                    For y = 0 To UBound(s2)
                        If InStr(1, s2(y), ",", vbTextCompare) Then
                            If InStr(1, s2(y), ".", vbTextCompare) < 1 Then
                                On Error Resume Next
                                WorkResult.UpdatedDate = Trim(s2(y))
                                On Error GoTo 0
                            End If
                            Exit For
                        End If
                    Next y

                    Me.Results.Add(WorkResult)
                    End If

            Next x

        Else
            'If there aren't set the competition count to 0 and continue on
            Me.TotalResults = 0
        End If

        Me.ParseErrorDetails = ""

        Return True

    End Function

End Class
