Imports System.Security.Cryptography
Imports System.Data
Imports System.Text
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Threading
Imports System.Runtime.Remoting.Messaging
Imports System.ServiceModel
Imports System.ComponentModel

Public Class frmLogin

    Public Event LoggedIn(ByVal sender As Object, ByVal e As PresentationServiceReference.authenticateUserCompletedEventArgs)
    Public Event ChoseProfileButton(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Public Event ChoseCreateAccount(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Dim errors As Boolean = False
    Dim wcfService As New PresentationServiceReference.UserServicesClient

    
    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Dim errors As Boolean = False
        'hook up the callback sub
        AddHandler wcfService.authenticateUserCompleted, AddressOf authenticateUserCallback

        Try

            If PresentationValidation.validUserName(txtUsername.Text) = True Then
                ErrorProvider1.SetError(txtUsername, "")
            Else
                ErrorProvider1.SetError(txtUsername, "The User Name can not be blank and must between 1-50 characters")
                errors = True
            End If

            If PresentationValidation.validPassword(txtPassword.Text) = True Then
                ErrorProvider1.SetError(txtPassword, "")
            Else
                ErrorProvider1.SetError(txtPassword, "The Password can not be blank and must between 1-32 characters")
                errors = True
            End If

            If Not errors Then
                wcfService.authenticateUser(txtUsername.Text, txtPassword.Text)
            End If

        Catch ex As FaultException
            MessageBox.Show(ex.Message)
        Catch ex As CommunicationException
            MessageBox.Show(ex.Message)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub llCreateAccount_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llCreateAccount.LinkClicked
        RaiseEvent ChoseCreateAccount(sender, e)
    End Sub

    Private Sub llForgotPassword_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llForgotPassword.LinkClicked

    End Sub

    Private Sub btnChooseProfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChooseProfile.Click
        RaiseEvent ChoseProfileButton(sender, e)
    End Sub

    Sub authenticateUserCallback(ByVal sender As Object, ByVal e As PresentationServiceReference.authenticateUserCompletedEventArgs)
        Try

            If CBool(e.Result) = True Then
                'User account is already set by the wcf service
                'Set the isLoggedIn flag to true
                'Show Recipe Over
                modGlobals.IsLoggedIn = True
                RaiseEvent LoggedIn(sender, e)
                Me.Close()

            Else
                modGlobals.IsLoggedIn = False
                MessageBox.Show("The user name or password did not match any records.")

            End If

        Catch ex As FaultException
            MessageBox.Show(ex.Message)

        Catch ex As CommunicationException

            MessageBox.Show(ex.Message)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

End Class