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 currencyColumns 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
                LOGGER.Warn("Unexpected Error in FormatDatatable: " & ex.Message)
                Return DT
            End Try
        End Function

        Public Sub FormatGridView(gridView As GridView)
            Try
                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

                currencyColumns = GetCurrencyColumns()

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

                    If Not IsNothing(column) Then
                        column.DisplayFormat.FormatType = FormatType.Numeric
                        column.DisplayFormat.FormatString = "C"
                        column.DisplayFormat.Format = New CurrencyFormatter()
                    End If
                Next
            Catch ex As Exception
                LOGGER.Warn("Unexpected Error in FormatGridView: " & ex.Message)
            End Try


        End Sub

        Class CurrencyFormatter
            Implements IFormatProvider, ICustomFormatter

            Public Function GetFormat(formatType As Type) As Object Implements IFormatProvider.GetFormat
                Return Me
            End Function

            Public Function Format(formatString As String, arg As Object, formatProvider As IFormatProvider) As String Implements ICustomFormatter.Format
                Dim formatValue As String = arg.ToString()

                If (formatValue.Count > 0 And formatString.ToUpper() = "C") Then
                    Return ClassHelper.Format_Currency(formatValue, USER_LANGUAGE)
                Else
                    Return formatValue
                End If
            End Function
        End Class

        Private Function GetCurrencyColumns() As List(Of String)
            Try
                Dim list As New List(Of String)
                ' Alle Currency Felder für aktuelle Ansicht heraussuchen
                Dim sql As String = "SELECT COL_NAME FROM TBPMO_CONTROL WHERE CONTROL_TYPE_ID = 2 AND FORMAT_TYPE = 'Currency' AND SHOW_COLUMN = 1 AND FORM_ID = " & Me.EntityId
                Dim dt As DataTable = ClassHelper.FILTER_DATATABLE(CURRENT_TBPMO_CONTROL, "CONTROL_TYPE_ID = 2 AND FORMAT_TYPE = 'Currency' AND SHOW_COLUMN = 1 AND FORM_ID = " & Me.EntityId, "")

                For Each row As DataRow In dt.Rows
                    list.Add(row.Item("COL_NAME"))
                Next

                Return list
            Catch ex As Exception
                LOGGER.Warn("Unexpected Error in GetCurrencyColumns: " & ex.Message)
                Return Nothing
            End Try
        End Function

        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 = ClassHelper.FILTER_DATATABLE(CURRENT_TBPMO_CONTROL, "CONTROL_TYPE_ID in (10,11) AND SHOW_COLUMN = 1 AND FORM_ID = " & Me.EntityId, "")
                '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("COL_NAME"))
                    Next
                End If
                listcheck.Add("files?")
                Return listcheck
            Catch ex As Exception
                LOGGER.Warn("Unexpected Error in GetCheckboxColumns: " & ex.Message)
                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 = ClassHelper.FILTER_DATATABLE(CURRENT_TBPMO_CONTROL, "CONTROL_TYPE_ID = 4 AND SHOW_COLUMN = 1 AND FORM_ID = " & Me.EntityId, "")

                '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("COL_NAME"))
                    Next
                End If
                Return listdate
            Catch ex As Exception
                LOGGER.Warn("Unexpected Error in GetCheckboxColumns: " & ex.Message)
                Return Nothing
            End Try
        End Function
    End Class

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

    End Class

End Class
