Imports EDSL.Entities
Imports EDSL.DAL
Partial Public Class CaseStudyListControl
    Inherits System.Web.UI.UserControl
    Dim intLimit As Integer
    Dim intTotalPageCount As Integer
    Const m_DISPLAYEDROWS = 7
    Const m_PAGESIZE As Integer = 7

    Dim clsMisc As New miscFunctionsFormatString

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        btnFirst.PostBackUrl = "~/CaseStudies.html?cst=" & Request.QueryString("cst")
        btnLast.PostBackUrl = "~/CaseStudies.html?cst=" & Request.QueryString("cst")
        btnNext.PostBackUrl = "~/CaseStudies.html?cst=" & Request.QueryString("cst")
        btnPrev.PostBackUrl = "~/CaseStudies.html?cst=" & Request.QueryString("cst")
        btnFirstBottom.PostBackUrl = "~/CaseStudies.html?cst=" & Request.QueryString("cst")
        btnLastBottom.PostBackUrl = "~/CaseStudies.html?cst=" & Request.QueryString("cst")
        btnNextBottom.PostBackUrl = "~/CaseStudies.html?cst=" & Request.QueryString("cst")
        btnPrevBottom.PostBackUrl = "~/CaseStudies.html?cst=" & Request.QueryString("cst")


        If IsPostBack = False Then
            If Request.QueryString("pageid") <> "" Then
                CurrentPage = Request.QueryString("pageid") - 1
            End If
            Call LoadData()

        End If


    End Sub
    Private Sub LoadData()
        Dim objMainCaseStudy As List(Of entMainCaseStudy)
        Dim caseStudyType As New Integer

        If Request.QueryString("cst") <> "" Then
            caseStudyType = Request.QueryString("cst")
        End If

        Dim objCategory As New dbMainCategory
        Dim category As New entmainCategory
        category = objCategory.GetItem(caseStudyType)

        'Find page control for meta description
        DirectCast(Me.Page, CaseStudy).SetMetaData("", category.CategoryName.ToString() & " Case Studies")

        'Find div on master page
        Dim masterPage As main = DirectCast(Me.Page.Master, main)
        masterPage.BodyTag.Attributes.Add("class", category.DropDownValue.ToString())

        'Find the breadcrumb control
        Dim control As UserControl = CType(Me.Parent.FindControl("breadcrumb"), Breadcrumb)
        Dim pageInfo As HyperLink = control.FindControl("lnkPage")
        pageInfo.Text = category.CategoryName.ToString() & " Case Studies"

        'Find the page title
        Dim lbl As Label = Me.Parent.FindControl("labelPageTitle")
        lbl.Text = category.CategoryName.ToString() & " Case Studies"

        objMainCaseStudy = GetByTypeID(caseStudyType)

        If objMainCaseStudy.Count > 0 Then
            Dim objPds As New PagedDataSource
            objPds.DataSource = objMainCaseStudy
            objPds.AllowPaging = True
            objPds.PageSize = m_DISPLAYEDROWS
            objPds.CurrentPageIndex = CurrentPage
            rptCaseStudies.DataSource = objPds
            rptCaseStudies.DataBind()

            btnFirst.Enabled = IIf(objPds.IsFirstPage, False, True)
            btnPrev.Enabled = Not (objPds.IsFirstPage)
            btnNext.Enabled = Not (objPds.IsLastPage)
            btnLast.Enabled = IIf(objPds.IsLastPage, False, True)
            btnFirstBottom.Enabled = IIf(objPds.IsFirstPage, False, True)
            btnPrevBottom.Enabled = Not (objPds.IsFirstPage)
            btnNextBottom.Enabled = Not (objPds.IsLastPage)
            btnLastBottom.Enabled = IIf(objPds.IsLastPage, False, True)

            TotalPageCount = objPds.PageCount
            lblCurrentPage.Text = CurrentPage + 1 & " of " & TotalPageCount.ToString()
            lblCurrentPageBottom.Text = CurrentPage + 1 & " of " & TotalPageCount.ToString()
            Dim intLow As Integer = objPds.CurrentPageIndex - 1
            If objPds.CurrentPageIndex - 1 < 1 Then intLow = 1
            If intLow > TotalPageCount - m_PAGESIZE Then intLow = TotalPageCount - m_PAGESIZE + 1
            If intLow < 1 Then intLow = 1

            Dim intHigh As Integer = objPds.CurrentPageIndex + 3
            If objPds.CurrentPageIndex + 3 >= objPds.PageCount Then
                intHigh = objPds.PageCount
            ElseIf intHigh < m_PAGESIZE Then
                intHigh = m_PAGESIZE
            End If

            Dim strPageIndicator As String = ""
            For intCount = intLow To intHigh
                Dim strSelected = ""
                If intCount - 1 = objPds.CurrentPageIndex Then
                    strSelected = "yellow"
                End If
                strPageIndicator += "<a href=""CaseStudies.html?cst=" & caseStudyType & "&pageid=" & intCount & """ class=""" & strSelected & """>" & intCount & "</a>"
            Next intCount
            divPageIndicator.InnerHtml = strPageIndicator
            divPageIndicatorBottom.InnerHtml = strPageIndicator
        Else
            Dim strOutput As String = "<div id=""divCatalogueRow"" class=""divCatalogueRow""" & ">Sorry no news articles were found</div>"
            'divctlContent.InnerHtml = strOutput
        End If

    End Sub

    Private Function GetAll() As List(Of entMainCaseStudy)
        Dim objMain As New dbMainCaseStudy
        Dim objMainEntity As List(Of entMainCaseStudy) = objMain.GetAll(" AND Active = 1 AND isCaseStudy=true", "", LimitCaseStudylist)
        Return objMainEntity
    End Function
    Private Function GetByTypeID(ByVal caseStudyTypeID As Integer) As List(Of entMainCaseStudy)
        Dim objMain As New dbMainCaseStudy
        Dim objMainEntity As List(Of entMainCaseStudy) = objMain.GetItemsByType(caseStudyTypeID)
        Return objMainEntity
    End Function

    Private Function CheckStringValue(ByVal strInput As String) As String
        Dim strOutput As String = ""
        If Not strInput Is Nothing AndAlso strInput <> "" Then
            strOutput = strInput & "<br />"
        End If
        Return strOutput
    End Function

    Public Function CleanUrlRewriter(ByVal strInput As String) As String
        Dim strOutput As String = ""
        Dim func As New miscFunctions
        strOutput = func.CleanUrlString(strInput)

        Return strOutput
    End Function
    Public Property LimitCaseStudylist() As Integer
        Get
            Return intLimit
        End Get
        Set(ByVal value As Integer)
            intLimit = value
        End Set
    End Property
#Region "Internal properties"
    Private Property CurrentPage() As Integer
        Get
            Return CInt(Me.ViewState("_CurrentPage"))
        End Get
        Set(ByVal value As Integer)
            Me.ViewState("_CurrentPage") = value
        End Set
    End Property
    Private Property TotalPageCount() As Integer
        Get
            Return Me.ViewState("_TotalPageCount")
        End Get
        Set(ByVal value As Integer)
            Me.ViewState("_TotalPageCount") = value
        End Set
    End Property
#End Region
#Region "Nav control code"
    Private Sub btnFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFirst.Click, btnFirstBottom.Click
        CurrentPage = 0
        Call LoadData()
    End Sub
    Private Sub btnPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrev.Click, btnPrevBottom.Click
        CurrentPage -= 1
        Call LoadData()
    End Sub
    Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click, btnNextBottom.Click
        CurrentPage += 1
        Call LoadData()
    End Sub
    Private Sub btnLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLast.Click, btnLastBottom.Click

        CurrentPage = TotalPageCount - 1
        Call LoadData()
    End Sub
#End Region
End Class
