Module ModuleHelperMethods

    Public Enum EnumFormatOptions
        [String] = 0
        Currency = 1
        [Decimal] = 2
    End Enum

    Public Enum EnumDateTimePickerDefaultValueOptions
        CurrentDate = 0
        Empty = 1
    End Enum

    Public Function NotNull(Of T)(ByVal value As T, ByVal defaultValue As T) As T
        If IsNothing(value) OrElse String.IsNullOrEmpty(value.ToString) OrElse IsDBNull(value) Then
            Return defaultValue
        Else
            Return value
        End If
    End Function

    Public Function NotNullInt(ByVal value As Integer, ByVal defaultValue As Integer) As Integer
        If IsNothing(value) OrElse IsDBNull(value) OrElse value = 0 Then
            Return defaultValue
        Else
            Return value
        End If
    End Function

    Public Function NotNull(ByVal value As String, ByVal defaultValue As String) As String
        If IsNothing(value) OrElse IsDBNull(value) OrElse String.IsNullOrEmpty(value) Then
            Return defaultValue
        Else
            Return value
        End If
    End Function


    Public Function BoolToInt(bool As Boolean) As Integer
        ' Wandelt einen Boolean Wert in einen Int um
        Return IIf(bool, 1, 0)
    End Function

    Public Function IntToBool(int As Integer) As Boolean
        If int = 0 Then
            Return False
        ElseIf int = 1 Then
            Return True
        Else
            Throw New Exception("IntToBool expects an Integer value that is either 0 or 1")
        End If
    End Function

    Public Function ColorToInt(color As Color) As Integer
        Return System.Drawing.ColorTranslator.ToWin32(color)
    End Function

    Public Function IntToColor(int As Integer) As Color
        Return System.Drawing.ColorTranslator.FromWin32(int)
    End Function

    Public Function StrToBool(str As Object) As Boolean
        Dim result As Boolean = False

        str = TryCast(str, String)

        Try
            result = Convert.ToBoolean(str)
        Catch ex As Exception
            result = False
        End Try

        Return result
    End Function

    Public Function ByteArrayToBitmap(bytearray() As Byte) As Bitmap
        Return New Bitmap(New System.IO.MemoryStream(bytearray))
    End Function

    Public Function StringToByteArray(ByVal hex As String) As Byte()
        Dim NumberChars As Integer = hex.Length

        Dim bytes(NumberChars / 2) As Byte

        For i As Integer = 0 To NumberChars - 1 Step 2
            bytes(i / 2) = Convert.ToByte(hex.Substring(i, 2), 16)
        Next

        Return bytes
    End Function

    Public Function BitmapToByteArray(bitmap As Bitmap) As Byte()
        Dim bytearray As Byte()

        Using stream As New System.IO.MemoryStream
            bitmap.Save(stream, bitmap.RawFormat)
            bytearray = stream.ToArray()
        End Using

        Return bytearray
    End Function

    Public Function IsGroupBox(c As Control)
        Return c.GetType().Name = "GroupBox"
    End Function

    Public Function ParentIsGroupBox(c As Control)
        Return c.Parent.GetType().Name = "GroupBox"
    End Function

    ''' <summary>
    ''' Checks if a property exists on an object
    ''' </summary>
    ''' <param name="obj">The Object to check</param>
    ''' <param name="prop">The Property to check for</param>
    ''' <returns>True, if prop exists</returns>
    Public Function propExists(ByVal obj As Object, ByVal prop As String) As Boolean
        Dim type As Type = obj.GetType
        Return type.GetProperty(prop) IsNot Nothing
    End Function

    Public Function propExistsWithType(ByVal obj As Object, ByVal prop As String, returnType As System.Type)
        Dim type As Type = obj.GetType
        Return type.GetProperty(prop, returnType) IsNot Nothing
    End Function

    ''' <summary>
    ''' Gets a ControlID from Control Name
    ''' </summary>
    ''' <param name="name">Control Name</param>
    ''' <param name="formid">Current Form ID</param>
    ''' <returns>Control ID</returns>
    Public Function GetControlID_for_Name(name As String, formid As Integer) As Integer
        Try
            Dim SQL = "SELECT GUID FROM TBPMO_CONTROL WHERE FORM_ID = " & formid & " and NAME = '" & name & "'"
            Dim ID As Integer = ClassDatabase.Execute_Scalar(SQL)
            If ID > 0 Then
                Return ID
            Else
                Return -1
            End If
        Catch ex As Exception
            MsgBox("Error in GetControlID_for_Name:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Function

    Public Function GetControlID_for_RecordID(name As String, recID As Integer) As Integer
        Try
            Dim SQL = "SELECT CONTROL_ID FROM VWPMO_VALUES WHERE RECORD_ID = " & recID & " and CONTROL_NAME = '" & name & "'"
            Dim ID As Integer = ClassDatabase.Execute_Scalar(SQL)
            If ID > 0 Then
                Return ID
            Else
                If LogErrorsOnly = False Then ClassLogger.Add(">> es konnte keine ID für name geholt werden: " & SQL, False)
                Return -1
            End If
        Catch ex As Exception
            MsgBox("Error in GetControlID:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Function

    ''' <summary>
    ''' Gets a Control Name from ControlID
    ''' </summary>
    ''' <param name="Id">ControlID</param>
    ''' <param name="formid">Current Form ID</param>
    ''' <returns>Control Name</returns>
    Public Function Get_Name_for_ControlID(Id As Integer, formid As Integer) As String
        Try
            Dim SQL = "SELECT NAME FROM TBPMO_CONTROL WHERE FORM_ID = " & formid & " AND GUID = " & Id
            Dim Name = ClassDatabase.Execute_Scalar(SQL)

            Return Name
        Catch ex As Exception
            MsgBox("Error in GetName_for_ControlID:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function

    Public Sub OpenFormInputFor(FormID As Integer, ScreenID As Integer)
        CURRENT_FORM_ID = FormID
        CURRENT_SCREEN_ID = ScreenID

        Dim frm As New frmFormInput
        frm.MdiParent = MAIN_FORM

        frm.Show()
        frm.BringToFront()
    End Sub

    Public Sub OpenFormCalendar()
        Dim frm As New frmCalendar
        frm = frmCalendar.Instance()
        If My.Settings.User_Calendar_isChild = True Then
            frm.MdiParent = MAIN_FORM
        End If


        frm.Show()
    End Sub
    Public Sub Close_Maximized_Forms()
        For i = System.Windows.Forms.Application.OpenForms.Count - 1 To 1 Step -1
            Dim form As Form = System.Windows.Forms.Application.OpenForms(i)
            form.WindowState = FormWindowState.Normal
        Next i
    End Sub
    Public Sub OpenFormOverview()
        Close_Maximized_Forms()
        Dim frm As New frmEntities
        frm = frmEntities.Instance()
        frm.MdiParent = MAIN_FORM

        frm.Show()
    End Sub
    Public Sub OpenFormLevelDesigner()
        For i = System.Windows.Forms.Application.OpenForms.Count - 1 To 1 Step -1
            Dim form As Form = System.Windows.Forms.Application.OpenForms(i)
            form.WindowState = FormWindowState.Normal
        Next i

        Dim frm As New frmLevel_Designer
        frm = frmLevel_Designer.Instance
        frm.MdiParent = MAIN_FORM
        frm.Show()


        Dim frm2 As New frmTool_ControlDesigner
        frm2 = frmTool_ControlDesigner.Instance
        If My.Settings.User_DesignPanels_areChild = True Then
            frm2.MdiParent = MAIN_FORM
        End If

        frm2.Show()

        Dim frm3 As New frmTool_ControlProperties
        frm3 = frmTool_ControlProperties.Instance
        If My.Settings.User_DesignPanels_areChild = True Then
            frm3.MdiParent = MAIN_FORM
        End If

        frm3.Show()

    End Sub

    Public Sub OpenFormConstructor(id As Integer, Optional recordId As Integer = -1)
        Try
            CURRENT_CONSTRUCTOR_ID = id
            Dim frm As New frmConstructor_Main()
            ' frm = frmForm_Constructor.Instance()

            Dim activeChild As Form = MAIN_FORM.ActiveMdiChild
            If activeChild IsNot Nothing Then
                activeChild.WindowState = FormWindowState.Normal
            End If

            If recordId <> -1 Then
                ' Wenn JUMP_RECORD_ID gesetzt wurde, wird zu diesem Record gesprungen
                JUMP_RECORD_ID = recordId
            End If

            frm.MdiParent = MAIN_FORM
            frm.Show()
        Catch ex As Exception
            ClassLogger.Add(ex)
            MsgBox("Fehler beim Laden des Formulars, bitte erneut versuchen")
        End Try
    End Sub

    Public Sub OpenTaskmanagement()
        Dim frm As New frmTask_Management
        frm = frmTask_Management.Instance()
        frm.MdiParent = MAIN_FORM

        Dim activeChild As Form = MAIN_FORM.ActiveMdiChild
        If activeChild IsNot Nothing Then
            activeChild.WindowState = FormWindowState.Normal
        End If

        frm.Show()
    End Sub
    Public Sub OpenDokumentartt()
        Dim frm As New frmDokumentart_Konfig
        frm = frmDokumentart_Konfig.Instance()
        frm.MdiParent = MAIN_FORM

        Dim activeChild As Form = MAIN_FORM.ActiveMdiChild
        If activeChild IsNot Nothing Then
            activeChild.WindowState = FormWindowState.Normal
        End If

        frm.Show()
    End Sub
    Public Sub OpenMenuDesigner()
        Dim frm As New frmMenuDesigner
        frm.MdiParent = MAIN_FORM

        Dim activeChild As Form = MAIN_FORM.ActiveMdiChild
        If activeChild IsNot Nothing Then
            activeChild.WindowState = FormWindowState.Normal
        End If

        frm.Show()
    End Sub
    Public Sub OpenUserKonfig()
        Dim frm As New frmUserKonfig
        frm = frmUserKonfig.Instance()
        frm.MdiParent = MAIN_FORM

        Dim activeChild As Form = MAIN_FORM.ActiveMdiChild
        If activeChild IsNot Nothing Then
            activeChild.WindowState = FormWindowState.Normal
        End If

        frm.Show()
    End Sub

    Public Sub OpenImageManager()
        Dim frm As New frmQuickAccessManager
        'frm = frmImageManager.Instance()
        frm.MdiParent = MAIN_FORM

        Dim activeChild As Form = MAIN_FORM.ActiveMdiChild
        If activeChild IsNot Nothing Then
            activeChild.WindowState = FormWindowState.Normal
        End If

        frm.Show()
    End Sub
    Public Sub OpenObjecttypeConfig()
        Dim frm As New frmObjecttypeConfig
        frm = frmObjecttypeConfig.Instance()
        frm.MdiParent = MAIN_FORM

        Dim activeChild As Form = MAIN_FORM.ActiveMdiChild
        If activeChild IsNot Nothing Then
            activeChild.WindowState = FormWindowState.Normal
        End If

        frm.Show()
    End Sub
    Public Sub OpenWiedervorlage()
        Dim frm As New frmWiedervorlage
        frm.MdiParent = MAIN_FORM

        Dim activeChild As Form = MAIN_FORM.ActiveMdiChild
        If activeChild IsNot Nothing Then
            activeChild.WindowState = FormWindowState.Normal
        End If

        frm.Show()
    End Sub
    Public Sub OpenLogRecord()
        Dim frm As New frmLogRecord
        frm = frmLogRecord.Instance()
        frm.MdiParent = MAIN_FORM

        Dim activeChild As Form = MAIN_FORM.ActiveMdiChild
        If activeChild IsNot Nothing Then
            activeChild.WindowState = FormWindowState.Normal
        End If

        frm.Show()
    End Sub
    Public Sub OpenTemplatemanagement()
        Dim frm As New frmTemplates
        frm.MdiParent = MAIN_FORM

        Dim activeChild As Form = MAIN_FORM.ActiveMdiChild
        If activeChild IsNot Nothing Then
            activeChild.WindowState = FormWindowState.Normal
        End If

        frm.Show()
    End Sub
    Public Sub OpenFormCockpit()
        Close_Maximized_Forms()
        Dim frm As New frmCockpit
        frm = frmCockpit.Instance()
        frm.MdiParent = MAIN_FORM
        frm.Show()
    End Sub

    Public Function ShortGUID() As String
        Return Guid.NewGuid().ToString().GetHashCode().ToString("x")
    End Function

End Module
