Imports System.Windows.Forms
Imports DevExpress.Utils
Imports DevExpress.XtraBars.Docking
Imports DevExpress.XtraGrid.Views.BandedGrid
Imports DevExpress.XtraGrid.Views.Grid
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Config
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Language

Namespace DocumentResultList
    Public Class Layout
        Inherits BaseClass

        Private ReadOnly Config As ConfigManager(Of Config)
        Private ReadOnly GridViews As List(Of GridView)

        Public Sub New(pLogConfig As LogConfig, pConfig As ConfigManager(Of DocumentResultList.Config), pGridViews As List(Of GridView))
            MyBase.New(pLogConfig)
            Config = pConfig
            GridViews = pGridViews
        End Sub

#Region "Saving and Restoring layout"
        Public Sub Workspace_Save(pWorkspaceManager As WorkspaceManager)
            Try
                Dim oFileName = GetWorkspace_LayoutName()
                pWorkspaceManager.SaveWorkspaces(oFileName)
            Catch ex As Exception
                Logger.Error(ex)
                Logger.Info("Error while restoring Workspaces: " & ex.Message)
            End Try
        End Sub

        Public Async Function Workspace_SaveAsync(pWorkspaceManager As WorkspaceManager) As Task
            Await Task.Run(Sub() Workspace_Save(pWorkspaceManager))
        End Function

        Public Sub Workspace_Restore(pWorkspaceManager As WorkspaceManager)
            Try
                Dim oFileName = GetWorkspace_LayoutName()
                If IO.File.Exists(oFileName) Then
                    pWorkspaceManager.LoadWorkspaces(oFileName)
                End If
            Catch ex As Exception
                Logger.Error(ex)
                Logger.Info("Error while restoring Workspaces: " & ex.Message)
            End Try
        End Sub

        'Public Sub DockManager_SaveLayout(pDockManager As DockManager)
        '    Try
        '        Dim oFileName As String = GetDockmanager_LayoutName()
        '        pDockManager.SaveLayoutToXml(oFileName)
        '    Catch ex As Exception
        '        Logger.Error(ex)
        '        Logger.Info("Error while saving GridLayout: " & ex.Message)
        '    End Try
        'End Sub

        'Public Async Function DockManager_SaveLayoutAsync(pDockManager As DockManager) As Task
        '    Await Task.Run(Sub() DockManager_SaveLayout(pDockManager))
        'End Function

        'Public Sub DockManager_RestoreLayout(pDockManager As DockManager)
        '    Try
        '        Dim oFilename As String = GetDockmanager_LayoutName()
        '        If IO.File.Exists(oFilename) Then
        '            pDockManager.RestoreLayoutFromXml(oFilename)
        '        End If
        '    Catch ex As Exception
        '        Logger.Error(ex)
        '        Logger.Info("Error while restoring GridLayout: " & ex.Message)
        '    End Try
        'End Sub

        'Public Sub GridView_RestoreLayout(pGridView As GridView)
        '    Try
        '        Dim oLayoutFile As String = GetGrid_LayoutName(pGridView)
        '        If IO.File.Exists(oLayoutFile) Then
        '            pGridView.RestoreLayoutFromXml(oLayoutFile, OptionsLayoutBase.FullLayout)
        '        End If
        '    Catch ex As Exception
        '        Logger.Error(ex)
        '        Logger.Info("Error while restoring layout: " & ex.Message)
        '    End Try
        'End Sub

        'Public Sub GridView_SaveLayout(pGridView As GridView)
        '    Try
        '        Dim oFileName As String = GetGrid_LayoutName(pGridView)
        '        Using oStream = New IO.FileStream(oFileName, IO.FileMode.Open)
        '            pGridView.SaveLayoutToStream(oStream, OptionsLayoutBase.FullLayout)
        '        End Using
        '    Catch ex As Exception
        '        Logger.Error(ex)
        '        Logger.Info("Error while saving GridLayout: " & ex.Message)
        '    End Try
        'End Sub

        'Public Async Function GridView_SaveLayoutAsync(pGridView As GridView) As Task
        '    Await Task.Run(Sub() GridView_SaveLayout(pGridView))
        'End Function

        Public Function GetDockmanager_LayoutName() As String
            Dim Filename As String = $"DockManagerDocResult_UserLayout.xml"
            Dim oDirectory As String = IO.Path.GetDirectoryName(Config.UserConfigPath)
            Return IO.Path.Combine(oDirectory, Filename)
        End Function

        Public Function GetGrid_LayoutName(pGridView As GridView) As String
            Dim Filename As String = $"DevExpressGridViewDocResult_{pGridView.Name}UserLayout.xml"
            Dim oDirectory As String = IO.Path.GetDirectoryName(Config.UserConfigPath)
            Return IO.Path.Combine(oDirectory, Filename)
        End Function

        Public Function GetWorkspace_LayoutName() As String
            Dim Filename As String = $"Workspace_UserLayout.xml"
            Dim oDirectory As String = IO.Path.GetDirectoryName(Config.UserConfigPath)
            Return IO.Path.Combine(oDirectory, Filename)
        End Function

        Public Sub LoadWindowLocationAndSize(ByRef pForm As Form)
            If Utils.IsVisibleOnAnyScreen(Config.Config.WindowLocation) Then
                If Utils.LocationIsVisible(Config.Config.WindowLocation) Then
                    pForm.Location = Config.Config.WindowLocation
                End If
                If Utils.SizeIsVisible(Config.Config.WindowSize) Then
                    pForm.Size = Config.Config.WindowSize
                End If
            End If
        End Sub

        Public Sub SaveWindowLocationAndSize(ByRef pForm As Form)
            Config.Config.WindowLocation = pForm.Location
            Config.Config.WindowSize = pForm.Size
            Config.Save()
        End Sub
#End Region

#Region "Resetting Layout"
        Public Sub DockManager_ResetLayout(pDockManager As DockManager)
            Dim oFile = GetDockmanager_LayoutName()
            If IO.File.Exists(oFile) Then
                IO.File.Delete(oFile)
            End If
        End Sub
#End Region

        Public Sub SetGroupPanelVisible(pVisible As Boolean)
            For Each oView In GridViews
                oView.OptionsView.ShowGroupPanel = pVisible
            Next
        End Sub

        Public Function GetGroupPanelVisible() As Boolean
            Dim oView = GridViews.First()
            Return oView.OptionsView.ShowGroupPanel
        End Function

        Public Sub SetFilterRowVisible(pVisible As Boolean)
            For Each oView In GridViews
                oView.OptionsView.ShowAutoFilterRow = pVisible
            Next
        End Sub

        Public Function GetFilterRowVisible() As Boolean
            Dim oView = GridViews.First()
            Return oView.OptionsView.ShowAutoFilterRow
        End Function

        Public Sub SetBandTitleVisible(pVisible As Boolean)
            For Each oView In GridViews
                DirectCast(oView, BandedGridView).OptionsView.ShowBands = pVisible
            Next
        End Sub

        Public Function GetBandTitleVisible() As Boolean
            Dim oView = GridViews.First()
            Return DirectCast(oView, BandedGridView).OptionsView.ShowBands
        End Function

    End Class




End Namespace