Public Class User_Input
    Public Shared gratingType, gratingMaterial, gratingMesh, gratingName As String
    Public Shared gratingSerrated As Boolean = False
    Public Shared gratingLacquered As Boolean = False
    Public Shared loadBarSpacing, crossBarSpacing, gratingHeight, loadBarThickness As Integer
    Public Shared CBDiameter, CBHeight, CBThickness As Double
    Public Shared wholeMeshesColumn As String

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

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


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

        Dim materialDT As DataTable = Database.database.Tables("Material")

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

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

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

        Dim serratedDT As DataTable = Database.database.Tables("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 = Individual.ComboBox_Material.Left
                    serratedCheckBox.Top = Individual.ComboBox_Material.Top + 25
                    AddHandler serratedCheckBox.CheckedChanged, AddressOf CheckBox_Serrated_CheckedChanged
                    Individual.Panel_Data.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
                Individual.Panel_Data.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 = Individual.ComboBox_Material.Left
            lacqueredCheckBox.Top = Individual.ComboBox_Material.Top + 50
            AddHandler lacqueredCheckBox.CheckedChanged, AddressOf CheckBox_Lacquered_CheckedChanged
            Individual.Panel_Data.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
            Individual.Panel_Data.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()
        Individual.ComboBox_Height.Items.Clear()
        Individual.ComboBox_Height.Enabled = False
        Individual.ComboBox_Thickness.Items.Clear()
        Individual.ComboBox_Thickness.Enabled = False

        Individual.ComboBox_Mesh.Items.Clear()

        Dim meshesDT As DataTable = Database.database.Tables("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 Individual.ComboBox_Mesh.Items.Count - 1
                    If item = Individual.ComboBox_Mesh.Items(j) Then
                        addItem = False
                        Exit For
                    End If
                Next
                If addItem = True Then
                    Individual.ComboBox_Mesh.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 Individual.ComboBox_Mesh.Items.Count = 0 Then
            Individual.ComboBox_Mesh.Items.Add("")
        End If
    End Sub

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

        Individual.ComboBox_Height.Enabled = True
        Individual.ComboBox_Height.Items.Clear()
        gratingMesh = Individual.ComboBox_Mesh.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 = Database.database.Tables("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
            Individual.ComboBox_Height.Items.Add(tempList(i))
        Next
    End Sub

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

        Dim meshesDT As DataTable = Database.database.Tables("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
            Individual.ComboBox_Thickness.Items.Add(tempList(i))
        Next
    End Sub


    Public Shared Sub ThicknessChanged(sender As Object, e As EventArgs)
        Individual.ComboBox_Width.Enabled = True
        Individual.ComboBox_Width.Items.Clear()
        Individual.TextBox_Width.Enabled = True
        Individual.TextBox_Length.Text = 1000
        Individual.TextBox_Length.Enabled = True
        If AppForm.fillMode = False Then
            Individual.Panel_Data.Controls("CheckBox_Whole_Mesh").Enabled = True
        End If

        loadBarThickness = CInt(Individual.ComboBox_Thickness.Text)

        Dim meshesDT As DataTable = Database.database.Tables("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"))

        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")
                If gratingType = "Pressure Welded" Then
                    CBDiameter = meshesDT.Rows(i)("CB-DIAMETER")
                    CBHeight = 0
                    CBThickness = 0
                Else
                    CBDiameter = 0
                    CBHeight = meshesDT.Rows(i)("CB-HEIGHT")
                    CBThickness = meshesDT.Rows(i)("CB-THICKNESS")
                End If
                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 = Database.database.Tables("Whole Meshes")
        For i = 0 To wholeMeshesDT.Rows.Count - 1
            If Not IsDBNull(wholeMeshesDT.Rows(i)(wholeMeshesColumn)) Then
                Individual.ComboBox_Width.Items.Add(wholeMeshesDT.Rows(i)(wholeMeshesColumn))
            End If
        Next

        Individual.ComboBox_Width.Text = Individual.ComboBox_Width.Items(0)
        Individual.TextBox_Width.Text = 1000
    End Sub


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

            Individual.Panel_Data.Controls.RemoveByKey("CheckBox_Edge_Bar")
        Else
            Individual.ComboBox_Width.DropDownStyle = ComboBoxStyle.DropDown

            Dim CheckTemp As New CheckBox With {
           .Name = "CheckBox_Edge_Bar",
           .Text = "Extra Edge Bar",
           .Left = 110,
           .Top = 495,
           .Font = New Font("Microsoft Sans Serif", 7.8),
           .Checked = True,
           .Enabled = True,
           .AutoSize = True,
           .BackColor = Color.Transparent
        }
            AddHandler CheckTemp.CheckedChanged, AddressOf CheckBox_Edge_Bar_CheckedChanged
            Individual.Panel_Data.Controls.Add(CheckTemp)
        End If
    End Sub


    Public Shared Sub CheckBox_Edge_Bar_CheckedChanged(sender As Object, e As EventArgs)

    End Sub


    Public Shared Sub WidthChanged(sender As Object, e As EventArgs)
        If AppForm.fillMode Then
            If Individual.TextBox_Width.Text <> "" Then
                If CInt(Individual.TextBox_Width.Text) >= Individual.ComboBox_Width.Items(Individual.ComboBox_Width.Items.Count - 1) Then
                    Data.gratingW = CInt(Individual.TextBox_Width.Text)
                    Draw_Grating.Update_GratingPoints()
                End If
                Enable_Buttons()
            End If
        Else
            If Individual.ComboBox_Width.Text <> "" Then
                If CInt(Individual.ComboBox_Width.Text) >= Individual.ComboBox_Width.Items(Individual.ComboBox_Width.Items.Count - 1) AndAlso
                    CInt(Individual.ComboBox_Width.Text) <= Individual.ComboBox_Width.Items(0) Then
                    Data.gratingW = CInt(Individual.ComboBox_Width.Text)
                    Draw_Grating.Update_GratingPoints()
                End If
                Enable_Buttons()
            End If
        End If
    End Sub

    Public Shared Sub LengthChanged(sender As Object, e As EventArgs)
        If Individual.TextBox_Length.Text <> "" Then
            If AppForm.fillMode Then
                If CInt(Individual.TextBox_Length.Text) >= 200 Then
                    Data.gratingL = CInt(Individual.TextBox_Length.Text)
                    Draw_Grating.Update_GratingPoints()
                Else
                    'Message: Out of boundary
                End If
                Enable_Buttons()
            Else
                If CInt(Individual.TextBox_Length.Text) >= 200 AndAlso CInt(Individual.TextBox_Length.Text) <= 6000 Then
                    Data.gratingL = CInt(Individual.TextBox_Length.Text)
                    Draw_Grating.Update_GratingPoints()
                Else
                    'Message: Out of boundary
                End If
                Enable_Buttons()
            End If
        End If
    End Sub

    Private Shared Sub Enable_Buttons()
        If AppForm.fillMode Then
            If Individual.TextBox_Width.Text <> "" AndAlso Individual.TextBox_Length.Text <> "" Then
                Individual.Button_Export_GUI.Enabled = True
                Individual.Button_Export_SW.Enabled = True
                Individual.Panel_Data.Controls("Button_Calculate").Enabled = True

                Individual.Button_Angle_Corner.Enabled = True
                Individual.Button_Angle_Side.Enabled = True
                Individual.Button_Square_Corner.Enabled = True
                Individual.Button_Square_Side.Enabled = True
                Individual.Button_Square_Middle.Enabled = True

                Individual.Panel_Data.Controls("TextBox_Max_Load").Enabled = True
            End If
        Else
            If Individual.ComboBox_Width.Text <> "" AndAlso Individual.TextBox_Length.Text <> "" Then
                Individual.Button_Export_GUI.Enabled = True
                Individual.Button_Export_SW.Enabled = True

                Individual.Button_Angle_Corner.Enabled = True
                Individual.Button_Angle_Side.Enabled = True
                Individual.Button_Square_Corner.Enabled = True
                Individual.Button_Square_Side.Enabled = True
                Individual.Button_Square_Middle.Enabled = True
            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 = Individual.ComboBox_Mesh.Text.Split("x")(0)
        Dim cSpacing As String = Individual.ComboBox_Mesh.Text.Split("x")(1).Split(" ")(0)

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

        gratingTable.Rows.Add()

        gratingTable.Rows(0)("MATERIAL") = gratingMaterial
        gratingTable.Rows(0)("NAME") = gratingName
        gratingTable.Rows(0)("SERRATED") = gratingSerrated
        gratingTable.Rows(0)("LACQUERED") = gratingLacquered
        gratingTable.Rows(0)("WIDTH") = CInt(Individual.ComboBox_Width.Text)
        gratingTable.Rows(0)("LENGTH") = CInt(Individual.TextBox_Length.Text)
        gratingTable.Rows(0)("LOADBAR_THICKNESS") = CInt(Individual.ComboBox_Thickness.Text)
        gratingTable.Rows(0)("LOADBAR_HEIGHT") = CInt(Individual.ComboBox_Height.Text)
        gratingTable.Rows(0)("LOADBAR_SPACING") = CInt(lSpacing)
        gratingTable.Rows(0)("CROSSBAR_SPACING") = CInt(cSpacing)

        If Individual.ComboBox_Type.Text = "Pressure Welded" Then
            If gratingSerrated = True Then
                gratingTable.Rows(0)("TYPE") = "pressure_welded_serrated"
            Else
                gratingTable.Rows(0)("TYPE") = "pressure_welded"
            End If
            gratingTable.Rows(0)("CROSSBAR_DIAMETER") = CBDiameter
            gratingTable.Rows(0)("CROSSBAR_THICKNESS") = 0
            gratingTable.Rows(0)("CROSSBAR_HEIGHT") = 0
        Else
            If gratingSerrated = True Then
                gratingTable.Rows(0)("TYPE") = "type_a_serrated"
            Else
                gratingTable.Rows(0)("TYPE") = "type_a"
            End If
            gratingTable.Rows(0)("CROSSBAR_DIAMETER") = 0
            gratingTable.Rows(0)("CROSSBAR_THICKNESS") = 2      'CBThickness    FIXA
            gratingTable.Rows(0)("CROSSBAR_HEIGHT") = 20        'CBHeight       FIXA
        End If

        Return gratingTable
    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
