Imports System.IO
Imports System.ComponentModel
Imports DevExpress.XtraBars.Docking2010
Imports DevExpress.LookAndFeel
Imports DevExpress.XtraBars.Ribbon
Imports DigitalData.GUIs.ClientSuite.ClassLayout
Imports DigitalData.GUIs.ClientSuite.Workers
Imports DigitalData.Modules.License

Public Class frmMain
    Private _PanelManager As PanelManager
    Private _WorkerManager As WorkerManager
    Private _Loading As Boolean = True

    Public Sub New()
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Show splashscreen
        frmSplash.ShowDialog()

        ' Initialize Form Related Variables
        My.MainForm = Me

        ' Initialize Common Functions
        My.Common = New ClassCommon(My.LogConfig)
    End Sub

    Private Sub SetOnlineLabel(Online As Boolean)
        If Online Then
            LabelServiceOnline.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
            LabelServiceOffline.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        Else
            LabelServiceOnline.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
            LabelServiceOffline.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
        End If
    End Sub

    Private Sub HandleOnlineChanged(sender As Object, Online As Boolean)
        SetOnlineLabel(Online)
        If Online = False Then
            MessageBox.Show($"Application will be closed now.{vbNewLine}Reason: Service is offline.", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Application.Exit()
        End If
    End Sub

    Private Sub FrmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            ' Initialize Worker Manager
            _WorkerManager = New WorkerManager(My.LogConfig, Me)
            Dim oHeartbeatWorker = _WorkerManager.GetWorker(Of HeartbeatWorker)()
            AddHandler oHeartbeatWorker.OnlineChanged, AddressOf HandleOnlineChanged

            ' Initialize Panel Manager
            _PanelManager = New PanelManager(My.LogConfig, DocumentManager, DockManager)

            ' Show Service Status Label 
            SetOnlineLabel(True)

            ' Apply Selected Skin Style
            UserLookAndFeel.Default.SetSkinStyle(My.UIConfig.SkinName)

            ' Show User Info
            LabelCurrentUser.Caption = My.Application.User.UserName
            LabelCurrentMachine.Caption = My.Application.User.MachineName
            LabelCurrentVersion.Caption = My.Application.Info.Version.ToString
            LabelCurrentLanguage.Caption = My.Application.User.Language

            ' Load and Save Layout
            AddHandler UserLookAndFeel.Default.StyleChanged, AddressOf frmMain_StyleChanged
            LoadLayout()

            ' We're done loading now
            _Loading = False
        Catch ex As Exception
            ShowErrorMessage(ex)
        End Try
    End Sub

    Private Sub FrmMain_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        SaveLayout()
    End Sub

    Private Sub frmMain_StyleChanged(sender As Object, e As EventArgs) Handles Me.StyleChanged
        If _Loading = False Then
            My.UIConfig.SkinName = LookAndFeel.ActiveSkinName
            My.UIConfigManager.Save()
        End If
    End Sub

    Private Sub LoadLayout()
        Try
            Dim oLayoutPathForDockManager As String = GetLayoutPath(GroupName.LayoutMain, LayoutComponent.DockManager)
            Dim oLayoutPathForDocumentManager As String = GetLayoutPath(GroupName.LayoutMain, LayoutComponent.DocumentManager)

            If File.Exists(oLayoutPathForDockManager) Then
                'DockManager.RestoreLayoutFromXml(oLayoutPathForDockManager)
            End If

            If File.Exists(oLayoutPathForDocumentManager) Then
                'DocumentManager.View.RestoreLayoutFromXml(oLayoutPathForDocumentManager)
            End If
        Catch ex As Exception
            ShowErrorMessage(ex)
        End Try
    End Sub
    Private Sub SaveLayout()
        Try
            Dim oLayoutPathForDockManager As String = GetLayoutPath(GroupName.LayoutMain, LayoutComponent.DockManager)
            Dim oLayoutPathForDocumentManager As String = GetLayoutPath(GroupName.LayoutMain, LayoutComponent.DocumentManager)
            Dim oDirectory As String = GetLayoutDirectory()

            If Not Directory.Exists(oDirectory) Then
                Directory.CreateDirectory(oDirectory)
            End If

            'DockManager.SaveLayoutToXml(oLayoutPathForDockManager)
            'DocumentManager.View.SaveLayoutToXml(oLayoutPathForDocumentManager)
        Catch ex As Exception
            ShowErrorMessage(ex)
        End Try
    End Sub

    Private Sub BarButtonUserSettings_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonUserSettings.ItemClick
        Dim frm As New frmConfigUser()
        frm.MdiParent = DocumentManager.MdiParent
        frm.Show()
    End Sub


    Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        Dim frm As New frmFileTest()
        frm.MdiParent = DocumentManager.MdiParent
        frm.Show()
    End Sub

    Private Sub BarButtonFormDesigner_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonFormDesigner.ItemClick
        Dim oForm As New frmFormDesigner()
        oForm.MdiParent = DocumentManager.MdiParent
        oForm.Show()
    End Sub

    Private Sub BarButtonConnectionSettings_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonConnectionSettings.ItemClick
        Dim frm As New frmConfigService()
        frm.MdiParent = DocumentManager.MdiParent
        frm.Show()
    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        Dim oUser1 = LicenseCreator.NewUser(UserType.PowerUser, 5)
        Dim oUser2 = LicenseCreator.NewUser(UserType.WriteOnly, 5, Date.Now)
        Dim oUsers As New List(Of LicenseModuleUser)
        oUsers.Add(oUser1)
        oUsers.Add(oUser2)

        Dim oModule = LicenseCreator.NewModule("EDMI", oUsers)
        Dim oModules As New List(Of LicenseModule)
        oModules.Add(oModule)

        Dim oLicense = LicenseCreator.NewLicense(oModules)

        Dim oLicenseFile As New LicenseFile(My.LogConfig, "E:\")
        oLicenseFile.SaveFile(oLicense)

        Dim oSerializer As New Xml.Serialization.XmlSerializer(GetType(LicenseSchema))
        Dim oLicense2 As LicenseSchema

        oLicense2 = oLicenseFile.LoadFile()
    End Sub

    Private Sub BarButtonUserManager_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonUserManager.ItemClick
        Dim oForm As New frmUserManager()
        oForm.MdiParent = DocumentManager.MdiParent
        oForm.Show()
    End Sub

    ' Manually merge the status bars of the parent and child MDI forms. 
    Private Sub RibbonControl1_Merge(ByVal sender As System.Object, ByVal e As RibbonMergeEventArgs) Handles RibbonControl.Merge
        Dim oParentRibbon As RibbonControl = TryCast(sender, RibbonControl)
        Dim oChildRibbon As RibbonControl = e.MergedChild
        oParentRibbon.StatusBar.MergeStatusBar(oChildRibbon.StatusBar)
    End Sub

    ' Manually unmerge the status bars. 
    Private Sub RibbonControl1_UnMerge(ByVal sender As System.Object, ByVal e As RibbonMergeEventArgs) Handles RibbonControl.UnMerge
        Dim oParentRibbon As RibbonControl = TryCast(sender, RibbonControl)
        oParentRibbon.StatusBar.UnMergeStatusBar()
    End Sub

    Private Sub BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick
        Dim frm As New frmDocTest With {
            .MdiParent = DocumentManager.MdiParent
        }
        frm.Show()
    End Sub

    Private Sub BarButtonInbox_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs)
        Dim oForm As New frmHome With {
            .MdiParent = DocumentManager.MdiParent
        }
        oForm.Show()
    End Sub

    Private Sub MainNav_SelectedItemChanging(sender As Object, e As DevExpress.XtraBars.Navigation.SelectedItemChangingEventArgs) Handles MainNav.SelectedItemChanging
        Select Case e.Item.Name
            Case NavbarItemHome.Name
                Dim oForm As New frmHome()
                oForm.MdiParent = DocumentManager.MdiParent
                oForm.Show()
            Case NavbarItemSearch.Name
                Dim oForm As New frmSearch()
                oForm.MdiParent = DocumentManager.MdiParent
                oForm.Show()
        End Select

        e.Cancel = True
    End Sub

    Private Sub BarButtonItem4_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem4.ItemClick
        Dim oForm As New frmWorkflowStep(172) With {
            .MdiParent = DocumentManager.MdiParent
        }
        oForm.Show()
    End Sub

    Private Sub BarButtonItem5_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem5.ItemClick
        Dim oForm As New frmWorkflowOverview() With {
            .MdiParent = DocumentManager.MdiParent
        }
        oForm.Show()
    End Sub
End Class