
Public Class Grating_Fill
    Public Shared maxSingleWidth, maxSingleLength As Integer
    Public Shared numOfHorizontal, numOfVertical As Integer
    Public Shared widthRevNeeded, lengthRevNeeded As Boolean
    Public Shared revLength, revWidth As Integer
    Public Shared minLength As Integer

    Public Shared gratingDimensions As New DataTable
    Public Shared Sub Calculate_Grid()
        Get_SingleGratingMaxDimensions()

        gratingDimensions.Clear()
        gratingDimensions.Columns.Clear()
        gratingDimensions.Columns.Add("INDEX", GetType(Integer))
        gratingDimensions.Columns.Add("ROW", GetType(Integer))
        gratingDimensions.Columns.Add("COLUMN", GetType(Integer))
        gratingDimensions.Columns.Add("WIDTH", GetType(Integer))
        gratingDimensions.Columns.Add("LENGTH", GetType(Integer))

        If (Data.gratingL / maxSingleLength) Mod 1 = 0 Then
            numOfHorizontal = Data.gratingL / maxSingleLength
        Else
            numOfHorizontal = Math.Floor(Data.gratingL / maxSingleLength) + 1
        End If

        If (Data.gratingW / maxSingleWidth) Mod 1 = 0 Then
            numOfVertical = Data.gratingW / maxSingleWidth
        Else
            numOfVertical = Math.Floor(Data.gratingW / maxSingleWidth) + 1
        End If

        minLength = (Math.Floor((Data.gratingL / 100) / numOfHorizontal) + 1) * 100
        If minLength > 1600 AndAlso (minLength / 100) Mod 2 = 1 Then
            minLength += 100
        End If
        If minLength > maxSingleLength Then
            minLength = maxSingleLength
        End If

        Dim endLength As Integer = Data.gratingL - (numOfHorizontal - 1) * minLength

        'If Data.gratingL - (numOfHorizontal - 1) * maxSingleLength < 200 Then
        '    endLength = 200
        '    revLength = maxSingleLength - (endLength - (Data.gratingL - (numOfHorizontal - 1) * maxSingleLength))
        '    lengthRevNeeded = True
        'Else
        '    endLength = Data.gratingL - (numOfHorizontal - 1) * maxSingleLength
        '    lengthRevNeeded = False
        'End If

        Dim endWidth As Integer
        If Data.gratingW - (numOfVertical - 1) * maxSingleWidth < 200 Then
            Dim neededLoadbars As Integer = Math.Floor((200 - (Data.gratingW - (numOfVertical - 1) * maxSingleWidth)) / User_Input.loadBarSpacing) + 1
            endWidth = Data.gratingW - (numOfVertical - 1) * maxSingleWidth + neededLoadbars * User_Input.loadBarSpacing
            revWidth = maxSingleWidth - (endWidth - (Data.gratingW - (numOfVertical - 1) * maxSingleWidth))
            widthRevNeeded = True
        Else
            endWidth = Data.gratingW - (numOfVertical - 1) * maxSingleWidth
            widthRevNeeded = False
        End If

        Dim row As Integer = 1
        For i = 0 To numOfHorizontal * numOfVertical - 1
            Dim tempDR As DataRow = gratingDimensions.NewRow
            tempDR("INDEX") = i
            tempDR("ROW") = row - 1
            tempDR("COLUMN") = i - numOfHorizontal * (row - 1)
            If CDbl((i + 1) / numOfHorizontal) = row Then
                tempDR("LENGTH") = endLength
                row += 1
                'ElseIf CDbl((i + 2) / numOfHorizontal) = row AndAlso lengthRevNeeded Then
                '    tempDR("LENGTH") = revLength
            Else
                tempDR("LENGTH") = minLength
            End If

            If i > numOfHorizontal * numOfVertical - 1 - numOfHorizontal Then
                tempDR("WIDTH") = endWidth
            ElseIf i > numOfHorizontal * numOfVertical - 1 - 2 * numOfHorizontal AndAlso widthRevNeeded Then
                tempDR("WIDTH") = revWidth
            Else
                tempDR("WIDTH") = maxSingleWidth
            End If
            gratingDimensions.Rows.Add(tempDR)
        Next


        Individual.Panel_Grating.Refresh()
    End Sub

    Private Shared Sub Get_SingleGratingMaxDimensions()
        maxSingleWidth = Database.database.Tables("Whole Meshes").Rows(0)(User_Input.wholeMeshesColumn)

        Dim loadDT As DataTable = Database.database.Tables("LOAD")

        Dim nameInDT As String = loadDT.Rows(0)("NAME")
        Dim LBHeightInDT As Integer = loadDT.Rows(0)("LB-HEIGHT")
        Dim LBThicknessInDT As Integer = loadDT.Rows(0)("LB-THICKNESS")

        Dim loadName As String = User_Input.gratingName.Substring(0, 1)
        If User_Input.gratingName.Substring(User_Input.gratingName.Length - 1, 1) = "T" Then
            loadName += "-T"
            If loadName = "F-T" Then
                loadName = "H-T"    ' Fixa: Tillfällig då det inte finns load data för F-T
            End If
        ElseIf loadName = "A" Then
            loadName += User_Input.gratingMesh.Split(" ")(0)
        End If

        For i = 0 To loadDT.Rows.Count - 1
            If loadName = nameInDT AndAlso User_Input.gratingHeight = LBHeightInDT _
                AndAlso User_Input.loadBarThickness = LBThicknessInDT Then

                For j = loadDT.Columns.Count - 1 To 0 Step -1
                    If loadDT.Rows(i)(j) > CInt(Individual.Panel_Data.Controls("TextBox_Max_Load").Text) Then
                        maxSingleLength = CInt(loadDT.Columns(j).ColumnName)
                        Exit For
                    End If
                Next

                Exit For
            End If
            Try
                nameInDT = loadDT.Rows(i + 1)("NAME")
                LBHeightInDT = loadDT.Rows(i + 1)("LB-HEIGHT")
                LBThicknessInDT = loadDT.Rows(i + 1)("LB-THICKNESS")
            Catch ex As Exception
            End Try
        Next
    End Sub

    ' Not used because of better method found
    Public Shared Sub Calculate_Recesses()
        Dim cornerAngleCounter As Integer = 0
        For k = 0 To Data.recessData.Rows.Count - 1
            If Data.recessData.Rows(k)("RECESS TYPE") = "ANGLE" Then
                cornerAngleCounter += 1
                For i = 1 To 2
                    Dim pX, pY As Integer
                    For Each gDR As DataRow In Data.gratingPoints.Rows
                        If gDR("NAME") = "CA" & cornerAngleCounter & "_" & i Then
                            pX = Data.gratingL / 2 + CInt(gDR("X") * 1000)
                            pY = Data.gratingW / 2 - CInt(gDR("Y") * 1000)
                            Exit For
                        End If
                    Next

                    Dim colIndex As Integer
                    For j = 0 To numOfHorizontal - 1
                        If pX >= j * minLength AndAlso pX <= (j + 1) * minLength Then
                            colIndex = j
                            Exit For
                        End If
                    Next

                    Dim rowIndex As Integer
                    For j = 0 To numOfVertical - 1
                        If widthRevNeeded AndAlso j = numOfVertical - 2 AndAlso pY >= j * maxSingleWidth AndAlso pY <= j * maxSingleWidth + revWidth Then
                            rowIndex = j
                            Exit For
                        ElseIf widthRevNeeded AndAlso j = numOfVertical - 1 Then
                            rowIndex = j
                            Exit For
                        ElseIf widthRevNeeded = False AndAlso pY >= j * maxSingleWidth AndAlso pY <= (j + 1) * maxSingleWidth Then
                            rowIndex = j
                            Exit For
                        End If
                    Next

                    Dim gratingIndex As Integer = rowIndex * numOfHorizontal + colIndex

                    Dim tempList As New List(Of Integer)
                    Try
                        tempList = Data.recessData.Rows(k)("GRATINGS")
                    Catch ex As Exception
                    End Try

                    Dim indexNotAdded As Boolean = True
                    For j = 0 To tempList.Count - 1
                        If gratingIndex = tempList(j) Then
                            indexNotAdded = False
                            Exit For
                        End If
                    Next
                    If indexNotAdded Then
                        tempList.Add(gratingIndex)
                    End If

                    Data.recessData.Rows(k)("GRATINGS") = tempList
                Next

                Dim tempList2 As New List(Of Integer)
                Try
                    tempList2 = Data.recessData.Rows(k)("GRATINGS")
                Catch ex As Exception
                End Try

                Dim gratingIndex2 As Integer

                If Data.recessData.Rows(k)("CORNER") = 1 Then
                    gratingIndex2 = 0
                ElseIf Data.recessData.Rows(k)("CORNER") = 2 Then
                    gratingIndex2 = numOfHorizontal - 1
                ElseIf Data.recessData.Rows(k)("CORNER") = 3 Then
                    gratingIndex2 = numOfHorizontal * numOfVertical - 1
                Else
                    gratingIndex2 = numOfHorizontal * (numOfVertical - 1)
                End If

                Dim indexNotAdded2 As Boolean = True
                For j = 0 To tempList2.Count - 1
                    If gratingIndex2 = tempList2(j) Then
                        indexNotAdded2 = False
                        Exit For
                    End If
                Next
                If indexNotAdded2 Then
                    tempList2.Add(gratingIndex2)
                End If

                Data.recessData.Rows(k)("GRATINGS") = tempList2
            End If
        Next
    End Sub
End Class
