Imports System.Collections.Generic
Imports System.Web.Security
Imports System.Text.RegularExpressions
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_RetrieveUsernameStep4
	Inherits System.Web.UI.UserControl
	Implements IMyAccountStepPage

	Private m_sError As String
    Private m_myQueryString As String
    Private m_MyAccount As IRetUNameAccount

#Region " Properties "

    Public Property QueryKey() As String
        Get
            Return m_myQueryString
        End Get
        Set(ByVal value As String)
            m_myQueryString = value
        End Set
    End Property

    Public ReadOnly Property ErrorMessage() As String Implements IMyAccountStepPage.ErrorMessage
        Get
            Return m_sError
        End Get
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'This complicated check keeps the amount of work done on ever other step's load from running.  Because this is a control
        'being loaded up from a parent page, each page load will try and instantiate and run this event.
        'by checking the querystring value to make sure it's "our turn" we can avoid unnecessarily running the
        'LoadAccount routine for all the other sub-controls "turns".
        If (Not IsPostBack And (Not Request.QueryString(QueryStringName.MyAccountStep) Is Nothing)) AndAlso Request.QueryString(QueryStringName.MyAccountStep) = Me.QueryKey Then
            If LoadAccount() Then
                lblUserName.Text = m_MyAccount.UserName
            End If
        End If
    End Sub

#End Region

#Region "Subs and Functions "
#Region "InterfaceHandling"



    Public Function DoWork() As Boolean Implements IMyAccountStepPage.DoWork

        ' user may EITHER
        ' - log in with current password
        ' OR
        ' - enter new password and confirm it
        ' - which requires both a "forgot user name" call AND a login call

        '----------------------------------------------
        ' Normal Login with current password 
        If txtCurrPassword.Text.Length >= 8 Then
            ' login
            Return NormalLogin()
        End If
        '----------------------------------------------
        ' Change password AND login
        Return ChangePasswordAndLogin()

    End Function
#End Region

    Private Function NormalLogin() As Boolean
        If LoadAccount() Then
            If Utilities.LoginHandler.Login(Me.Context, True, _
                                            lblUserName.Text, txtCurrPassword.Text, _
                                            m_MyAccount.ZipCode, Nothing, False) Then
                Return True
            End If
        End If

        m_sError = GetAndClearError()
        Return False

    End Function

    Private Function ChangePasswordAndLogin() As Boolean
        If txtPassword.Text.Length = 0 Then
            m_sError = "You must supply a valid password."
            Return False
        End If

        m_sError = ""
        ' the validators are not automatically performed. only done if needed (i.e. if we are
        ' in the change-password flow.
        m_sError = PasswordValidations()
        If Not (String.IsNullOrEmpty(m_sError)) Then
            txtPassword.Focus()
            Return False
        End If

        If txtPassword.Text <> txtConfirmPwd.Text Then
            m_sError = "Passwords do not match."
            Return False
        End If

        ' change password
        ' reload local account variable if successful
        ' login if successful
        If AccountManager.Instance.ChangeMyAccountPasswordFromForgotUserName(txtPassword.Text) _
            AndAlso _
            LoadAccount() _
           AndAlso _
                Utilities.LoginHandler.Login(Me.Context, True, _
                                                lblUserName.Text, txtPassword.Text, _
                                                m_MyAccount.ZipCode, Nothing, False) Then
            Return True
        End If

        ' either a change password or login failure - return an error
        m_sError = GetAndClearError()
        Return False

    End Function

    Private Function LoadAccount() As Boolean
        Try
            m_MyAccount = AccountManager.Instance.GetMyAccountRetUNameAccount()
            If Not (m_MyAccount Is Nothing) Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            m_sError = ex.Message
            Return False
        End Try
    End Function

    Private Function GetAndClearError() As String
        Dim m_sError As String = AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage
        AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()
        Return m_sError
    End Function

#Region "Validations"
    Private Function PasswordValidations() As String
        Dim sMsg As String = String.Empty

        sMsg = SingleValidator(revLowercase)
        If Not (String.IsNullOrEmpty(sMsg)) Then
            Return sMsg
        End If

        sMsg = SingleValidator(revLength)
        If Not (String.IsNullOrEmpty(sMsg)) Then
            Return sMsg
        End If

        sMsg = SingleValidator(revNumber)
        If Not (String.IsNullOrEmpty(sMsg)) Then
            Return sMsg
        End If

        sMsg = SingleValidator(revUppercase)
        If Not (String.IsNullOrEmpty(sMsg)) Then
            Return sMsg
        End If

        sMsg = SingleValidator(revSpecialCharacter)
        If Not (String.IsNullOrEmpty(sMsg)) Then
            Return sMsg
        End If

        sMsg = SingleValidator(rfvConfirmPassword)
        If Not (String.IsNullOrEmpty(sMsg)) Then
            Return sMsg
        End If

        sMsg = SingleValidator(cfvPassword)
        If Not (String.IsNullOrEmpty(sMsg)) Then
            Return sMsg
        End If

        Return String.Empty
    End Function

    Private Function SingleValidator(oVal As IValidator) As String
        oVal.Validate()
        If (Not (oVal.IsValid())) Then
            Return oVal.ErrorMessage
        End If
        Return String.Empty
    End Function
#End Region

#End Region

End Class