Imports DevExpress.Utils

Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraGrid.Views.Grid

Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Repository


Public Class ClassGridFormatter

    Private Shared tDate As Type = GetType(Date)
    Private Shared tBool As Type = GetType(Boolean)



    Public Enum DataTableType
        CONSTRUCTOR_VIEW
        WINDREAM_RESULT
    End Enum

    ''' <summary>
    ''' Die Formatierungsfunktionen für die ConstruktorView Tabellen
    ''' </summary>
    Public Class ConstructorView
        Private EntityId As Integer
        Private DT As DataTable

        Private dateEdit As New RepositoryItemTimeEdit()
        Private checkboxEdit = New RepositoryItemCheckEdit()

        Public checkboxColumns As New List(Of String)
        Public dateColumns As New List(Of String)

        Public Sub New(DT As DataTable, EntityId As Integer)
            Me.EntityId = EntityId
            Me.DT = DT

            ' dateEdit vor-formatieren
            dateEdit.DisplayFormat.FormatType = FormatType.DateTime
            dateEdit.DisplayFormat.FormatString = CURRENT_DATE_FORMAT
            dateEdit.Mask.MaskType = Mask.MaskType.DateTime
            dateEdit.Mask.EditMask = CURRENT_DATE_FORMAT
            dateEdit.Mask.UseMaskAsDisplayFormat = True

            ' checkboxEdit vor-formatieren
            checkboxEdit.ValueChecked = True
            checkboxEdit.ValueUnchecked = False
        End Sub

        Public Function FormatDatatable() As DataTable
            Try
                ' Datatable klonen, um Spalten zu formatieren
                Dim clonedDT As DataTable = Me.DT.Clone()

                ' Checkbox Spalten bestimmen
                Me.checkboxColumns = GetCheckboxColumns()

                ' Checkbox Spalten formatieren
                For Each columnName As String In Me.checkboxColumns
                    Dim column As DataColumn = clonedDT.Columns(columnName)

                    If Not IsNothing(column) Then
                        column.DataType = tBool
                    End If
                Next

                ' Datums Spalten bestimmen
                Me.dateColumns = GetDateColumns()

                ' Datums Spalten Formatieren
                For Each columnName As String In Me.dateColumns
                    Dim column As DataColumn = clonedDT.Columns(columnName)

                    If Not IsNothing(column) Then
                        column.DataType = tDate
                    End If
                Next

                ' Daten aus DT in formatierte DataTable clonedDT laden
                clonedDT.Load(DT.CreateDataReader)
                Return clonedDT
            Catch ex As Exception
                ClassLogger.Add("Unexpected Error in FormatDatatable: " & ex.Message, True)
                Return DT
            End Try
        End Function

        Public Sub FormatGridView(gridView As GridView)
            If IsNothing(gridView.GridControl.DataSource) Then
                Throw New Exception("Error in FormatGridView: DataSource is Empty!")
            End If

            ' Editoren zum Grid hinzufügen
            gridView.GridControl.RepositoryItems.AddRange({checkboxEdit, dateEdit})

            For Each columnName As String In Me.dateColumns
                Dim column As GridColumn = gridView.Columns(columnName)

                If Not IsNothing(column) Then
                    column.DisplayFormat.FormatType = FormatType.DateTime
                    column.DisplayFormat.FormatString = CURRENT_DATE_FORMAT
                    column.OptionsFilter.FilterPopupMode = FilterPopupMode.Date
                    column.FilterMode = ColumnFilterMode.Value ' was DisplayText
                    column.ColumnEdit = dateEdit
                End If
            Next

            For Each columnName As String In Me.checkboxColumns
                Dim column As GridColumn = gridView.Columns(columnName)

                If Not IsNothing(column) Then
                    column.ColumnEdit = checkboxEdit
                End If
            Next

        End Sub

        Private Function GetCheckboxColumns()
            Try
                Dim listcheck As New List(Of String)
                ' Alle Checkboxen für aktuelle Ansicht heraussuchen
                Dim sqlcheck As String = "SELECT COL_NAME FROM TBPMO_CONTROL WHERE CONTROL_TYPE_ID in (10,11) AND SHOW_COLUMN = 1 AND FORM_ID = " & Me.EntityId
                Dim dtcheck As DataTable = ClassDatabase.Return_Datatable(sqlcheck, True)
                'Liste von allen Spaltentiteln mit Checkbox erstellen
                If dtcheck.Rows.Count > 0 Then
                    For Each row As DataRow In dtcheck.Rows
                        listcheck.Add(row.Item(0))
                    Next
                End If
                listcheck.Add("files?")
                Return listcheck
            Catch ex As Exception
                ClassLogger.Add("Unexpected Error in GetCheckboxColumns: " & ex.Message, True)
                Return Nothing
            End Try
        End Function
        Private Function GetDateColumns()
            Try
                Dim listdate As New List(Of String)
                ' Alle Checkboxen für aktuelle Ansicht heraussuchen
                Dim sqlcheck As String = "SELECT COL_NAME FROM TBPMO_CONTROL WHERE CONTROL_TYPE_ID = 4 AND SHOW_COLUMN = 1 AND FORM_ID = " & Me.EntityId
                Dim dtdate As DataTable = ClassDatabase.Return_Datatable(sqlcheck, True)
                'Liste von allen Spaltentiteln mit Checkbox erstellen
                If dtdate.Rows.Count > 0 Then
                    For Each row As DataRow In dtdate.Rows
                        listdate.Add(row.Item(0))
                    Next
                End If
                Return listdate
            Catch ex As Exception
                ClassLogger.Add("Unexpected Error in GetCheckboxColumns: " & ex.Message, True)
                Return Nothing
            End Try
        End Function
    End Class

    ''' <summary>
    ''' Die Formatierungsfunktionen für die WindreamResult Tabellen
    ''' </summary>
    Public Class WindreamResult

    End Class

