Imports System.Windows.Forms

Public Class frmStartupOverview

    Private WithEvents loginForm As frmLogin
    Private WithEvents recipeOverviewForm As frmRecipeOverview
    Private WithEvents profileLogin As frmProfileOverview
    Private WithEvents newAccount As frmCreateAccount
    Private WithEvents editAccount As frmEditAccount
    Private WithEvents newRecipe As frmAddRecipe
    Private WithEvents editRecipe As frmEditRecipe
    Private WithEvents splash As RecipeSplashScreen
    Private WithEvents ingredientType As frmAddIngredientTypes
    Private WithEvents recipeViewer As frmRecipeViewer

    Dim recipeService As PresentationServiceReference.UserServicesClient

    Private Sub StartupOverview_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        modGlobals.IsLoggedIn = False
        mnuLogOff.Enabled = False
        mnuEditAccount.Enabled = False
        mnuEditRecipe.Enabled = False
        mnuNewRecipe.Enabled = False
        mnuEditRecipe.Enabled = False
        mnuSearchRecipe.Enabled = False
        mnuRecipeViewer.Enabled = False
        mnuNewIngredientTypes.Enabled = False
        modGlobals.CurrentAccountID = -1
        System.Net.ServicePointManager.DefaultConnectionLimit = 200
        mnuLogIn.Enabled = False

    End Sub


    Private Sub mnuNewAccount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNewAccount.Click
        ' Create a new instance of the child form.
        newAccount = New frmCreateAccount
        ' Make it a child of this MDI form before showing it.
        newAccount.MdiParent = Me
        AddHandler newAccount.btnCreateAccount.Click, AddressOf mnuSearchRecipe_Click
        AddHandler newAccount.UpdateStatus, AddressOf UpdateStatus
        newAccount.Show()
    End Sub

    Private Sub mnuEditAccount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditAccount.Click
        ' Create a new instance of the child form.
        editAccount = New frmEditAccount
        ' Make it a child of this MDI form before showing it.
        editAccount.MdiParent = Me
        AddHandler editAccount.UpdateStatus, AddressOf UpdateStatus
        editAccount.Show()
    End Sub

    Private Sub mnuProfileLogIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuProfileLogIn.Click
        'check to see if loginForm is still showing
        'If it is then close it
        If Not loginForm Is Nothing Then
            loginForm.Close()
        End If

        ' Create a new instance of the child form
        ' Make it a child of this MDI form before showing it.
        'Add handler to the search recipe menu click. Whenever someone chooses a profile the search recipe form shows
        profileLogin = New frmProfileOverview
        profileLogin.MdiParent = Me
        AddHandler profileLogin.LoggedIn, AddressOf mnuSearchRecipe_Click
        profileLogin.Show()
        
    End Sub

    Private Sub mnuLogIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuLogIn.Click
        'check to see if profile log in is still showing
        'If it is then close it
        If Not profileLogin Is Nothing Then
            profileLogin.Close()
        End If

        ' Create a new instance of the child form.
        ' Make it a child of this MDI form before showing it.
        'Add handler to the search recipe menu click. Whenever someone logs in the search recipe form shows
        loginForm = New frmLogin

        loginForm.MdiParent = Me

        AddHandler loginForm.btnOK.Click, AddressOf mnuSearchRecipe_Click
        AddHandler loginForm.ChoseProfileButton, AddressOf mnuProfileLogIn_Click
        AddHandler loginForm.ChoseCreateAccount, AddressOf mnuNewAccount_Click
        loginForm.Show()

    End Sub

    Private Sub mnuLogOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuLogOff.Click
        LogOff()
        modGlobals.CurrentAccountID = -1
        CloseMdiChidren()
        toolStripStatus.Text = "Logged Off"
    End Sub
    
    Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
        CloseMdiChidren()
        Me.Close()
    End Sub

    Private Sub mnuNewRecipe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNewRecipe.Click
        ' Create a new instance of the child form.
        newRecipe = New frmAddRecipe
        AddHandler newRecipe.UpdateStatus, AddressOf UpdateStatus
        ' Make it a child of this MDI form before showing it.
        newRecipe.MdiParent = Me
        newRecipe.Show()
    End Sub

    Private Sub mnuEditRecipe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditRecipe.Click
        ' Create a new instance of the child form.
        editRecipe = New frmEditRecipe
        ' Make it a child of this MDI form before showing it.
        editRecipe.MdiParent = Me
        editRecipe.Show()
    End Sub

    Private Sub mnuSearchRecipe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSearchRecipe.Click
        If modGlobals.IsLoggedIn = True Then
            IsLoggedIn()
            toolStripStatus.Text = "Currently logged In by username"
            recipeOverviewForm = New frmRecipeOverview
            recipeOverviewForm.MdiParent = Me
            recipeOverviewForm.Show()
        End If
        
    End Sub

    Private Sub mnuRecipeViewer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuRecipeViewer.Click
        ' Create a new instance of the child form.
        recipeViewer = New frmRecipeViewer
        ' Make it a child of this MDI form before showing it.
        recipeViewer.MdiParent = Me
        recipeViewer.Show()
    End Sub
   
    Private Sub mnuNewIngredientTypes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNewIngredientTypes.Click
        ' Create a new instance of the child form.
        ingredientType = New frmAddIngredientTypes
        ' Make it a child of this MDI form before showing it.
        ingredientType.MdiParent = Me
        ingredientType.Show()
    End Sub

    Private Sub mnuProgramInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuProgramInfo.Click
        ' Create a new instance of the child form.
        splash = New RecipeSplashScreen
        ' Make it a child of this MDI form before showing it.
        splash.MdiParent = Me
        splash.Show()
    End Sub

    Private Sub IsLoggedIn()
        modGlobals.IsLoggedIn = True
        mnuLogOff.Enabled = True
        mnuNewRecipe.Enabled = True
        'mnuEditRecipe.Enabled = True
        mnuSearchRecipe.Enabled = True
        'mnuRecipeViewer.Enabled = True
        mnuNewIngredientTypes.Enabled = True
        mnuEditAccount.Enabled = True
        mnuProfileLogIn.Enabled = False
        mnuLogIn.Enabled = False
    End Sub

    Private Sub LogOff()
        modGlobals.IsLoggedIn = False
        mnuLogIn.Enabled = False
        mnuLogOff.Enabled = False
        mnuEditRecipe.Enabled = False
        mnuNewRecipe.Enabled = False
        mnuEditRecipe.Enabled = False
        mnuSearchRecipe.Enabled = False
        mnuRecipeViewer.Enabled = False
        mnuNewIngredientTypes.Enabled = False
        mnuEditAccount.Enabled = False
        mnuProfileLogIn.Enabled = True
    End Sub

    Private Sub CloseMdiChidren()
        For Each formToClose As Form In Me.MdiChildren
            formToClose.Close()
        Next
    End Sub

    Public Sub UpdateStatus(ByVal statusString As String)
        toolStripStatus.Text = statusString
    End Sub
End Class
