Imports System.Collections.Generic
Imports System.Xml
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.CommonEntities.Library.Enumerations
Imports CharterDotCom.CommonEntities.Library.EntityMemberServices
'Imports CharterDotCom.CommonEntities.Library.EntityEBSServices
Imports CharterDotCom.CommonEntities.Library.EntitySupport
'Imports CharterDotCom.CommonEntities.Library.EntitySearch
Imports CharterDotCom.CommonEntities.Library.EntityWebConfig
'Imports CharterDotCom.CommonEntities.Library.EntityG2BServices
Imports CharterDotCom.CommonEntities.Library.Common
Imports CharterDotCom.WebSite.Library

Partial Class Controls_SearchResults
    Inherits System.Web.UI.UserControl

    Private _document As XmlDocument
    Private _node As XmlNode

    Public Property SupportOnly() As Boolean
        Get
            If ViewState(Me.ID & "_SupportOnly") Is Nothing Then
                Return False
            Else
                Return CType(ViewState(Me.ID & "_SupportOnly"), Boolean)
            End If
        End Get

        Set(ByVal value As Boolean)
            ViewState(Me.ID & "_SupportOnly") = value
        End Set
    End Property

    Public Property Criteria() As String
        Get
            If ViewState(Me.ID & "_Criteria") Is Nothing Then
                Return String.Empty
            Else
                Return CType(ViewState(Me.ID & "_Criteria"), String)
            End If
        End Get

        Set(ByVal value As String)
            ViewState(Me.ID & "_Criteria") = value
            grdSearchResults.CurrentPageIndex = 0
        End Set
    End Property

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim _resultCollection As ISearchResultCollection
        Dim _results As List(Of ISearchResult)
        Dim totalCount As Integer
        Dim count As Integer
        Dim pageIndex As Integer

        If Criteria = String.Empty Then
            grdSearchResults.Visible = False
            ltrSearchResults.Visible = False
            lblCriteria.Visible = False
        Else
            ltrSearchResults.Visible = True
            lblCriteria.Visible = True

            If SupportOnly Then
                _resultCollection = SiteManager.Instance.SearchSupport(Criteria)
            Else
                _resultCollection = SiteManager.Instance.SearchSite(Criteria)
            End If

            _results = _resultCollection.results

            _document = New XmlDocument()
            _document.LoadXml(CType(Application(ApplicationKey.SearchXml), String))

            grdSearchResults.DataSource = _results

            grdSearchResults.DataBind()

            totalCount = _results.Count
            count = grdSearchResults.Items.Count
            pageIndex = grdSearchResults.CurrentPageIndex
            'If grdSearchResults.Items.Count <= 10 Then
            '  grdSearchResults.AllowPaging = False
            'Else
            '  grdSearchResults.AllowPaging = True
            'End If
            lblCriteria.Text = "Search Results for: " & Criteria

            If totalCount = 0 Then
                ltrSearchResults.Text = "No articles were found."
                grdSearchResults.Visible = False
            Else
                ltrSearchResults.Text = "Showing " & (pageIndex * 10 + 1).ToString() & "-" & (pageIndex * 10 + count).ToString() & " of " & totalCount & " results"
                grdSearchResults.Visible = True
            End If
        End If
    End Sub

    Protected Sub grdSearchResults_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdSearchResults.ItemCreated
        If (e.Item.ItemType = ListItemType.Pager) Then
            Dim strNewPager As String = "Page "
            Dim Pager As TableCell = CType(e.Item.Controls(0), TableCell)
            Pager.Controls.AddAt(0, New LiteralControl(strNewPager))
            For Each ctl As Control In e.Item.Controls(0).Controls
                Try
                    If ctl.GetType.ToString = "System.Web.UI.WebControls.DataGridLinkButton" Then
                        Dim anch As WebControls.LinkButton = CType(ctl, WebControls.LinkButton)
                        anch.OnClientClick = "javascript:fnChanged(this.href)"
                    End If
                Catch ex As Exception
                End Try
            Next
        End If
    End Sub

    Protected Sub grdSearchResults_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles grdSearchResults.PageIndexChanged
        grdSearchResults.CurrentPageIndex = e.NewPageIndex
        grdSearchResults.DataBind()
    End Sub

    Public Function RenderUrl(ByVal arg0 As Object, ByVal arg1 As Object) As String
        Dim _url As String
        Dim _mimeType As MimeType
        Dim _tokens As String()

        _url = CType(arg0, String)
        _mimeType = CType(arg1, MimeType)

        If _mimeType = MimeType.CONTENT_ARTICLE_FROM_DB Then
            _tokens = _url.Split(",")

            _node = _document.SelectSingleNode(String.Format("/search/page[@controlID='{0}']", _tokens(0)))

            If _node IsNot Nothing Then
                If _tokens(2) = OwnerType.Other Then
                    Return _node.Attributes("url").Value
                Else
                    Return String.Format("../Visitors/Localize.aspx?{0}={1}", QueryStringName.TargetPage, Server.HtmlDecode(_node.Attributes("url").Value))
                End If
            Else
                Return "#"
            End If
        Else
            Return _url
        End If
    End Function

    Public Function RenderTarget(ByVal _object As Object) As String
        Dim _mimeType As MimeType

        _mimeType = CType(_object, MimeType)

        If _mimeType = MimeType.SUPPORT_ARTICLE_FROM_DB Or _mimeType = MimeType.CONTENT_ARTICLE_FROM_DB Then
            Return "_self"
        Else
            Return "_charter_article"
        End If
    End Function
End Class