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_RetrieveUsernameStep2
	Inherits System.Web.UI.UserControl
	Implements IMyAccountStepPage

	Private m_sError As String
	Dim m_myQueryString As String
	Dim 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
		If Not IsPostBack And ((Not Request.QueryString(QueryStringName.MyAccountStep) Is Nothing) AndAlso Request.QueryString(QueryStringName.MyAccountStep) = Me.QueryKey) Then
			LoadAccount()
            GetSecretQuestion()
		End If
	End Sub

#End Region

#Region " Subs and Functions "

	Private Sub GetSecretQuestion()
		'Get the Secret Question and answer that user has entered in the past
		'from the OSSAPI call getLogin webservice
		'check if the secret question is part of the current list of secret questions
		'if so, select and show that question in the dropdown list of Secret questions
		Try
			If AccountManager.Instance.GetLoginUserName() Then
				If Not (m_MyAccount.SecretQuestion Is Nothing) Then
					lblSecretQuestion.Text = m_MyAccount.SecretQuestion & ""
				End If
			End If
		Catch ex As Exception
			m_sError = ex.Message
		End Try
	End Sub

	Public Function DoWork() As Boolean Implements IMyAccountStepPage.DoWork
		Dim _return As Boolean

		If txtSecretAnswer.Text > "" Then

			Try

				'the account object should now be populated with the username, from LoadSecretQuestions method, lets validate their answer
				'we have to reload the account object because it was modified in the above routine
				If LoadAccount() Then
					If AccountManager.Instance.AuthenticateLoginSecretQA(m_MyAccount.UserName, lblSecretQuestion.Text, txtSecretAnswer.Text) Then
						_return = True
					Else
						'TODO: Fail-out logic.
						m_sError = AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage
						AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()

						'return False
						'authenticate login secret QA already sets the session variable MYACCOUNT_SECRETQA_FAILED_TotalAttemptCount
						'increases the count  of the variable by one.
						'main page should check for number fail out attempts based on that count against the maximum allowd counts
						'and then 
						'stop the user from moving forward to next step if number of attempts
						'exceeded allowed number
						_return = False
					End If

				End If

			Catch ex As Exception
                m_sError = ex.Message
				_return = False
			End Try
		Else
			'the page validators failed.
			m_sError = "Please supply a valid answer."
			_return = False
		End If

		Return _return
	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

#End Region

End Class