Imports System.Reflection

Public MustInherit Class SearchEnginePage

    'Purpose:
    ' Base class for search engine page parsing

    'Begin Member Variables
    '------------------------------------------------------------------------------------------
    Private m_SearchTerm As String          'Term used for the search
    Private m_TotalResults As Double        'Total results reported
    Private m_StartingResult As Double      'The result number this search page started at
    Private m_Results As List(Of SearchEngineResult)     'Collection of regular results displayed on this page
    Private m_DataDate As Date              'Date the page was displayed/collected
    Private m_ParseErrorDetails As String   'Details on the parsing error if there was one
    Private m_processed As Boolean = False
    Dim m_PageAds As List(Of PageAd)
    Dim m_AdWords As List(Of AdWord)
    'Dim m_SuggestedKeyword As KeywordSuggestion
    '------------------------------------------------------------------------------------------

    'Class Initialization and Destruction Functions
    '------------------------------------------------------------------------------------------
    Public Sub New()
        m_Results = New List(Of SearchEngineResult)

    End Sub

    Protected Overrides Sub Finalize()

        m_Results = Nothing

        MyBase.Finalize()
    End Sub

    '------------------------------------------------------------------------------------------

    'Begin Public Properties and Functions
    '------------------------------------------------------------------------------------------
    Public Property Processed() As Boolean
        Get
            Return m_processed
        End Get
        Set(ByVal value As Boolean)
            m_processed = value
        End Set
    End Property

    'Public Property SuggestedKeyword() As KeywordSuggestion
    '    Get
    '        Return m_SuggestedKeyword
    '    End Get
    '    Set(ByVal value As KeywordSuggestion)
    '        m_SuggestedKeyword = value
    '    End Set
    'End Property

    Public Property SearchTerm() As String
        Get
            Return m_SearchTerm
        End Get
        Protected Set(ByVal value As String)
            m_SearchTerm = value
        End Set
    End Property

    Public Property TotalResults() As Double
        Get
            Return m_TotalResults
        End Get
        Protected Set(ByVal value As Double)
            m_TotalResults = value
        End Set
    End Property

    Public Property StartingResult() As Double
        Get
            Return m_StartingResult
        End Get
        Protected Set(ByVal value As Double)
            m_StartingResult = value
        End Set
    End Property

    Public Property PageAds() As List(Of PageAd)
        Get
            Return m_PageAds
        End Get
        Protected Set(ByVal value As List(Of PageAd))
            m_PageAds = value
        End Set
    End Property

    Public Property AdWords() As List(Of AdWord)
        Get
            Return m_AdWords
        End Get
        Protected Set(ByVal value As List(Of AdWord))
            m_AdWords = value
        End Set
    End Property

    Public Property Results() As List(Of SearchEngineResult)
        Get
            Return m_Results
        End Get
        Protected Set(ByVal value As List(Of SearchEngineResult))
            m_Results = value
        End Set
    End Property

    Public Property DataDate() As Date
        Get
            Return m_DataDate
        End Get
        Protected Set(ByVal value As Date)
            m_DataDate = value
        End Set
    End Property

    Public Property ParseErrorDetails() As String
        Get
            Return m_ParseErrorDetails
        End Get
        Protected Set(ByVal value As String)
            m_ParseErrorDetails = value
        End Set
    End Property

    Protected Sub ErrorLog(ByVal sHTML As String, ByVal engine As String)
        'Let the calling app know the page did not parse properly for some reason
        'Resume
        System.Diagnostics.Debug.WriteLine(Date.Now.ToString() & " : " & engine & " -> " & Err.Description & " - " & Me.ParseErrorDetails)

        If InStr(1, sHTML, "</html>", CompareMethod.Text) > 1 Then

            If Not System.IO.Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\" & Assembly.GetEntryAssembly().GetName().Name & "\") Then
                System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\" & Assembly.GetEntryAssembly().GetName().Name & "\")
            End If

            Dim file As New System.IO.StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\" & Assembly.GetEntryAssembly().GetName().Name & "\" & engine & "parse.log")
            file.Write(sHTML)
            file.Close()
        Else
            Me.ParseErrorDetails = "Incomplete HTML"
        End If

        If Err.Number > 0 Then
            'Store the error details for retrieval from the host app if needed

            Me.ParseErrorDetails = "Parse" & engine & engine & "Page() Error -> " & Err.Description & " -> " & Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Ad Word Analyzer\" & engine & "parse.log"
        End If
    End Sub

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

End Class

