Imports System.Text
Imports DataAccess
Imports System.ServiceModel

Public Class frmEditAccount

    Dim wcfService As New PresentationServiceReference.UserServicesClient
    Public Event UpdateStatus(ByVal statusString As String)

    Private Sub frmEditAccount_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim dateToTest As New Nullable(Of Date)
            dateToTest = Nothing
            Dim account As EntitiesLayer.RecipeHolderDatabaseDataSet.AccountsDataTable = wcfService.searchAccountProfile(18, "", "", "", dateToTest, True)

            txtFirstName.Text = account.Item(0).FirstName
            txtLastName.Text = account.Item(0).LastName
            'cbProtect.Checked = account.Item(0).isProtected
            txtUserName.Text = account.Item(0).UserName
            txtEMail.Text = account.Item(0).EMail
            cbProtect.Enabled = False
            'If cbProtect.Checked Then
            'lblEMail.Enabled = True
            'txtEMail.Enabled = True
            'txtPassword.Enabled = True
            'lblPassword.Enabled = True
            'txtUserName.Enabled = True
            'lblUserName.Enabled = True
            '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 btnSaveAccount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAccount.Click
        Try

            If wcfService.updateAccount(txtFirstName.Text, txtLastName.Text, txtUserName.Text, txtPassword.Text, cbProtect.Checked, txtEMail.Text, wcfService.getAccountId) = True Then
                RaiseEvent UpdateStatus("Account has been updated")
            Else
                RaiseEvent UpdateStatus("Account has failed to update")
            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 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
        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