Public Class Middle_Rectangle
    Private Shared buttonOffset As Integer
    Private Shared middleSquareValues(3) As Integer
    Private Shared numOfMiddleRectangles As Integer = 0

    ' --- Create textboxes when user clicks the rectangle side recess button ---
    Public Shared Sub SquareMiddleButtton(sender As Object, e As EventArgs)
        Individual.Panel_Recesses.Controls("Button_Angle_Corner").Enabled = False
        Individual.Panel_Recesses.Controls("Button_Angle_Side").Enabled = False
        Individual.Panel_Recesses.Controls("Button_Square_Corner").Enabled = False
        Individual.Panel_Recesses.Controls("Button_Square_Side").Enabled = False
        Individual.Panel_Recesses.Controls("Button_Square_Middle").Enabled = False

        Dim buttonX As Integer = Individual.Panel_Recesses.Controls("Button_Square_Middle").Location.X + Individual.Panel_Recesses.Controls("Button_Square_Middle").Width
        Dim buttonY As Integer = Individual.Panel_Recesses.Controls("Button_Square_Middle").Location.Y

        Individual.Create_TextBox_Recesses("Y_TextBox", "", buttonX + 60, buttonY + 10, 60)
        AddHandler Individual.Panel_Recesses.Controls("Y_TextBox").TextChanged, AddressOf FunctionTextMiddle_Changed
        AddHandler Individual.Panel_Recesses.Controls("Y_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        Individual.Create_Label_Recesses("Y_Label", "Width", buttonX + 18, buttonY + 13)

        Individual.Create_TextBox_Recesses("X_TextBox", "", buttonX + 60, buttonY + 40, 60)
        AddHandler Individual.Panel_Recesses.Controls("X_TextBox").TextChanged, AddressOf FunctionTextMiddle_Changed
        AddHandler Individual.Panel_Recesses.Controls("X_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        Individual.Create_Label_Recesses("X_Label", "Length", buttonX + 10, buttonY + 43)

        Individual.Create_TextBox_Recesses("OffX_TextBox", "", buttonX + 185, buttonY + 10, 60)
        AddHandler Individual.Panel_Recesses.Controls("OffX_TextBox").TextChanged, AddressOf FunctionTextMiddle_Changed
        AddHandler Individual.Panel_Recesses.Controls("OffX_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        Individual.Create_Label_Recesses("OffX_Label", "Offset X", buttonX + 126, buttonY + 13)

        Individual.Create_TextBox_Recesses("OffY_TextBox", "", buttonX + 185, buttonY + 40, 60)
        AddHandler Individual.Panel_Recesses.Controls("OffY_TextBox").TextChanged, AddressOf FunctionTextMiddle_Changed
        AddHandler Individual.Panel_Recesses.Controls("OffY_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        Individual.Create_Label_Recesses("OffY_Label", "Offset Y", buttonX + 126, buttonY + 43)

        buttonOffset = 260
        Individual.Panel_Recesses.Controls("Button_Radius").Left = Individual.Panel_Recesses.Controls("Button_Radius").Left + buttonOffset

        Individual.Button_Square_Middle.FlatStyle = FlatStyle.Flat
        Individual.Button_Square_Middle.FlatAppearance.BorderColor = Color.Red
    End Sub

    ' --- Check the key pressed by the user ---
    Private Shared Sub FunctionText_KeyPress(sender As Object, e As KeyPressEventArgs)
        User_Input.Check_IfNumber(e)
    End Sub

    ' --- Adds clickable buttons for all sides if conditions are met ---
    Private Shared Sub FunctionTextMiddle_Changed(sender As TextBox, e As EventArgs)
        Try
            middleSquareValues(0) = CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text)
            middleSquareValues(1) = CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text)
            middleSquareValues(2) = CInt(Individual.Panel_Recesses.Controls("OffX_TextBox").Text)
            middleSquareValues(3) = CInt(Individual.Panel_Recesses.Controls("OffY_TextBox").Text)
        Catch ex As Exception
            middleSquareValues(0) = 0
            middleSquareValues(1) = 0
            middleSquareValues(2) = 0
            middleSquareValues(3) = 0
        End Try

        If middleSquareValues(0) > 0 AndAlso middleSquareValues(1) > 0 AndAlso middleSquareValues(2) > 0 AndAlso middleSquareValues(3) > 0 Then
            If Individual.Panel_Grating.Controls.Count = 2 Then
                Dim middleButton As New Button With {
                    .Width = middleSquareValues(0) / Data.scaleDiff,
                    .Height = middleSquareValues(1) / Data.scaleDiff,
                    .Left = Data.grossAreaPoints.Rows(3)("GUI X") + middleSquareValues(2) / Data.scaleDiff,
                    .Top = Data.grossAreaPoints.Rows(3)("GUI Y") - (middleSquareValues(3) + middleSquareValues(1)) / Data.scaleDiff,
                    .Name = "Button_Middle",
                    .Text = ""
                }

                middleButton.BackColor = Color.FromArgb(50, Color.Red)
                middleButton.FlatStyle = FlatStyle.Flat
                middleButton.FlatAppearance.BorderSize = 0
                middleButton.FlatAppearance.MouseOverBackColor = Color.Red
                middleButton.FlatAppearance.MouseDownBackColor = Color.DarkRed

                Individual.Panel_Grating.Controls.Add(middleButton)

                AddHandler middleButton.Click, AddressOf MiddleButton_Click
            Else
                ' Update button size and position
                Individual.Panel_Grating.Controls("Button_Middle").Width = middleSquareValues(0) / Data.scaleDiff
                Individual.Panel_Grating.Controls("Button_Middle").Height = middleSquareValues(1) / Data.scaleDiff
                Individual.Panel_Grating.Controls("Button_Middle").Left = Data.grossAreaPoints.Rows(3)("GUI X") + middleSquareValues(2) / Data.scaleDiff
                Individual.Panel_Grating.Controls("Button_Middle").Top = Data.grossAreaPoints.Rows(3)("GUI Y") - (middleSquareValues(3) + middleSquareValues(1)) / Data.scaleDiff
            End If

        ElseIf Individual.Panel_Grating.Controls.Count > 2 Then
            RemoveHandler Individual.Panel_Grating.Controls("Button_Middle").Click, AddressOf MiddleButton_Click
            Individual.Panel_Grating.Controls.RemoveByKey("Button_Middle")
        End If
    End Sub

    ' --- When a side function button is pressed ---
    Private Shared Sub MiddleButton_Click(sender As Button, e As EventArgs)
        ' Remove point buttons
        RemoveHandler Individual.Panel_Grating.Controls("Button_Middle").Click, AddressOf MiddleButton_Click
        Individual.Panel_Grating.Controls.RemoveByKey("Button_Middle")

        Dim mesNum As Integer = Data.pointsMeasurements.Count - 1

        Data.recessData.Rows.Add()
        Data.recessData.Rows(Data.recessData.Rows.Count - 1)("NAME") = "MS" & numOfMiddleRectangles

        Dim pointRow1 As DataRow = Data.gratingMiddlePoints.NewRow
        Dim pointRow2 As DataRow = Data.gratingMiddlePoints.NewRow
        Dim pointRow3 As DataRow = Data.gratingMiddlePoints.NewRow
        Dim pointRow4 As DataRow = Data.gratingMiddlePoints.NewRow

        pointRow1("NAME") = "MS" & numOfMiddleRectangles & "_" & 1
        pointRow1("X") = Data.grossAreaPoints.Rows(3)("X") + middleSquareValues(2) / 1000
        pointRow1("Y") = Data.grossAreaPoints.Rows(3)("Y") + (middleSquareValues(3) + middleSquareValues(1)) / 1000
        pointRow1("GUI X") = Data.grossAreaPoints.Rows(3)("GUI X") + middleSquareValues(2) / Data.scaleDiff
        pointRow1("GUI Y") = Data.grossAreaPoints.Rows(3)("GUI Y") - (middleSquareValues(3) + middleSquareValues(1)) / Data.scaleDiff

        pointRow2("NAME") = "MS" & numOfMiddleRectangles & "_" & 2
        pointRow2("X") = Data.grossAreaPoints.Rows(3)("X") + (middleSquareValues(2) + middleSquareValues(0)) / 1000
        pointRow2("Y") = Data.grossAreaPoints.Rows(3)("Y") + (middleSquareValues(3) + middleSquareValues(1)) / 1000
        pointRow2("GUI X") = Data.grossAreaPoints.Rows(3)("GUI X") + (middleSquareValues(2) + middleSquareValues(0)) / Data.scaleDiff
        pointRow2("GUI Y") = Data.grossAreaPoints.Rows(3)("GUI Y") - (middleSquareValues(3) + middleSquareValues(1)) / Data.scaleDiff

        pointRow3("NAME") = "MS" & numOfMiddleRectangles & "_" & 3
        pointRow3("X") = Data.grossAreaPoints.Rows(3)("X") + (middleSquareValues(2) + middleSquareValues(0)) / 1000
        pointRow3("Y") = Data.grossAreaPoints.Rows(3)("Y") + middleSquareValues(3) / 1000
        pointRow3("GUI X") = Data.grossAreaPoints.Rows(3)("GUI X") + (middleSquareValues(2) + middleSquareValues(0)) / Data.scaleDiff
        pointRow3("GUI Y") = Data.grossAreaPoints.Rows(3)("GUI Y") - middleSquareValues(3) / Data.scaleDiff

        pointRow4("NAME") = "MS" & numOfMiddleRectangles & "_" & 4
        pointRow4("X") = Data.grossAreaPoints.Rows(3)("X") + middleSquareValues(2) / 1000
        pointRow4("Y") = Data.grossAreaPoints.Rows(3)("Y") + middleSquareValues(3) / 1000
        pointRow4("GUI X") = Data.grossAreaPoints.Rows(3)("GUI X") + middleSquareValues(2) / Data.scaleDiff
        pointRow4("GUI Y") = Data.grossAreaPoints.Rows(3)("GUI Y") - middleSquareValues(3) / Data.scaleDiff

        numOfMiddleRectangles += 1

        Dim sidePoints3 As New List(Of Integer)
        For j = 2 To Data.gratingPoints.Rows.Count - 1
            If Data.gratingPoints.Rows(j)("GUI Y") = Data.grossAreaPoints.Rows(2)("GUI Y") Then
                sidePoints3.Add(j)
            End If
        Next

        Dim sidePoints4 As New List(Of Integer)
        For j = 3 To Data.gratingPoints.Rows.Count - 1
            If Data.gratingPoints.Rows(j)("GUI X") = Data.grossAreaPoints.Rows(3)("GUI X") Then
                sidePoints4.Add(j)
            End If
        Next
        sidePoints4.Add(0)

        ' Add recess measuements (SW)
        Data.pointsMeasurements.Add("MS1_" & numOfMiddleRectangles & "_" & mesNum, {sidePoints4(0) + 1, numOfMiddleRectangles * 4, 3})
        Data.pointsMeasurements.Add("MS1_" & numOfMiddleRectangles & "_" & mesNum + 1, {sidePoints3(sidePoints3.Count - 1) + 1, numOfMiddleRectangles * 4, 4})
        Data.pointsMeasurements.Add("MS_" & numOfMiddleRectangles & "_" & mesNum + 2, {numOfMiddleRectangles * 4, numOfMiddleRectangles * 4 - 1, 3})
        Data.pointsMeasurements.Add("MS_" & numOfMiddleRectangles & "_" & mesNum + 3, {numOfMiddleRectangles * 4 - 3, numOfMiddleRectangles * 4, 4})


        ' Add support line data for sidesquare measurement
        Dim lineOffset As Integer = 5
        Draw_Grating.sideSquareMesLine.Add("MS1_" & numOfMiddleRectangles & "_" & mesNum,
                                           {Data.grossAreaPoints.Rows(3)("GUI X") + lineOffset,
                                           Data.grossAreaPoints.Rows(3)("GUI Y") - middleSquareValues(3) / Data.scaleDiff,
                                           Data.grossAreaPoints.Rows(3)("GUI X") + middleSquareValues(2) / Data.scaleDiff - lineOffset,
                                           Data.grossAreaPoints.Rows(3)("GUI Y") - middleSquareValues(3) / Data.scaleDiff})
        Draw_Grating.sideSquareMesLine.Add("MS1_" & numOfMiddleRectangles & "_" & mesNum + 1,
                                           {Data.grossAreaPoints.Rows(3)("GUI X") + middleSquareValues(2) / Data.scaleDiff,
                                           Data.grossAreaPoints.Rows(3)("GUI Y") - lineOffset,
                                           Data.grossAreaPoints.Rows(3)("GUI X") + middleSquareValues(2) / Data.scaleDiff,
                                           Data.grossAreaPoints.Rows(3)("GUI Y") - middleSquareValues(3) / Data.scaleDiff + lineOffset})

        ' Add recess measurments (GUI)
        Draw_Grating.measureLabels.Add("MS1_" & numOfMiddleRectangles & "_" & mesNum,
                                       {CInt(Individual.Panel_Recesses.Controls("OffX_TextBox").Text),
                                       Data.grossAreaPoints.Rows(3)("GUI X") + (middleSquareValues(2) / Data.scaleDiff) / 2 - 8,
                                       Data.grossAreaPoints.Rows(3)("GUI Y") - middleSquareValues(3) / Data.scaleDiff - 12 - 3,
                                       1})
        Draw_Grating.measureLabels.Add("MS1_" & numOfMiddleRectangles & "_" & mesNum + 1,
                                       {CInt(Individual.Panel_Recesses.Controls("OffY_TextBox").Text),
                                       Data.grossAreaPoints.Rows(3)("GUI X") + middleSquareValues(2) / Data.scaleDiff + 3,
                                       Data.grossAreaPoints.Rows(3)("GUI Y") - middleSquareValues(3) / Data.scaleDiff + (middleSquareValues(3) / Data.scaleDiff) / 2 - 6,
                                       1})
        Draw_Grating.measureLabels.Add("MS_" & numOfMiddleRectangles & "_" & mesNum + 2,
                                       {CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text),
                                       Data.grossAreaPoints.Rows(3)("GUI X") + middleSquareValues(2) / Data.scaleDiff + (CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text) / Data.scaleDiff) / 2 - 8,
                                       Data.grossAreaPoints.Rows(3)("GUI Y") - middleSquareValues(3) / Data.scaleDiff - CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text) / Data.scaleDiff - 12 - 3,
                                       1})
        Draw_Grating.measureLabels.Add("MS_" & numOfMiddleRectangles & "_" & mesNum + 3,
                                       {CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text),
                                       Data.grossAreaPoints.Rows(3)("GUI X") + middleSquareValues(2) / Data.scaleDiff + CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text) / Data.scaleDiff + 3,
                                       Data.grossAreaPoints.Rows(3)("GUI Y") - middleSquareValues(3) / Data.scaleDiff - (CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text) / Data.scaleDiff) / 2 - 6,
                                       1})


        ' Recess Data
        Data.recessData.Rows(Data.recessData.Rows.Count - 1)("RECESS TYPE") = "MIDDLE SQUARE"
        Data.recessData.Rows(Data.recessData.Rows.Count - 1)("WIDTH") = CInt(Individual.Panel_Recesses.Controls("Y_TextBox").Text)
        Data.recessData.Rows(Data.recessData.Rows.Count - 1)("LENGTH") = CInt(Individual.Panel_Recesses.Controls("X_TextBox").Text)
        Data.recessData.Rows(Data.recessData.Rows.Count - 1)("OFFSET X") = CInt(Individual.Panel_Recesses.Controls("OffX_TextBox").Text)
        Data.recessData.Rows(Data.recessData.Rows.Count - 1)("OFFSET Y") = CInt(Individual.Panel_Recesses.Controls("OffY_TextBox").Text)



        Data.gratingMiddlePoints.Rows.InsertAt(pointRow1, numOfMiddleRectangles * 4 - 4)
        Data.gratingMiddlePoints.Rows.InsertAt(pointRow2, numOfMiddleRectangles * 4 - 3)
        Data.gratingMiddlePoints.Rows.InsertAt(pointRow3, numOfMiddleRectangles * 4 - 2)
        Data.gratingMiddlePoints.Rows.InsertAt(pointRow4, numOfMiddleRectangles * 4 - 1)

        'Redraw grating
        Individual.Panel_Grating.Refresh()

        RemoveHandler Individual.Panel_Recesses.Controls("X_TextBox").TextChanged, AddressOf FunctionTextMiddle_Changed
        RemoveHandler Individual.Panel_Recesses.Controls("X_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        Individual.Panel_Recesses.Controls.RemoveByKey("X_TextBox")

        RemoveHandler Individual.Panel_Recesses.Controls("Y_TextBox").TextChanged, AddressOf FunctionTextMiddle_Changed
        RemoveHandler Individual.Panel_Recesses.Controls("Y_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        Individual.Panel_Recesses.Controls.RemoveByKey("Y_TextBox")

        RemoveHandler Individual.Panel_Recesses.Controls("OffX_TextBox").TextChanged, AddressOf FunctionTextMiddle_Changed
        RemoveHandler Individual.Panel_Recesses.Controls("OffX_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        Individual.Panel_Recesses.Controls.RemoveByKey("OffX_TextBox")

        RemoveHandler Individual.Panel_Recesses.Controls("OffY_TextBox").TextChanged, AddressOf FunctionTextMiddle_Changed
        RemoveHandler Individual.Panel_Recesses.Controls("OffY_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        Individual.Panel_Recesses.Controls.RemoveByKey("OffY_TextBox")

        Individual.Panel_Recesses.Controls.RemoveByKey("X_Label")
        Individual.Panel_Recesses.Controls.RemoveByKey("Y_Label")
        Individual.Panel_Recesses.Controls.RemoveByKey("OffX_Label")
        Individual.Panel_Recesses.Controls.RemoveByKey("OffY_Label")

        Individual.Panel_Recesses.Controls("Button_Angle_Corner").Enabled = True
        Individual.Panel_Recesses.Controls("Button_Angle_Side").Enabled = True
        Individual.Panel_Recesses.Controls("Button_Square_Corner").Enabled = True
        Individual.Panel_Recesses.Controls("Button_Square_Side").Enabled = True
        Individual.Panel_Recesses.Controls("Button_Square_Middle").Enabled = True

        Individual.Panel_Recesses.Controls("Button_Radius").Left = Individual.Panel_Recesses.Controls("Button_Radius").Left - buttonOffset

        Individual.Button_Square_Middle.FlatAppearance.BorderColor = Color.Black
    End Sub
End Class
