Imports System.Collections.Generic
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.WebSite.Library
Imports CharterMobile.Utilities

Partial Class Controls_Channels
    Inherits System.Web.UI.UserControl

    Private m_TargetBrowserType As BrowserType

    Public Property TargetBrowserType() As BrowserType
        Get
            Return m_TargetBrowserType
        End Get
        Set(ByVal value As BrowserType)
            m_TargetBrowserType = value
        End Set
    End Property



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

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

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

        Set(ByVal value As String)
            ViewState(Me.ID & "_PrintPageUrl") = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Dim _address As IAddress
        Select Case TargetBrowserType
            Case BrowserType.StandardBrowser
                If Not Page.IsPostBack Then
                    lblHDTV1.Visible = HDTVOnly
                    lblHDTV2.Visible = HDTVOnly
                    lnkPrint.Visible = False
                    rprChannels.Visible = False
                    lblNotFound.Visible = False

                    '_address = AccountManager.Instance.GetAccount().Address
                    Dim street1 As String = String.Empty
                    Dim street2 As String = String.Empty
                    Dim zip As String = String.Empty
                    Dim st1, st2, objZip As HttpCookie

                    st1 = HttpContext.Current.Request.Cookies(CookieKey.LocalAddress1)
                    st2 = HttpContext.Current.Request.Cookies(CookieKey.LocalAddress2)
                    objZip = HttpContext.Current.Request.Cookies(CookieKey.LocalZip)
                    If st1 IsNot Nothing Then
                        street1 = CType(st1.Value, String)
                    End If

                    If st2 IsNot Nothing Then
                        street2 = CType(st2.Value, String)
                    End If

                    If objZip IsNot Nothing Then
                        zip = CType(objZip.Value, String)
                    End If

                    txtAddress1.Text = street1
                    txtAddress2.Text = street2
                    txtZipCode.Text = zip


                    If Page.User.IsInRole("member") Then
                        '#################################################
                        'WHEN HDTV, PRELOAD THE INPUT BOXES ON THE PAGE
                        '#################################################
                        If Request.QueryString(QueryStringName.ProductItem) = ProductItem.HighDefinitionTV Then
                            Dim _account As IAccount = AccountManager.Instance.GetAccount()
                            If Not _account Is Nothing Then
                                If Not _account.Address Is Nothing Then
                                    If Not _account.Address.Street1 Is Nothing Then
                                        txtAddress1.Text = _account.Address.Street1
                                    End If
                                    If Not _account.Address.Street2 Is Nothing Then
                                        txtAddress2.Text = _account.Address.Street2
                                    End If
                                    If Not _account.Address.Zip Is Nothing Then
                                        txtZipCode.Text = _account.Address.Zip
                                    End If
                                End If

                                'RenderChannels(street1, street2, zip, HDTVOnly)
                            End If

                        End If

                    End If
                End If
            Case Else
                If Not Page.IsPostBack Then
                    StandardCLULayout.Visible = False
                    GeneralMobileCLULayout.Visible = True

                    lblHDTV2Mobile.Visible = HDTVOnly
                    lnkPrintMobile.Visible = False
                    rprChannelsMobile.Visible = False
                    lblNotFoundMobile.Visible = False

                    '_address = AccountManager.Instance.GetAccount().Address
                    Dim street1 As String = String.Empty
                    Dim street2 As String = String.Empty
                    Dim zip As String = String.Empty
                    Dim st1, st2, objZip As HttpCookie

                    st1 = HttpContext.Current.Request.Cookies(CookieKey.LocalAddress1)
                    st2 = HttpContext.Current.Request.Cookies(CookieKey.LocalAddress2)
                    objZip = HttpContext.Current.Request.Cookies(CookieKey.LocalZip)
                    If st1 IsNot Nothing Then
                        street1 = CType(st1.Value, String)
                    End If

                    If st2 IsNot Nothing Then
                        street2 = CType(st2.Value, String)
                    End If

                    If objZip IsNot Nothing Then
                        zip = CType(objZip.Value, String)
                    End If

                    txtAddress1Mobile.Text = street1
                    txtAddress2Mobile.Text = street2
                    txtZipCodeMobile.Text = zip


                    If Page.User.IsInRole("member") Then
                        '#################################################
                        'WHEN HDTV, PRELOAD THE INPUT BOXES ON THE PAGE
                        '#################################################
                        If Request.QueryString(QueryStringName.ProductItem) = ProductItem.HighDefinitionTV Then
                            Dim _account As IAccount = AccountManager.Instance.GetAccount()
                            If Not _account Is Nothing Then
                                If Not _account.Address Is Nothing Then
                                    If Not _account.Address.Street1 Is Nothing Then
                                        txtAddress1Mobile.Text = _account.Address.Street1
                                    End If
                                    If Not _account.Address.Street2 Is Nothing Then
                                        txtAddress2Mobile.Text = _account.Address.Street2
                                    End If
                                    If Not _account.Address.Zip Is Nothing Then
                                        txtZipCodeMobile.Text = _account.Address.Zip
                                    End If
                                End If

                            End If

                        End If

                    End If
                End If
        End Select


    End Sub

    Protected Sub btnSeeChannelListMobile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSeeChannelListMobile.Click
        If Page.IsValid Then
            Dim zip As String = txtZipCodeMobile.Text.Substring(0, 5).ToString
            RenderChannelsMobile(txtAddress1Mobile.Text, txtAddress2Mobile.Text, zip, HDTVOnly)
        End If
    End Sub

    Protected Sub btnSeeChannelList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSeeChannelList.Click
        If Page.IsValid Then
            Dim zip As String = txtZipCode.Text.Substring(0, 5).ToString
            'RenderChannels(txtAddress1.Text, txtAddress2.Text, txtZipCode.Text, HDTVOnly)
            RenderChannels(txtAddress1.Text, txtAddress2.Text, zip, HDTVOnly)
        End If
    End Sub

    Private Sub RenderChannelsMobile(ByVal address1 As String, ByVal address2 As String, ByVal zipCode As String, ByVal isHDTVonly As Boolean)
        Dim lineup As IChannelLineUp

        lineup = SiteManager.Instance.GetChannelLineup(address1, address2, zipCode, isHDTVonly)

        If lineup IsNot Nothing AndAlso lineup.ChannelLineUp.Count <> 0 Then
            rprChannelsMobile.DataSource = lineup.ChannelLineUp
            rprChannelsMobile.DataBind()

            lnkPrintMobile.NavigateUrl = String.Format("{0}?addr1={1}&addr2={2}&zip={3}", PrintPageUrl, Server.UrlEncode(address1), _
            Server.UrlEncode(address2), Server.UrlEncode(zipCode))
            lnkPrintMobile.Visible = Not HDTVOnly
            rprChannelsMobile.Visible = True
            lblNotFoundMobile.Visible = False
        Else
            lnkPrintMobile.Visible = False
            rprChannelsMobile.Visible = False
            lblNotFoundMobile.Visible = True
        End If
    End Sub

    Private Sub RenderChannels(ByVal address1 As String, ByVal address2 As String, ByVal zipCode As String, ByVal isHDTVonly As Boolean)
        Dim lineup As IChannelLineUp

        lineup = SiteManager.Instance.GetChannelLineup(address1, address2, zipCode, isHDTVonly)

        If lineup IsNot Nothing AndAlso lineup.ChannelLineUp.Count <> 0 Then
            rprChannels.DataSource = lineup.ChannelLineUp
            rprChannels.DataBind()

            lnkPrint.NavigateUrl = String.Format("{0}?addr1={1}&addr2={2}&zip={3}", PrintPageUrl, Server.UrlEncode(address1), _
            Server.UrlEncode(address2), Server.UrlEncode(zipCode))
            lnkPrint.Visible = Not HDTVOnly
            rprChannels.Visible = True
            lblNotFound.Visible = False
        Else
            lnkPrint.Visible = False
            rprChannels.Visible = False
            lblNotFound.Visible = True
        End If
    End Sub


End Class