End Class

' FOR REFERENCE IN CASE SOMETHING BREAKS :)
'Public Shared Function Format_GridColumns(Primary_DT As DataTable, _ENTITYSQL As String, listcheck As List(Of String), listdate As List(Of String))
'    Try
'        Dim tbltemp As DataTable = Primary_DT.Clone()
'        ' Nicht benötigt? Datumsspalten werden im Grid formatiert
'        For Each col As String In listdate
'            Dim colDate As DataColumn = tbltemp.Columns(col)
'            If Not IsNothing(colDate) Then
'                Try
'                    colDate.DataType = GetType(Date)
'                Catch ex As Exception
'                    MsgBox("Error in Format_GridColumns:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
'                End Try
'            End If
'        Next
'        For Each col1 As String In listcheck
'            Dim collist As DataColumn = tbltemp.Columns(col1)
'            If Not IsNothing(collist) Then
'                collist.DataType = GetType(Boolean)
'            End If
'        Next
'        Try
'            Dim collist As DataColumn = tbltemp.Columns("files?")
'            If Not IsNothing(collist) Then
'                collist.DataType = GetType(Boolean)
'            End If
'        Catch ex As Exception

'        End Try
'        Try
'            tbltemp.Load(Primary_DT.CreateDataReader)
'            Return tbltemp
'        Catch ex As Exception
'            ClassLogger.Add(">> Attention: Format_GridColumns - Could not load converted datatable: " & ex.Message, False)
'            'Tabelle wird ohne Datekonvertierung geladen
'            Dim DTEntity As DataTable = ClassDatabase.Return_Datatable(_ENTITYSQL, "Load_Entity_Data_Only")
'            Dim primaryKey(1) As DataColumn
'            primaryKey(0) = DTEntity.Columns("Record-ID")
'            DTEntity.PrimaryKey = primaryKey
'            Return DTEntity
'        End Try
'    Catch ex As Exception
'        MsgBox("Unexpected Error in Format_GridColumns:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
'        ClassLogger.Add("Error in Format_GridColumns: " & ex.Message, True)
'        Return Nothing
'    End Try

'End Function
