Imports CharterDotCom.CommonEntities.Library.Enumerations
Imports CharterDotCom.WebSite.Library
Imports CharterDotCom.CommonEntities.Library
Imports System.Collections.Generic
Partial Class Controls_SupportArticleRanking
    Inherits System.Web.UI.UserControl

    Private QryKey As String = "supportArticleID"

    Public Sub RateArticle(ByVal supportArticleID As Integer, ByVal rating As Integer)
        Dim _comment As IAddDocumentCommentsRequest

        _comment = New BLAddDocumentCommentsRequest()
        With _comment
            .DocumentID = supportArticleID
            .Helpful = False
            ' issue 28388
            If Not (String.IsNullOrEmpty(txtComments.Text)) Then
                .Comments = txtComments.Text
            Else
                .Comments = ""
            End If
            .Rating = rating
            .CreatedBy = String.Empty
        End With

        SiteManager.Instance.AddDocumentComments(_comment)

    End Sub

    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSend.Click
        'save rating
        Dim rank As Integer = 0

        If (Not Me.rdoComment.SelectedValue Is Nothing And Me.rdoComment.SelectedValue <> "") Then
            rank = Me.rdoComment.SelectedValue
        End If

        If (Not Request.QueryString(QryKey) Is Nothing) And rank > 0 Then
            Me.pnlArticleRanking.Visible = False
            Me.pnlArticleRankingThanks.Visible = True
            RateArticle(CType(Request.QueryString(QryKey), Integer), Me.rdoComment.SelectedValue)
        End If
    End Sub

    ' issue 28388
    '################################################################
    ' display comments for selected articles
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            DisplayCommentBoxIfNeeded()
        End If
    End Sub

    Private Sub DisplayCommentBoxIfNeeded()
        Dim articleList As List(Of String) = Nothing
        Dim selectedArticle As String = Nothing
        Dim bDisplay As Boolean = False

        ' hide comment area
        commentRow.Visible = False
        If (Request.QueryString(QryKey) Is Nothing) Then
            Return
        End If

        ' display comments area if a special support article ID is being rated
        articleList = GetArticlesToCommentOn()
        If articleList.Count = 0 Then
            Return
        End If

        selectedArticle = Request.QueryString(QryKey).ToString()
        bDisplay = False
        For i As Integer = 0 To articleList.Count - 1
            If String.Compare(selectedArticle, articleList(i), True) = 0 Then
                bDisplay = True
            End If
        Next

        commentRow.Visible = bDisplay
    End Sub

    Private Function GetArticlesToCommentOn() As List(Of String)
        ' todo: jg - example
        Dim articleList As String
        Dim configKey As String = "FeedbackTextArticles"
        Dim returnVal As New List(Of String)
        Dim articleArray As Array

        If IsNothing(Application(configKey)) Then
            Return New List(Of String)
        End If

        articleList = Application(configKey).ToString().Trim()
        articleArray = articleList.Split(",")

        For i As Integer = 0 To articleArray.GetLength(0) - 1
            If Not (String.IsNullOrEmpty(articleArray(i))) Then
                returnVal.Add(articleArray(i).trim)
            End If
        Next

        Return returnVal
    End Function
    ' end issue 28388 additions
    '################################################################
End Class