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.CommonEntities.Library.Common
Imports CharterDotCom.WebSite.Library

Partial Class Controls_ManageWebSpace
    Inherits System.Web.UI.UserControl
    Dim _account As IAccount
    Dim _webspaceID As String

    Public strSectionTitle As String = "Manage My Web Space"

    Public Property Account() As IAccount
        Get
            Return _account
        End Get
        Set(ByVal value As IAccount)
            _account = value
        End Set
    End Property

    Dim outLink As String = String.Empty

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        ' Naqib Quarishi dec 17 08 - Get the content zone for pnlExistingUser.replace the placeholder {{ltlWebSpaceURL}} with the actual outlink
        If Not Page.IsPostBack Then
            introTxt.Text = ContentManager.Instance.GetContent("myaccount:managewebspace:existinguser", 1, OwnerType.Other)
            Call LoadPage()
            introTxt.Text = introTxt.Text.Replace("{{ltlWebSpaceURL}}", outLink)
        End If
        If pnlNewUser.Visible Then
            headerText.Text = "Create My Web Space" 'When we have the newpanel visible -naqib
        Else
            headerText.Text = "Manage My Web Space" 'This is the default header text - Naqib
        End If

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        linkbtnRemoveWebspace.Attributes("onclick") = "javascript:return " & _
                        "confirm('Are you sure you want to delete this webspace login?')"


        ltrMessage.ForeColor = Drawing.Color.Black
        ltrMessage.Text = ""

        If Not IsPostBack Then
            If Not (_account Is Nothing) Then
                If Not (_account.Person Is Nothing) Then
                    If Not (_account.Person.MSPackageInfo Is Nothing) Then

                        If Not (ddlMSPackage Is Nothing) Then
                            If Not (ddlMSPackage.Items Is Nothing) Then
                                If ddlMSPackage.Items.Count < 1 Then
                                    'ddlMSPackage.Items.Add(New ListItem("-- Choose One --", "")) 'ADD BLANK ROW

                                    For Each MSPackage As IAccount.IMember.IMemberServicePackageInfo In _account.Person.MSPackageInfo
                                        If Not (MSPackage Is Nothing) Then
                                            If Not (MSPackage.descriptionField() Is Nothing) Then
                                                If MSPackage.descriptionField.IndexOf(",") > 0 Then
                                                    Dim lDisplayName As String = MSPackage.descriptionField.Substring(0, MSPackage.descriptionField.IndexOf(","))
                                                    ddlMSPackage.Items.Add(New ListItem(lDisplayName, MSPackage.provisioningIdField & MSPackage.descriptionField()))
                                                Else
                                                    ddlMSPackage.Items.Add(New ListItem(MSPackage.descriptionField, MSPackage.provisioningIdField & MSPackage.descriptionField()))
                                                End If
                                            End If
                                        End If
                                    Next

                                    '##################################################
                                    'PRESELECT THE FIRST PACKAGE IN THE DROPDOWN LIST
                                    '##################################################
                                    If ddlMSPackage.Items.Count > 0 Then
                                        ddlMSPackage.SelectedIndex = 0
                                    End If
                                End If
                            End If
                        End If

                    End If
                End If
            End If
        End If

        Call LoadPage()

        '#######################################################
        'MAKE MS PACKAGE DROPDOWN PANEL NOT VISIBLE SINCE THERE 
        'IS ONLY ONE PACKAGE AND NO MORE TO SELECT FROM
        '#######################################################
        If Not (ddlMSPackage Is Nothing) Then
            If Not (ddlMSPackage.Items Is Nothing) Then
                If ddlMSPackage.Items.Count = 1 Then
                    pnlMSPackageDropDown.Visible = False
                End If
            End If
        End If
    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSubmit.Click
        'Dim _account As IAccount
        Dim _succeeded As Boolean
        Dim sb As StringBuilder = New StringBuilder()

        If Page.IsValid Then

            '##########################################################################
            ' 10/4/07 Brad Miller - changed validation format from bullet list to table
            '##########################################################################
            If txtProvisioningID.Text().Trim().Length() > 0 Then
                If txtUserName.Text().Trim().Length() > 0 Then
                    If txtPassword.Text().Trim().Length() > 0 Then
                        If txtPassword.Text().Trim().Length() > 7 Then
                            If txtPassword2.Text().Trim().Length() > 0 Then
                                If txtPassword.Text().Trim() = txtPassword2.Text().Trim() Then
                                    _succeeded = AccountManager.Instance.CreateWebSpaceAccount(txtUserName.Text.ToLower(), txtPassword.Text, txtProvisioningID.Text)
                                    If _succeeded Then
                                        sb.Append("New Web Space created successfully.")
                                        Response.Redirect("~/Account/ManageWebSpace.aspx")
                                    Else
                                        '##################################################################################
                                        '##########     E R R O R   C O D E   M A P P I N G    S O L U T I O N      #######
                                        '#################          MODIFIED BY ROD COPELAND, 5/2/2007     ################ 
                                        '##################################################################################
                                        'WRITE THE (IDEALLY) CHARTER FRIENDLY ERROR MESSAGES TO THE PAGE.  
                                        'THESE MESSAGES ARE MANAGED IN THE CHARTERDOTCOM.ADMIN.ERROR_CODE_MAPPING TABLE.
                                        'THE UNDERLYING LOGIC WILL DISPLAY THE DATASOURCE ERROR MESSAGE IF A 
                                        'CHARTERDOTCOM FRIENDLY ERROR MESSAGE IS NOT PRESENT ON THE RECORD.
                                        'THERE MAY BE MORE THAN ONE ERROR MESSAGE THAT COMES BACK, DISPLAY 
                                        'ACCORDINGLY.
                                        '##################################################################################
                                        ltrMessage.ForeColor = Drawing.Color.Red
                                        Select Case True
                                            Case Not AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage Is Nothing
                                                sb.Append(AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage & "<br />")
                                                AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()
                                            Case Else
                                                sb.Append("Failed to create your new Web Space.<br />")
                                        End Select
                                        '##################################################################################
                                    End If
                                Else
                                    ltrMessage.ForeColor = Drawing.Color.Red
                                    'PASSWORD AND NEW PASSWORD DONT MATCH
                                    sb.Append("<b>Confirm Password</b> does not match <b>Password</b>.<br/>")
                                End If
                            Else
                                ltrMessage.ForeColor = Drawing.Color.Red
                                'NEED A VALUE FOR THE PASSWORD 2
                                sb.Append("<b>Confirm Password</b> is a required field.<br/>")
                            End If
                        Else
                            ltrMessage.ForeColor = Drawing.Color.Red
                            'NEED ATLEAST 8 CHARACTERS FOR PASSWORD
                            sb.Append("<b>Password</b> must be at least 8 characters.<br/>")
                        End If
                    Else
                        ltrMessage.ForeColor = Drawing.Color.Red
                        'NEED A VALUE FOR PASSWORD
                        sb.Append("<b>Password</b> is a required field.<br/>")
                    End If
                Else
                    ltrMessage.ForeColor = Drawing.Color.Red
                    'NEED A VALUE FOR USER NAME
                    sb.Append("<b>Username</b> is a required field.<br/>")
                End If
            Else
                ltrMessage.ForeColor = Drawing.Color.Red
                sb.Append("Unable to complete your request at this time.<br/>")
            End If
            '################################################

            Call LoadPage()
            If sb.Length > 0 Then
                If _succeeded Then
                    ltrMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/sup_icon_successChange.ccom' /> " & String.Format("{0}", sb.ToString()) & "</div>"
                Else
                    ltrMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & String.Format("{0}", sb.ToString()) & "</div>"
                End If

                ltrMessage.Visible = True
            Else
                ltrMessage.Visible = False
            End If

        End If
    End Sub

    Private Sub LoadPage()
        '###############################
        'CLEAR THE ERROR MESSAGE LABLE
        '###############################
        ltrEditWebSpacePWMessage.Text = ""
        ltrRemoveWebspaceMessage.Text = ""
        ltrMessage.Text = ""
        ltrMessage.Visible = False

        If ddlMSPackage.Items.Count > 0 Then
            '#############################
            'SHOW THE CORRESPONDING PAGE
            '#############################
            Dim intSelectedValue As Integer = ddlMSPackage.SelectedIndex
            If Not (_account Is Nothing) Then
                If Not (_account.Person Is Nothing) Then
                    If Not (_account.Person.MSPackageInfo Is Nothing) Then
                        If Not (_account.Person.MSPackageInfo(intSelectedValue).provisioningIdField Is Nothing) Then
                            txtProvisioningID.Text = _account.Person.MSPackageInfo(intSelectedValue).provisioningIdField
                        End If
                        If Not (_account.Person.MSPackageInfo(intSelectedValue).webspaceField Is Nothing) Then
                            If (_account.Person.MSPackageInfo(intSelectedValue).webspaceField.Trim() <> "") Then
                                _webspaceID = _account.Person.MSPackageInfo(intSelectedValue).webspaceField
                                outLink = "<a href=""http://webpages.charter.net/" & _account.Person.MSPackageInfo(intSelectedValue).webspaceField & """ target=""_blank"">http://webpages.charter.net/" & _account.Person.MSPackageInfo(intSelectedValue).webspaceField & "</a>"
                                lblWebSpaceName.Text = _account.Person.MSPackageInfo(intSelectedValue).webspaceField.Trim()
                                pnlNewUser.Visible = False
                                pnlExistingUser.Visible = True
                                pnlEditPassword.Visible = False
                            Else
                                pnlNewUser.Visible = True
                                pnlExistingUser.Visible = False
                                pnlEditPassword.Visible = False
                            End If
                        Else
                            pnlNewUser.Visible = True
                            pnlExistingUser.Visible = False
                            pnlEditPassword.Visible = False
                        End If

                        If Not (_account.Person.MSPackageInfo(intSelectedValue).descriptionField Is Nothing) Then
                            lblPackageDescription.Text = _account.Person.MSPackageInfo(intSelectedValue).descriptionField
                        End If

                    End If
                End If
            End If
        Else
            '########################################
            'CLEAR THE PAGE EXCEPT FOR THE DROP LIST
            '########################################
            txtProvisioningID.Text = ""
            pnlNewUser.Visible = False
            pnlExistingUser.Visible = False
            pnlEditPassword.Visible = False
        End If

    End Sub

    Protected Sub btnChangePassword_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnChangePassword.Click
        Dim lWebSpaceID As String = txtWebspaceAccount_EditPW.Text
        Dim lPW As String = txtPassword_EditPW.Text
        Dim blnResponse As Boolean = AccountManager.Instance.ChangeWebSpaceAccountPassword(lWebSpaceID, lPW, txtProvisioningID.Text)

        If blnResponse Then
            pnlNewUser.Visible = False
            pnlExistingUser.Visible = True
            pnlEditPassword.Visible = False
        Else
            '##################################################################################
            '##########     E R R O R   C O D E   M A P P I N G    S O L U T I O N      #######
            '#################          MODIFIED BY ROD COPELAND, 5/2/2007     ################ 
            '##################################################################################
            'WRITE THE (IDEALLY) CHARTER FRIENDLY ERROR MESSAGES TO THE PAGE.  
            'THESE MESSAGES ARE MANAGED IN THE CHARTERDOTCOM.ADMIN.ERROR_CODE_MAPPING TABLE.
            'THE UNDERLYING LOGIC WILL DISPLAY THE DATASOURCE ERROR MESSAGE IF A 
            'CHARTERDOTCOM FRIENDLY ERROR MESSAGE IS NOT PRESENT ON THE RECORD.
            'THERE MAY BE MORE THAN ONE ERROR MESSAGE THAT COMES BACK, DISPLAY 
            'ACCORDINGLY.
            '##################################################################################
            Select Case True
                Case Not AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage Is Nothing
                    ltrEditWebSpacePWMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage & "</div>"
                    AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()
                Case Else
                    ltrEditWebSpacePWMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & "Sorry, we're unable to complete your request at this time." & "</div>"
            End Select
            '##################################################################################
            pnlNewUser.Visible = False
            pnlExistingUser.Visible = False
            pnlEditPassword.Visible = True
        End If
    End Sub

    Protected Sub linkbtnChangeWebSpacePassword_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles linkbtnChangeWebSpacePassword.Click
        pnlNewUser.Visible = False
        pnlExistingUser.Visible = False
        pnlEditPassword.Visible = True
        txtWebspaceAccount_EditPW.Text = _webspaceID
    End Sub

    Protected Sub linkbtnRemoveWebspace_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles linkbtnRemoveWebspace.Click
        Dim blnResult As Boolean = AccountManager.Instance.RemoveWebSpaceAccount(txtProvisioningID.Text)

        Call LoadPage()


        If blnResult Then
            ltrMessage.Visible = False
        Else
            '##################################################################################
            '##########     E R R O R   C O D E   M A P P I N G    S O L U T I O N      #######
            '#################          MODIFIED BY ROD COPELAND, 5/2/2007     ################ 
            '##################################################################################
            'WRITE THE (IDEALLY) CHARTER FRIENDLY ERROR MESSAGES TO THE PAGE.  
            'THESE MESSAGES ARE MANAGED IN THE CHARTERDOTCOM.ADMIN.ERROR_CODE_MAPPING TABLE.
            'THE UNDERLYING LOGIC WILL DISPLAY THE DATASOURCE ERROR MESSAGE IF A 
            'CHARTERDOTCOM FRIENDLY ERROR MESSAGE IS NOT PRESENT ON THE RECORD.
            'THERE MAY BE MORE THAN ONE ERROR MESSAGE THAT COMES BACK, DISPLAY 
            'ACCORDINGLY.
            '##################################################################################
            Select Case True
                Case Not AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage Is Nothing
                    ltrRemoveWebspaceMessage.Text = AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage
                    AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()
                Case Else
                    ltrRemoveWebspaceMessage.Text = "Sorry, we're unable to complete your request at this time."
            End Select
            '##################################################################################

            ltrMessage.Visible = True
        End If

    End Sub

    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnReset.Click
        Response.Redirect("ManageWebSpace.aspx")
    End Sub
End Class
