Public Class GUI_Gratings_Data
    Public Shared gratingMaxW As Integer = 1000
    Public Shared gratingMaxL As Integer = 1000

    Private Shared gratingType, gratingMaterial, gratingMesh, gratingName As String
    Private Shared gratingSerrated As Boolean = False
    Private Shared loadBarSpacing, crossBarSpacing, gratingHeight, loadBarThickness As Integer

    Private Shared serratedCheckBox As New CheckBox
    Private Shared serratedCheckBoxBol As Boolean = False


    Private Shared lacqueredCheckBox As New CheckBox
    Private Shared lacqueredCheckBoxBol As Boolean = False
    Private Shared gratingLacquered As Boolean = False


    Public Shared Sub TypeChanged(sender As Object, e As EventArgs)
        GUI.ComboBox_Material.Enabled = True
        GUI.ComboBox_Material.Items.Clear()
        gratingType = GUI.ComboBox_TypeChooser.Text

        Dim materialDT As DataTable = GUI.dataBase("Material")

        Dim typeInDT As String = materialDT.Rows(0)("TYPE")
        For i = 0 To materialDT.Rows.Count - 1
            If gratingType = materialDT.Rows(i)("TYPE") Then
                GUI.ComboBox_Material.Items.Add(materialDT.Rows(i)("MATERIAL"))
            End If
            Try
                typeInDT = materialDT.Rows(i + 1)("TYPE")
            Catch ex As Exception
            End Try
        Next

        GUI.ComboBox_Material.Text = GUI.ComboBox_Material.Items(0)
    End Sub

    Public Shared Sub MaterialChanged(sender As Object, e As EventArgs)
        GUI.ComboBox_MeshSize.Enabled = True
        gratingMaterial = GUI.ComboBox_Material.Text

        Dim serratedDT As DataTable = GUI.dataBase("Serrated")

        Dim removeCheckBox As Boolean = True
        Dim typeInDT As String = serratedDT.Rows(0)("TYPE")
        Dim materialInDT As String = serratedDT.Rows(0)("MATERIAL")

        If serratedCheckBoxBol = False Then
            For i = 0 To serratedDT.Rows.Count - 1
                If gratingMaterial = materialInDT AndAlso gratingType = typeInDT Then
                    serratedCheckBox.Name = "CheckBox_Serrated"
                    serratedCheckBox.Text = "Serrated"
                    serratedCheckBox.Left = 115
                    serratedCheckBox.Top = 115
                    AddHandler serratedCheckBox.CheckedChanged, AddressOf CheckBox_Serrated_CheckedChanged
                    GUI.Controls.Add(serratedCheckBox)
                    serratedCheckBoxBol = True
                    Exit For
                End If
                Try
                    typeInDT = serratedDT.Rows(i + 1)("TYPE")
                    materialInDT = serratedDT.Rows(i + 1)("MATERIAL")
                Catch ex As Exception
                End Try
            Next
        Else
            For i = 0 To serratedDT.Rows.Count - 1
                If gratingMaterial = materialInDT AndAlso gratingType = typeInDT Then
                    removeCheckBox = False
                    Exit For
                End If
                Try
                    typeInDT = serratedDT.Rows(i + 1)("TYPE")
                    materialInDT = serratedDT.Rows(i + 1)("MATERIAL")
                Catch ex As Exception
                End Try
            Next
            If serratedCheckBoxBol = True AndAlso removeCheckBox = True Then
                RemoveHandler serratedCheckBox.CheckedChanged, AddressOf CheckBox_Serrated_CheckedChanged
                GUI.Controls.RemoveByKey("CheckBox_Serrated")
                serratedCheckBoxBol = False
                gratingSerrated = False
            End If
        End If


        If lacqueredCheckBoxBol = False AndAlso (gratingMaterial = "Hot dip galvanized steel" _
            OrElse gratingMaterial = "Untreated") Then

            lacqueredCheckBox.Name = "CheckBox_Lacquered"
            lacqueredCheckBox.Text = "Lacquered"
            lacqueredCheckBox.Left = 115
            lacqueredCheckBox.Top = 135
            AddHandler lacqueredCheckBox.CheckedChanged, AddressOf CheckBox_Lacquered_CheckedChanged
            GUI.Controls.Add(lacqueredCheckBox)
            lacqueredCheckBoxBol = True
        ElseIf lacqueredCheckBoxBol = True AndAlso gratingMaterial <> "Hot dip galvanized steel" _
            AndAlso gratingMaterial <> "Untreated" Then

            RemoveHandler lacqueredCheckBox.CheckedChanged, AddressOf CheckBox_Lacquered_CheckedChanged
            GUI.Controls.RemoveByKey("CheckBox_Lacquered")
            lacqueredCheckBoxBol = False
            gratingLacquered = False
        End If

        ' Untreated finns för alla som är varmförzinkade enligt Jakob Teams
        Update_AvalaibleMeshes()
    End Sub

    Private Shared Sub CheckBox_Serrated_CheckedChanged(sender As Object, e As EventArgs)
        If sender.Checked Then
            gratingSerrated = True
            Update_AvalaibleMeshes()
        Else
            gratingSerrated = False
            Update_AvalaibleMeshes()
        End If
    End Sub

    Private Shared Sub CheckBox_Lacquered_CheckedChanged(sender As Object, e As EventArgs)
        'Här hanterar vi om den är lackad eller inte
        'påverkar ej övrig kod (förutom kommentar att det är special order)
        If sender.Checked Then
            gratingLacquered = True
        Else
            gratingLacquered = False
        End If
    End Sub

    Private Shared Sub Update_AvalaibleMeshes()
        GUI.ComboBox_Height.Items.Clear()
        GUI.ComboBox_Height.Enabled = False
        GUI.ComboBox_Thickness.Items.Clear()
        GUI.ComboBox_Thickness.Enabled = False

        GUI.ComboBox_MeshSize.Items.Clear()

        Dim meshesDT As DataTable = GUI.dataBase("Meshes")

        Dim typeInDT As String = meshesDT.Rows(0)("TYPE")
        Dim materialInDT As String = meshesDT.Rows(0)("MATERIAL")
        Dim serratedInDT As Boolean = CBool(meshesDT.Rows(0)("SERRATED"))
        For i = 0 To meshesDT.Rows.Count - 1
            If gratingSerrated = serratedInDT AndAlso gratingMaterial = materialInDT AndAlso gratingType = typeInDT Then
                Dim item As String
                item = meshesDT.Rows(i)("LB-SPACING") & "x" & meshesDT.Rows(i)("CB-SPACING") &
                    " (" & meshesDT.Rows(i)("NAME") & ")"

                Dim addItem As Boolean = True
                For j = 0 To GUI.ComboBox_MeshSize.Items.Count - 1
                    If item = GUI.ComboBox_MeshSize.Items(j) Then
                        addItem = False
                        Exit For
                    End If
                Next
                If addItem = True Then
                    GUI.ComboBox_MeshSize.Items.Add(item)
                End If
            End If
            Try
                typeInDT = meshesDT.Rows(i + 1)("TYPE")
                materialInDT = meshesDT.Rows(i + 1)("MATERIAL")
                serratedInDT = meshesDT.Rows(i + 1)("SERRATED")
            Catch ex As Exception
            End Try
        Next

        If GUI.ComboBox_MeshSize.Items.Count = 0 Then
            GUI.ComboBox_MeshSize.Items.Add("")
        End If
    End Sub

    Public Shared Sub MeshChanged(sender As Object, e As EventArgs)
        GUI.ComboBox_Thickness.Items.Clear()
        GUI.ComboBox_Thickness.Enabled = False

        GUI.ComboBox_Height.Enabled = True
        GUI.ComboBox_Height.Items.Clear()
        gratingMesh = GUI.ComboBox_MeshSize.Text
        loadBarSpacing = CInt(gratingMesh.Split("x")(0))
        crossBarSpacing = CInt(gratingMesh.Split("x")(1).Split(" ")(0))
        gratingName = gratingMesh.Split("(")(1).Split(")")(0)

        ' Add heights
        Dim meshesDT As DataTable = GUI.dataBase("Meshes")

        Dim nameInDT As String = meshesDT.Rows(0)("NAME")
        Dim materialInDT As String = meshesDT.Rows(0)("MATERIAL")
        Dim serratedInDT As Boolean = CBool(meshesDT.Rows(0)("SERRATED"))
        Dim LBSpacingInDT As Integer = CInt(meshesDT.Rows(0)("LB-SPACING"))
        Dim CBSpacingInDT As Integer = CInt(meshesDT.Rows(0)("CB-SPACING"))

        Dim tempList As New List(Of Integer)

        For i = 0 To meshesDT.Rows.Count - 1
            If gratingName = nameInDT AndAlso loadBarSpacing = LBSpacingInDT AndAlso crossBarSpacing = CBSpacingInDT _
                AndAlso gratingSerrated = serratedInDT AndAlso gratingMaterial = materialInDT Then

                Dim addItem As Boolean = True
                For j = 0 To tempList.Count - 1
                    If meshesDT.Rows(i)("LB-HEIGHT") = tempList(j) Then
                        addItem = False
                        Exit For
                    End If
                Next
                If addItem = True Then
                    tempList.Add(CInt(meshesDT.Rows(i)("LB-HEIGHT")))
                End If
            End If

            Try
                nameInDT = meshesDT.Rows(i + 1)("NAME")
                materialInDT = meshesDT.Rows(i + 1)("MATERIAL")
                serratedInDT = CBool(meshesDT.Rows(i + 1)("SERRATED"))
                LBSpacingInDT = CInt(meshesDT.Rows(i + 1)("LB-SPACING"))
                CBSpacingInDT = CInt(meshesDT.Rows(i + 1)("CB-SPACING"))
            Catch ex As Exception
            End Try
        Next

        tempList.Sort()
        For i = 0 To tempList.Count - 1
            GUI.ComboBox_Height.Items.Add(tempList(i))
        Next
    End Sub

    Public Shared Sub HeightChanged(sender As Object, e As EventArgs)
        GUI.ComboBox_Thickness.Enabled = True
        GUI.ComboBox_Thickness.Items.Clear()
        gratingHeight = CInt(GUI.ComboBox_Height.Text)

        Dim meshesDT As DataTable = GUI.dataBase("Meshes")

        Dim nameInDT As String = meshesDT.Rows(0)("NAME")
        Dim materialInDT As String = meshesDT.Rows(0)("MATERIAL")
        Dim serratedInDT As Boolean = CBool(meshesDT.Rows(0)("SERRATED"))
        Dim LBSpacingInDT As Integer = CInt(meshesDT.Rows(0)("LB-SPACING"))
        Dim CBSpacingInDT As Integer = CInt(meshesDT.Rows(0)("CB-SPACING"))
        Dim HeightInDT As Integer = CInt(meshesDT.Rows(0)("LB-HEIGHT"))

        Dim tempList As New List(Of Integer)

        For i = 0 To meshesDT.Rows.Count - 1
            If gratingName = nameInDT AndAlso loadBarSpacing = LBSpacingInDT AndAlso crossBarSpacing = CBSpacingInDT _
                AndAlso gratingSerrated = serratedInDT AndAlso gratingMaterial = materialInDT _
                AndAlso gratingHeight = HeightInDT Then

                Dim addItem As Boolean = True
                For j = 0 To tempList.Count - 1
                    If meshesDT.Rows(i)("LB-THICKNESS") = tempList(j) Then
                        addItem = False
                        Exit For
                    End If
                Next
                If addItem = True Then
                    tempList.Add(CInt(meshesDT.Rows(i)("LB-THICKNESS")))
                End If
            End If

            Try
                nameInDT = meshesDT.Rows(i + 1)("NAME")
                materialInDT = meshesDT.Rows(i + 1)("MATERIAL")
                serratedInDT = CBool(meshesDT.Rows(i + 1)("SERRATED"))
                LBSpacingInDT = CInt(meshesDT.Rows(i + 1)("LB-SPACING"))
                CBSpacingInDT = CInt(meshesDT.Rows(i + 1)("CB-SPACING"))
                HeightInDT = CInt(meshesDT.Rows(i + 1)("LB-HEIGHT"))
            Catch ex As Exception
            End Try
        Next

        tempList.Sort()
        For i = 0 To tempList.Count - 1
            GUI.ComboBox_Thickness.Items.Add(tempList(i))
        Next

    End Sub

    Public Shared Sub WholeMeshWidthsChanged(sender As Object, e As EventArgs)
        If sender.Checked Then
            GUI.ComboBox_Width.DropDownStyle = ComboBoxStyle.DropDownList
        Else
            GUI.ComboBox_Width.DropDownStyle = ComboBoxStyle.DropDown
        End If
    End Sub

    Public Shared Sub ThicknessChanged(sender As Object, e As EventArgs)
        GUI.ComboBox_Width.Enabled = True
        GUI.ComboBox_Width.Items.Clear()
        GUI.TextBox_Length.Text = 1000
        GUI.TextBox_Length.Enabled = True
        GUI.CheckBox_WholeMeshWidths.Enabled = True

        loadBarThickness = CInt(GUI.ComboBox_Thickness.Text)

        Dim meshesDT As DataTable = GUI.dataBase("Meshes")

        Dim nameInDT As String = meshesDT.Rows(0)("NAME")
        Dim materialInDT As String = meshesDT.Rows(0)("MATERIAL")
        Dim serratedInDT As Boolean = CBool(meshesDT.Rows(0)("SERRATED"))
        Dim LBSpacingInDT As Integer = CInt(meshesDT.Rows(0)("LB-SPACING"))
        Dim CBSpacingInDT As Integer = CInt(meshesDT.Rows(0)("CB-SPACING"))
        Dim HeightInDT As Integer = CInt(meshesDT.Rows(0)("LB-HEIGHT"))
        Dim ThicknessInDT As Integer = CInt(meshesDT.Rows(0)("LB-THICKNESS"))

        Dim wholeMeshesColumn As String = ""
        For i = 0 To meshesDT.Rows.Count - 1
            If gratingName = nameInDT AndAlso loadBarSpacing = LBSpacingInDT AndAlso crossBarSpacing = CBSpacingInDT _
                AndAlso gratingSerrated = serratedInDT AndAlso gratingMaterial = materialInDT _
                AndAlso gratingHeight = HeightInDT AndAlso loadBarThickness = ThicknessInDT Then

                wholeMeshesColumn = meshesDT.Rows(i)("WHOLE MESHES")
                Exit For
            End If

            Try
                nameInDT = meshesDT.Rows(i + 1)("NAME")
                materialInDT = meshesDT.Rows(i + 1)("MATERIAL")
                serratedInDT = CBool(meshesDT.Rows(i + 1)("SERRATED"))
                LBSpacingInDT = CInt(meshesDT.Rows(i + 1)("LB-SPACING"))
                CBSpacingInDT = CInt(meshesDT.Rows(i + 1)("CB-SPACING"))
                HeightInDT = CInt(meshesDT.Rows(i + 1)("LB-HEIGHT"))
                ThicknessInDT = CInt(meshesDT.Rows(i + 1)("LB-THICKNESS"))
            Catch ex As Exception
            End Try
        Next

        Dim wholeMeshesDT As DataTable = GUI.dataBase("Whole Meshes")
        For i = 0 To wholeMeshesDT.Rows.Count - 1
            If Not IsDBNull(wholeMeshesDT.Rows(i)(wholeMeshesColumn)) Then
                GUI.ComboBox_Width.Items.Add(wholeMeshesDT.Rows(i)(wholeMeshesColumn))
            End If
        Next
    End Sub



    Public Shared Sub WidthChanged(sender As Object, e As EventArgs)
        If GUI.ComboBox_Width.Text <> "" Then
            If CInt(GUI.ComboBox_Width.Text) >= GUI.ComboBox_Width.Items(GUI.ComboBox_Width.Items.Count - 1) AndAlso
                CInt(GUI.ComboBox_Width.Text) <= GUI.ComboBox_Width.Items(0) Then
                gratingMaxW = CInt(GUI.ComboBox_Width.Text)
                GUI_Drawing_Panel.Update_GratingPoints()
            End If
        End If
    End Sub

    Public Shared Sub LengthChanged(sender As Object, e As EventArgs)
        If GUI.TextBox_Length.Text <> "" Then
            If CInt(GUI.TextBox_Length.Text) >= 200 AndAlso CInt(GUI.TextBox_Length.Text) <= 6000 Then
                gratingMaxL = CInt(GUI.TextBox_Length.Text)
                GUI_Drawing_Panel.Update_GratingPoints()
            Else
                'Message: Out of boundary
            End If
        End If
    End Sub




    ' --- Creates the table with points data used in SW ---
    Public Shared Function Create_ExportTable() As DataTable
        Dim lSpacing As String = GUI.ComboBox_MeshSize.Text.Split("x")(0)
        Dim cSpacing As String = GUI.ComboBox_MeshSize.Text.Split("x")(1).Split(" ")(0)

        Dim gratingTable As New DataTable
        gratingTable.Columns.Add("TYPE", GetType(String))
        gratingTable.Columns.Add("SERRATED", GetType(Boolean))
        gratingTable.Columns.Add("WIDTH", GetType(Decimal))
        gratingTable.Columns.Add("LENGTH", GetType(Decimal))
        gratingTable.Columns.Add("LOADBAR_THICKNESS", GetType(Decimal))
        gratingTable.Columns.Add("LOADBAR_HEIGHT", GetType(Decimal))
        gratingTable.Columns.Add("LOADBAR_SPACING", GetType(Decimal))
        gratingTable.Columns.Add("CROSSBAR_SPACING", GetType(Decimal))
        gratingTable.Columns.Add("CROSSBAR_DIAMETER", GetType(Decimal))
        gratingTable.Columns.Add("CROSSBAR_THICKNESS", GetType(Decimal))
        gratingTable.Columns.Add("CROSSBAR_HEIGHT", GetType(Decimal))

        gratingTable.Rows.Add()

        gratingTable.Rows(0)("SERRATED") = False    'Hämta från GUI
        gratingTable.Rows(0)("WIDTH") = CDec(GUI.ComboBox_Width.Text)
        gratingTable.Rows(0)("LENGTH") = CDec(GUI.TextBox_Length.Text)
        gratingTable.Rows(0)("LOADBAR_THICKNESS") = CDec(GUI.ComboBox_Thickness.Text)
        gratingTable.Rows(0)("LOADBAR_HEIGHT") = CDec(GUI.ComboBox_Height.Text)
        gratingTable.Rows(0)("LOADBAR_SPACING") = CDec(lSpacing)
        gratingTable.Rows(0)("CROSSBAR_SPACING") = CDec(cSpacing)

        If GUI.ComboBox_TypeChooser.Text = "Pressure Welded" Then
            gratingTable.Rows(0)("TYPE") = "pressure_welded"  ' Här behövs namn på HLCt modellen
            gratingTable.Rows(0)("CROSSBAR_DIAMETER") = 5
            gratingTable.Rows(0)("CROSSBAR_THICKNESS") = 0
            gratingTable.Rows(0)("CROSSBAR_HEIGHT") = 0
        Else
            gratingTable.Rows(0)("TYPE") = "type_a"  ' Här behövs namn på HLCt modellen
            gratingTable.Rows(0)("CROSSBAR_DIAMETER") = 0
            gratingTable.Rows(0)("CROSSBAR_THICKNESS") = 2
            gratingTable.Rows(0)("CROSSBAR_HEIGHT") = 15
        End If

        Return gratingTable
    End Function


    ' --- Generate a table containing all the points X- and Y-values ---
    Public Shared Function Create_PointTable()
        Dim pointTable As New DataTable
        pointTable.Columns.Add("X", GetType(Decimal))
        pointTable.Columns.Add("Y", GetType(Decimal))

        For i = 0 To GUI_Drawing_Panel.pointsOrder.Count - 1
            Dim pTemp() As Decimal
            pTemp = GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(i))

            pointTable.Rows.Add()
            pointTable.Rows(i)("X") = pTemp(2)
            pointTable.Rows(i)("Y") = pTemp(3)
        Next

        Return pointTable
    End Function


    ' --- Check if the key pressed is a number ---
    Public Shared Sub Check_IfNumber(e As KeyPressEventArgs)
        If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
                e.Handled = True
            End If
        End If
    End Sub

End Class