Imports System.Data.SqlClient
Imports System.Security.Cryptography
Imports System.Text
Imports System.ServiceModel

Public Class frmCreateAccount

    Dim errors As Boolean = False
    Dim recipeService As New PresentationServiceReference.UserServicesClient
    Public Event UpdateStatus(ByVal statusString As String)


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

    End Sub

    Private Sub cbProtect_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbProtect.CheckedChanged
        If cbProtect.Checked = True Then
            txtUserName.Enabled = True
            txtPassword.Enabled = True
            lblUserName.Enabled = True
            lblPassword.Enabled = True
            lblEMail.Enabled = True
            txtEMail.Enabled = True

        Else
            txtUserName.Enabled = False
            txtPassword.Enabled = False
            lblUserName.Enabled = False
            lblPassword.Enabled = False
            lblEMail.Enabled = False
            txtEMail.Enabled = False
            ErrorProvider1.SetError(txtUserName, "")
            ErrorProvider1.SetError(txtPassword, "")
        End If


    End Sub

    ''' <summary>
    ''' When user clicks the create account button.....
    ''' Checks to see if there are any errors from the validation events. 
    ''' The event then proceeds to insert new account data into database table Accounts.
    ''' </summary>
    ''' <param name="sender">provides information about the event</param>
    ''' <param name="e">contains information you need to process the event</param>
    ''' <remarks></remarks>
    Private Sub btnCreateAccount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateAccount.Click
        'If there are no errors
        'Save the account information and get the new account id
        'Else show error
        Try


            If errors = False Then
                Dim accountId As Integer
                accountId = recipeService.saveAccount(txtFirstName.Text, txtLastName.Text, txtUserName.Text, txtPassword.Text, txtEMail.Text, cbProtect.Checked)
                'If the account was saved and returned rows
                '
                If accountId >= 0 Then
                    modGlobals.IsLoggedIn = True
                    modGlobals.CurrentAccountID = accountId
                    recipeService.setAccountID(accountId)

                    RaiseEvent UpdateStatus("Account Created")
                    MessageBox.Show("Account Created.")
                    Me.Close()
                Else
                    RaiseEvent UpdateStatus("Account Error")
                    MessageBox.Show("Account was not saved, there are errors", "Error in saving account", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Else
                RaiseEvent UpdateStatus("Account Error")
                MessageBox.Show("Account was not saved, there are errors", "Error in saving account", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

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

    Private Sub txtFirstName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtFirstName.Validating

        If txtFirstName.Text = "" OrElse txtFirstName.Text.Length > 50 Then
            ErrorProvider1.SetError(txtFirstName, "First name field is required and must be less than 50 characters.")
            errors = True
        Else
            ErrorProvider1.SetError(txtFirstName, "")
            errors = False
        End If

    End Sub

    Private Sub txtLastName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtLastName.Validating

        If txtLastName.Text = "" OrElse txtLastName.Text.Length > 50 Then
            ErrorProvider1.SetError(txtLastName, "Last name field is required and must be less than 50 characters.")
            errors = True
        Else
            ErrorProvider1.SetError(txtLastName, "")
            errors = False
        End If

    End Sub

    Private Sub txtUserName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtUserName.Validating

        If cbProtect.Checked = True Then
            If txtUserName.Text = "" OrElse txtUserName.Text.Length > 50 Then
                ErrorProvider1.SetError(txtUserName, "User name field is required when Protect Account checkbox is checked and must be less than 50 characters.")
                errors = True
            Else
                ErrorProvider1.SetError(txtUserName, "")
                errors = False
            End If
        End If

    End Sub

    Private Sub txtPassword_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtPassword.Validating

        If cbProtect.Checked = True Then
            If txtPassword.Text = "" Or txtPassword.Text.Length > 32 Then
                ErrorProvider1.SetError(txtPassword, "Password field is required when Protect Account checkbox is checked and must be less than 32 characters.")
                errors = True
            Else
                ErrorProvider1.SetError(txtPassword, "")
                errors = False
            End If
        End If

    End Sub

    Private Sub btnExitForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExitForm.Click
        Me.Close()
    End Sub
End Class