Imports System.Drawing
Imports System.Windows.Forms
Imports FlexCell

Namespace UTSModule.Project
    Public Class FrmStationPlan
        Private _isShown As Boolean

        Public Property StationPlan() As ProjectStationPlan

        Public Property StationPlanCommandHelpers As Dictionary(Of String, StationPlanCommandHelper)

        Public Property StationPlanColHelpers() As Dictionary(Of String, PlanColHelper)

        Public Sub ShowForm(parentControl As Control)
            FormBorderStyle = FormBorderStyle.None
            TopLevel = False
            Dock = DockStyle.Fill
            Parent = parentControl
            Enabled = StationPlan IsNot Nothing

            Show()
        End Sub


        Public Sub Station_Changed(station As StationInfo)
            StationPlan = station.Packet.StationPlan

            Enabled = StationPlan IsNot Nothing

            If StationPlan IsNot Nothing Then
                GrdTestPlan.AutoRedraw = False

                GrdTestPlan.LoadFromStream(StationPlan.StationPlanGrid.ExportToStream())
                GrdTestPlan.ComboBox(StationPlanGrid.ColNameEnum.Command).Items.Clear()
                GrdTestPlan.ComboBox(StationPlanGrid.ColNameEnum.Command).Items.AddRange(StationPlanCommandHelpers.Keys.ToArray())

                GrdTestPlan.AutoRedraw = True
                GrdTestPlan.Refresh()

                StationPlan.StationPlanGrid.Dispose() '清空资源
                StationPlan.StationPlanGrid = GrdTestPlan '保证站包与站流程表指向内容一致

                LockedTestPlanGrid(StationPlan.TestPlanLocked)
            End If
        End Sub

        Public Sub LockedTestPlanGrid(testPlanLocked As Boolean)
            GrdTestPlan.Range(1, 1, GrdTestPlan.Rows - 1, GrdTestPlan.Cols - 1).Locked = testPlanLocked
            If testPlanLocked Then
                TsBtnLockedEdit.Text = $"解锁表格"
                TsBtnLockedEdit.ForeColor = Color.Red
            Else
                TsBtnLockedEdit.Text = $"加锁表格"
                TsBtnLockedEdit.ForeColor = Color.Green
            End If
        End Sub


        Private Sub TsBtnLockedEdit_Click(sender As Object, e As EventArgs) Handles TsBtnLockedEdit.Click
            If StationPlan.TestPlanLocked Then
                If StationPlan.ParentPacket.PassWord.Length > 0 Then
                    Dim pwd As String = InputBox(String.Empty, $"请输入解锁密码")
                    If String.Compare(pwd, StationPlan.ParentPacket.PassWord) <> 0 Then Return
                End If
                StationPlan.TestPlanLocked = False
                LockedTestPlanGrid(StationPlan.TestPlanLocked)
            Else
                StationPlan.TestPlanLocked = True
                LockedTestPlanGrid(StationPlan.TestPlanLocked)
            End If
        End Sub

        Private Sub InitStationPlanCommandHelper()
            Try
                StationPlanCommandHelpers = StationPlanCommandHelper.InitCommandHelper(Database.Sqlite.ConnectionParams.Path, Database.Sqlite.ConnectionParams.Password)
            Catch ex As Exception
                Console.WriteLine($"InitStationPlanCommandHelper Error:{ex.Message}")
            End Try
            If StationPlanCommandHelpers Is Nothing Then
                StationPlanCommandHelpers = New Dictionary(Of String, StationPlanCommandHelper)()
            End If

        End Sub

        Private Sub InitStationPlanColHelper()
            Try
                StationPlanColHelpers = PlanColHelper.InitPlanColHelper(Database.Sqlite.ConnectionParams.Path, Database.Sqlite.ConnectionParams.Password)
            Catch ex As Exception
                Console.WriteLine($"InitStationPlanColHelper Error:{ex.Message}")
            End Try

            If StationPlanColHelpers Is Nothing Then
                StationPlanColHelpers = New Dictionary(Of String, PlanColHelper)()
            End If
        End Sub

        Private Sub InitForm()
            Dim commands As String() = StationPlanCommandHelpers.Keys.ToArray()
            StationPlanGrid.InitGrid(GrdTestPlan, commands)
            SingleRowGrid.InitGrid(GrdSingleRowInfo, commands)
        End Sub

        Private Sub FrmStationPlan_Load(sender As Object, e As EventArgs) Handles Me.Load
            _isShown = False

            InitStationPlanCommandHelper()

            InitStationPlanColHelper()

            InitForm()

            _isShown = True
        End Sub

        Private Sub TsBtnReleaseStation_Click(sender As Object, e As EventArgs) Handles TsBtnReleaseStation.Click

        End Sub


#Region "TestPlan"

        Private Sub TestPlanSelChange(selRow As Integer, selCol As Integer)
            Static lastRow As Integer = 0
            Static lastCol As Integer = 0

            If selRow < 1 OrElse selRow > GrdTestPlan.Rows - 1 Then Return
            If selCol < 0 OrElse selCol > GrdTestPlan.Cols - 1 Then Return

            _cellChanged = GridCellChangedEnum.TestPlanSelChange

            If lastRow <> selRow Then
                SyncTestPlanRow(selRow)
                ChangeSingleRowGridColName()

                UpdateSingleRowTip(TestPlanColToSingleRow(selCol))
                lastRow = selRow
            End If

            If lastCol <> selCol Then
                SingleRowSelChange(TestPlanColToSingleRow(selCol))
                lastCol = selCol
            End If

            _cellChanged = GridCellChangedEnum.None
        End Sub


        ''' <summary>
        ''' 流程表格选中行修改时，将流程表格某行的内容,填充到单行信息表中
        ''' </summary>
        ''' <param name="selRow"></param>
        Private Sub SyncTestPlanRow(selRow As Integer)
            Dim col As Integer = SingleRowGrid.ColNameEnum.ColValue

            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Step, col).Text = selRow.ToString()

            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.[Label], col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.[Label]).Text
            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Action, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.Action).Text

            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Command, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.Command).Text
            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Description, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.Description).Text

            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Retry, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.Retry).Text
            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.RetryInterval, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.RetryInterval).Text

            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Record, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.Record).Text
            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.RecordName, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.RecordName).Text

            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.PassGoto, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.PassGoto).Text
            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.FailGoto, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.FailGoto).Text

            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.ErrorCode, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.ErrorCode).Text
            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.ErrorMessage, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.ErrorMessage).Text

            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Help, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.Help).Text

            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter1, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.Parameter1).Text
            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter2, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.Parameter2).Text
            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter3, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.Parameter3).Text
            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter4, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.Parameter4).Text
            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter5, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.Parameter5).Text
            GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter6, col).Text = GrdTestPlan.Cell(selRow, StationPlanGrid.ColNameEnum.Parameter6).Text
        End Sub

        ''' <summary>
        ''' 命令变更时，修改单行信息表中列名
        ''' </summary>
        Private Sub ChangeSingleRowGridColName()
            Static colNameChanged As Boolean = False

            Dim col As Integer = SingleRowGrid.ColNameEnum.ColValue
            Dim command As String = GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Command, col).Text
            Dim nameCol As Integer = SingleRowGrid.ColNameEnum.ColName

            If String.IsNullOrEmpty(command) OrElse StationPlanCommandHelpers.ContainsKey(command) = False Then
                If colNameChanged Then

                    GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter1, nameCol).Text = SingleRowGrid.RowNameEnum.Parameter1.ToString()
                    GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter2, nameCol).Text = SingleRowGrid.RowNameEnum.Parameter2.ToString()
                    GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter3, nameCol).Text = SingleRowGrid.RowNameEnum.Parameter3.ToString()
                    GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter4, nameCol).Text = SingleRowGrid.RowNameEnum.Parameter4.ToString()
                    GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter5, nameCol).Text = SingleRowGrid.RowNameEnum.Parameter5.ToString()
                    GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter6, nameCol).Text = SingleRowGrid.RowNameEnum.Parameter6.ToString()

                    colNameChanged = False
                End If
            Else
                Dim commandHelper As StationPlanCommandHelper = StationPlanCommandHelpers.Item(command)

                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter1, nameCol).Text = commandHelper.Params(0).Desc
                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter2, nameCol).Text = commandHelper.Params(1).Desc
                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter3, nameCol).Text = commandHelper.Params(2).Desc
                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter4, nameCol).Text = commandHelper.Params(3).Desc
                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter5, nameCol).Text = commandHelper.Params(4).Desc
                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter6, nameCol).Text = commandHelper.Params(5).Desc

                If colNameChanged = False Then colNameChanged = True
            End If
        End Sub


        Private Sub GrdTestPlan_SelChange(sender As Object, e As Grid.SelChangeEventArgs) Handles GrdTestPlan.SelChange
            TestPlanSelChange(e.FirstRow, e.FirstCol)
        End Sub

        ''' <summary>
        ''' 测试流程表列号转换成单行信息表的对应行号
        ''' </summary>
        ''' <param name="col"></param>
        ''' <returns></returns>
        Private Function TestPlanColToSingleRow(col As Integer) As Integer
            Dim row As Integer
            Select Case col
                Case StationPlanGrid.ColNameEnum.Step
                    row = SingleRowGrid.RowNameEnum.Step

                Case StationPlanGrid.ColNameEnum.[Label]
                    row = SingleRowGrid.RowNameEnum.[Label]
                Case StationPlanGrid.ColNameEnum.Action
                    row = SingleRowGrid.RowNameEnum.Action

                Case StationPlanGrid.ColNameEnum.Command
                    row = SingleRowGrid.RowNameEnum.Command
                Case StationPlanGrid.ColNameEnum.Description
                    row = SingleRowGrid.RowNameEnum.Description

                Case StationPlanGrid.ColNameEnum.Parameter1
                    row = SingleRowGrid.RowNameEnum.Parameter1
                Case StationPlanGrid.ColNameEnum.Parameter2
                    row = SingleRowGrid.RowNameEnum.Parameter2
                Case StationPlanGrid.ColNameEnum.Parameter3
                    row = SingleRowGrid.RowNameEnum.Parameter3
                Case StationPlanGrid.ColNameEnum.Parameter4
                    row = SingleRowGrid.RowNameEnum.Parameter4
                Case StationPlanGrid.ColNameEnum.Parameter5
                    row = SingleRowGrid.RowNameEnum.Parameter5
                Case StationPlanGrid.ColNameEnum.Parameter6
                    row = SingleRowGrid.RowNameEnum.Parameter6

                Case StationPlanGrid.ColNameEnum.Retry
                    row = SingleRowGrid.RowNameEnum.Retry
                Case StationPlanGrid.ColNameEnum.RetryInterval
                    row = SingleRowGrid.RowNameEnum.RetryInterval

                Case StationPlanGrid.ColNameEnum.Record
                    row = SingleRowGrid.RowNameEnum.Record
                Case StationPlanGrid.ColNameEnum.RecordName
                    row = SingleRowGrid.RowNameEnum.RecordName

                Case StationPlanGrid.ColNameEnum.PassGoto
                    row = SingleRowGrid.RowNameEnum.PassGoto
                Case StationPlanGrid.ColNameEnum.FailGoto
                    row = SingleRowGrid.RowNameEnum.FailGoto

                Case StationPlanGrid.ColNameEnum.ErrorCode
                    row = SingleRowGrid.RowNameEnum.ErrorCode
                Case StationPlanGrid.ColNameEnum.ErrorMessage
                    row = SingleRowGrid.RowNameEnum.ErrorMessage

                Case StationPlanGrid.ColNameEnum.Help
                    row = SingleRowGrid.RowNameEnum.Help
                Case Else
                    Console.WriteLine($"SyncTestPlanChange Unknown Col:{col}")
            End Select
            Return row
        End Function

        ''' <summary>
        ''' 测试流程表内容修改时，同步更新单行信息表内容
        ''' </summary>
        ''' <param name="e"></param>
        Private Sub SyncTestPlanChange(e As Grid.CellChangingEventArgs)
            Dim row As Integer = TestPlanColToSingleRow(e.Col)
            Dim col As Integer = SingleRowGrid.ColNameEnum.ColValue

            GrdSingleRowInfo.Cell(row, col).Text = GrdTestPlan.Cell(e.Row, e.Col).Text
        End Sub

        Private _dropDownRow As Integer
        Private _dropDownCol As Integer

        ''' <summary>
        ''' 测试流程表格命令变更时,应用帮助信息
        ''' </summary>
        Private Sub GrdTestPlanCommandChanged()
            Dim key As String = GrdTestPlan.Cell(_dropDownRow, _dropDownCol).Text
            If StationPlanCommandHelpers.ContainsKey(key) Then
                Dim planCommandHelper As StationPlanCommandHelper = StationPlanCommandHelpers.Item(key)

                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.Action).Text = "1"

                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.Description).Text = planCommandHelper.CommandDesc

                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.Parameter1).Text = planCommandHelper.Params(0).Value
                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.Parameter2).Text = planCommandHelper.Params(1).Value
                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.Parameter3).Text = planCommandHelper.Params(2).Value
                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.Parameter4).Text = planCommandHelper.Params(3).Value
                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.Parameter5).Text = planCommandHelper.Params(4).Value
                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.Parameter6).Text = planCommandHelper.Params(5).Value

                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.Retry).Text = planCommandHelper.Retry
                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.RetryInterval).Text = planCommandHelper.RetryInterval

                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.Record).Text = planCommandHelper.Record
                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.RecordName).Text = planCommandHelper.RecordName

                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.PassGoto).Text = planCommandHelper.PassGoto
                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.FailGoto).Text = planCommandHelper.FailGoto

                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.ErrorCode).Text = planCommandHelper.ErrorCode
                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.ErrorMessage).Text = planCommandHelper.ErrorMessage

                GrdTestPlan.Cell(_dropDownRow, StationPlanGrid.ColNameEnum.Help).Text = planCommandHelper.Help
            End If

            ChangeSingleRowGridColName() '变更单行信息表列名
        End Sub

        Private Sub GrdTestPlan_CellChanging(sender As Object, e As Grid.CellChangingEventArgs) Handles GrdTestPlan.CellChanging
            If _isShown = False Then Return
            If _cellChanged = GridCellChangedEnum.None Then _cellChanged = GridCellChangedEnum.TestPlan

            If _cellChanged = GridCellChangedEnum.TestPlan Then
                SyncTestPlanChange(e)
                _cellChanged = GridCellChangedEnum.None
            End If
        End Sub

        Private Sub GrdTestPlan_ComboClick(sender As Object, e As Grid.ComboClickEventArgs) Handles GrdTestPlan.ComboClick
            If _dropDownCol = StationPlanGrid.ColNameEnum.Command Then
                GrdTestPlanCommandChanged()
            End If
        End Sub

        Private Sub GrdTestPlan_ComboDropDown(sender As Object, e As Grid.ComboDropDownEventArgs) Handles GrdTestPlan.ComboDropDown
            _dropDownRow = e.Row
            _dropDownCol = e.Col
        End Sub
#End Region

#Region "SingleRow"
        Enum GridCellChangedEnum
            None
            TestPlan
            SingleRowInfo
            TestPlanSelChange
        End Enum

        Private _cellChanged As GridCellChangedEnum = GridCellChangedEnum.None

        Private _grdSingleRowDropRow As Integer
        Private _grdSingleRowDropCol As Integer

        Private Function SingleRowToTestPlanCol(row As Integer) As Integer
            Dim col As Integer
            Select Case row
                Case SingleRowGrid.RowNameEnum.[Label]
                    col = StationPlanGrid.ColNameEnum.[Label]
                Case SingleRowGrid.RowNameEnum.Action
                    col = StationPlanGrid.ColNameEnum.Action

                Case SingleRowGrid.RowNameEnum.Command
                    col = StationPlanGrid.ColNameEnum.Command
                Case SingleRowGrid.RowNameEnum.Description
                    col = StationPlanGrid.ColNameEnum.Description

                Case SingleRowGrid.RowNameEnum.Parameter1
                    col = StationPlanGrid.ColNameEnum.Parameter1
                Case SingleRowGrid.RowNameEnum.Parameter2
                    col = StationPlanGrid.ColNameEnum.Parameter2
                Case SingleRowGrid.RowNameEnum.Parameter3
                    col = StationPlanGrid.ColNameEnum.Parameter3
                Case SingleRowGrid.RowNameEnum.Parameter4
                    col = StationPlanGrid.ColNameEnum.Parameter4
                Case SingleRowGrid.RowNameEnum.Parameter5
                    col = StationPlanGrid.ColNameEnum.Parameter5
                Case SingleRowGrid.RowNameEnum.Parameter6
                    col = StationPlanGrid.ColNameEnum.Parameter6

                Case SingleRowGrid.RowNameEnum.Retry
                    col = StationPlanGrid.ColNameEnum.Retry
                Case SingleRowGrid.RowNameEnum.RetryInterval
                    col = StationPlanGrid.ColNameEnum.RetryInterval

                Case SingleRowGrid.RowNameEnum.Record
                    col = StationPlanGrid.ColNameEnum.Record
                Case SingleRowGrid.RowNameEnum.RecordName
                    col = StationPlanGrid.ColNameEnum.RecordName

                Case SingleRowGrid.RowNameEnum.PassGoto
                    col = StationPlanGrid.ColNameEnum.PassGoto
                Case SingleRowGrid.RowNameEnum.FailGoto
                    col = StationPlanGrid.ColNameEnum.FailGoto

                Case SingleRowGrid.RowNameEnum.ErrorCode
                    col = StationPlanGrid.ColNameEnum.ErrorCode
                Case SingleRowGrid.RowNameEnum.ErrorMessage
                    col = StationPlanGrid.ColNameEnum.ErrorMessage

                Case SingleRowGrid.RowNameEnum.Help
                    col = StationPlanGrid.ColNameEnum.Help
                Case Else
                    Console.WriteLine($"SyncSingleRowChange Unknown Row:{row}")
            End Select
            Return col
        End Function

        ''' <summary>
        ''' 单行信息表格内容修改时,同步修改测试流程表的内容
        ''' </summary>
        ''' <param name="e"></param>
        Private Sub SyncSingleRowChange(e As Grid.CellChangingEventArgs)
            If e.Col <> SingleRowGrid.ColNameEnum.ColValue Then Return

            If GrdTestPlan.Selection Is Nothing Then Return
            If GrdTestPlan.Selection.FirstRow <= 0 OrElse GrdTestPlan.Selection.LastCol >= GrdTestPlan.Cols Then Return

            Dim col As Integer = SingleRowToTestPlanCol(e.Row)
            Dim row As Integer = GrdTestPlan.Selection.FirstRow
            GrdTestPlan.Cell(row, col).Text = GrdSingleRowInfo.Cell(e.Row, e.Col).Text
        End Sub

        ''' <summary>
        ''' 单行信息表格命令修改后,同步刷新本身信息
        ''' </summary>
        Private Sub GrdSingleRowInfoCommandChanged()
            Dim key As String = GrdSingleRowInfo.Cell(_grdSingleRowDropRow, _grdSingleRowDropCol).Text
            If StationPlanCommandHelpers.ContainsKey(key) Then
                Dim planCommandHelper As StationPlanCommandHelper = StationPlanCommandHelpers.Item(key)
                Dim col As Integer = SingleRowGrid.ColNameEnum.ColValue

                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Action, col).Text = "1"

                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Description, col).Text = planCommandHelper.CommandDesc

                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter1, col).Text = planCommandHelper.Params(0).Value
                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter2, col).Text = planCommandHelper.Params(1).Value
                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter3, col).Text = planCommandHelper.Params(2).Value
                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter4, col).Text = planCommandHelper.Params(3).Value
                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter5, col).Text = planCommandHelper.Params(4).Value
                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Parameter6, col).Text = planCommandHelper.Params(5).Value

                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Retry, col).Text = planCommandHelper.Retry
                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.RetryInterval, col).Text = planCommandHelper.RetryInterval

                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Record, col).Text = planCommandHelper.Record
                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.RecordName, col).Text = planCommandHelper.RecordName

                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.PassGoto, col).Text = planCommandHelper.PassGoto
                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.FailGoto, col).Text = planCommandHelper.FailGoto

                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.ErrorCode, col).Text = planCommandHelper.ErrorCode
                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.ErrorMessage, col).Text = planCommandHelper.ErrorMessage

                GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Help, col).Text = planCommandHelper.Help
            End If
        End Sub
        Private Sub GrdSingleRowInfo_CellChanging(sender As Object, e As Grid.CellChangingEventArgs) Handles GrdSingleRowInfo.CellChanging
            If _isShown = False Then Return
            If _cellChanged = GridCellChangedEnum.None Then _cellChanged = GridCellChangedEnum.SingleRowInfo

            If _cellChanged = GridCellChangedEnum.SingleRowInfo Then
                SyncSingleRowChange(e)
                _cellChanged = GridCellChangedEnum.None
            End If
        End Sub

        Private Sub GrdSingleRowInfo_ComboDropDown(sender As Object, e As Grid.ComboDropDownEventArgs) Handles GrdSingleRowInfo.ComboDropDown
            _grdSingleRowDropRow = e.Row
            _grdSingleRowDropCol = e.Col

            Select Case e.Row
                Case SingleRowGrid.RowNameEnum.Command
                    GrdSingleRowInfo.ComboBox(0).Items.Clear()
                    GrdSingleRowInfo.ComboBox(0).Items.AddRange(StationPlanCommandHelpers.Keys.ToArray())
            End Select
        End Sub

        Private Sub GrdSingleRowInfo_ComboClick(sender As Object, e As Grid.ComboClickEventArgs) Handles GrdSingleRowInfo.ComboClick
            If _grdSingleRowDropRow = SingleRowGrid.RowNameEnum.Command Then
                GrdSingleRowInfoCommandChanged()
            End If
        End Sub

#End Region


#Region "TestPlan 右键菜单"
        Private Sub MsiAddPlanRow_Click(sender As Object, e As EventArgs) Handles MsiAddPlanRow.Click, MsiPlanAddRow.Click
            If GrdTestPlan.Selection Is Nothing Then
                GrdTestPlan.AddItem("")
            Else
                GrdTestPlan.InsertRow(GrdTestPlan.Selection.FirstRow, 1)
            End If
        End Sub

        Private Sub 删除一行ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 删除一行ToolStripMenuItem.Click
            If GrdTestPlan.Selection IsNot Nothing Then
                GrdTestPlan.Selection.DeleteByRow()
            End If
        End Sub

        Private Sub 内容自适应ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 内容自适应ToolStripMenuItem.Click
            GrdTestPlan.AutoSize = Not GrdTestPlan.AutoSize
            If GrdTestPlan.AutoSize Then
                GrdTestPlan.AutoSize = False
                内容自适应ToolStripMenuItem.Text = $"取消自适应"
            Else
                GrdTestPlan.AutoSize = True
                内容自适应ToolStripMenuItem.Text = $"内容自适应"
            End If
        End Sub

        Private Sub 调整字体ToolStripMenuItem_DropDownItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles 调整字体ToolStripMenuItem.DropDownItemClicked
            GrdTestPlan.Selection.FontName = e.ClickedItem.Text
        End Sub

        Private Sub 调整字色ToolStripMenuItem_DropDownItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles 调整字色ToolStripMenuItem.DropDownItemClicked
            GrdTestPlan.Selection.ForeColor = e.ClickedItem.ForeColor
        End Sub

        Private Sub 调整背景ToolStripMenuItem_DropDownItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles 调整背景ToolStripMenuItem.DropDownItemClicked
            GrdTestPlan.Selection.BackColor = e.ClickedItem.BackColor
        End Sub

        Private Sub 对齐方式ToolStripMenuItem_DropDownItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles 对齐方式ToolStripMenuItem.DropDownItemClicked
            Dim alignment As AlignmentEnum = AlignmentEnum.CenterCenter
            Select Case e.ClickedItem.Text
                Case "上层居左"
                    alignment = AlignmentEnum.LeftTop
                Case "上层居中"
                    alignment = AlignmentEnum.CenterTop
                Case "上层居右"
                    alignment = AlignmentEnum.RightTop
                Case "中层居左"
                    alignment = AlignmentEnum.LeftCenter
                Case "中层居中"
                    alignment = AlignmentEnum.CenterCenter
                Case "中层居右"
                    alignment = AlignmentEnum.RightCenter
                Case "下层居左"
                    alignment = AlignmentEnum.LeftBottom
                Case "下层居中"
                    alignment = AlignmentEnum.CenterBottom
                Case "下层居右"
                    alignment = AlignmentEnum.RightBottom
                Case Else
                    Console.WriteLine($"对齐方式ToolStripMenuItem_DropDownItemClicked Unknown Text:{e.ClickedItem.Text}")
            End Select
            GrdTestPlan.Selection.Alignment = alignment
        End Sub

        Private Sub 修改主题ToolStripMenuItem_DropDownItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles 修改主题ToolStripMenuItem.DropDownItemClicked
            Select Case e.ClickedItem.Text
                Case "默认主题"

                Case Else
                    Console.WriteLine($"修改主题ToolStripMenuItem_DropDownItemClicked Unknown Text:{e.ClickedItem.Text}")
            End Select
        End Sub
#End Region


#Region "TestPlan光标追随"
        Private _mouseFollow As Boolean = False '是否开启鼠标跟随功能
        Private _mouseMoveLastRow As Integer = 0 '鼠标移动上次所在行

        Private Sub TsBtnMouseFollow_Click(sender As Object, e As EventArgs) Handles TsBtnMouseFollow.Click
            If _mouseFollow Then
                _mouseFollow = False

                GrdTestPlan.Range(_mouseMoveLastRow, 1, _mouseMoveLastRow, GrdTestPlan.Cols - 1).Borders(EdgeEnum.Outside) = LineStyleEnum.None

                TsBtnMouseFollow.Text = $"光标预览"
            Else
                _mouseFollow = True
                TsBtnMouseFollow.Text = $"取消预览"


            End If
        End Sub

        Private Sub TestPlanMouseMoveChangeBorders(mouseRow As Integer)
            If _mouseMoveLastRow = mouseRow Then Return
            If mouseRow < 1 OrElse mouseRow > GrdTestPlan.Rows - 1 Then Return

            GrdTestPlan.Range(_mouseMoveLastRow, 1, _mouseMoveLastRow, GrdTestPlan.Cols - 1).Borders(EdgeEnum.Outside) = LineStyleEnum.None

            GrdTestPlan.Range(mouseRow, 1, mouseRow, GrdTestPlan.Cols - 1).Borders(EdgeEnum.Outside) = LineStyleEnum.Thin

            _mouseMoveLastRow = mouseRow
        End Sub

        Private Sub TestPlanMouseMove()
            If _mouseFollow = False Then Return

            Dim mouseRow As Integer = GrdTestPlan.MouseRow
            Dim mouseCol As Integer = GrdTestPlan.MouseCol

            TestPlanSelChange(mouseRow, mouseCol)

            TestPlanMouseMoveChangeBorders(mouseRow)
        End Sub

        Private Sub GrdTestPlan_MouseMove(sender As Object, e As MouseEventArgs) Handles GrdTestPlan.MouseMove
            TestPlanMouseMove()
        End Sub


        Private Sub GrdTestPlan_Scroll(sender As Object, e As EventArgs) Handles GrdTestPlan.Scroll
            TestPlanMouseMove()
        End Sub
#End Region

#Region "SingleRow 提示选择列"
        Private _lastSingleRowSelCol As Integer
        Private _lastSingleRowSelRow As Integer

        Private Sub UpdatePlanColNameTip(colName As String)
            If StationPlanColHelpers.ContainsKey(colName) Then
                Dim colHelper As PlanColHelper = StationPlanColHelpers.Item(colName)
                RtxColTip.SuspendLayout()
                RtxColTip.Clear()

                RtxColTip.AppendText($"{colHelper.ColName}{vbCrLf}")
                RtxColTip.AppendText($"类型：{colHelper.ColType}{vbCrLf}")
                RtxColTip.AppendText($"描述：{colHelper.ColDesc}{vbCrLf}")
                RtxColTip.ResumeLayout(False)
            End If
        End Sub


        Private Sub UpdateCommandTip(colName As String, command As String, paramIndex As Integer)
            If StationPlanCommandHelpers.ContainsKey(command) Then
                Dim commandHelper As StationPlanCommandHelper = StationPlanCommandHelpers.Item(command)
                RtxColTip.SuspendLayout()
                RtxColTip.Clear()

                RtxColTip.AppendText($"{colName}{vbCrLf}")
                RtxColTip.AppendText($"类型：{commandHelper.Params(paramIndex).Type}{vbCrLf}")
                RtxColTip.AppendText($"上限：{commandHelper.Params(paramIndex).UpperLimit}{vbCrLf}")
                RtxColTip.AppendText($"下限：{commandHelper.Params(paramIndex).LowerLimit}{vbCrLf}")
                RtxColTip.AppendText($"描述：{commandHelper.Params(paramIndex).Desc}{vbCrLf}")
                RtxColTip.ResumeLayout(False)
            Else
                RtxColTip.AppendText($"{colName}{vbCrLf}")
            End If
        End Sub

        ''' <summary>
        ''' 单行信息表格更新当前行
        ''' </summary>
        ''' <param name="row"></param>
        Private Sub UpdateSingleRowTip(row As Integer)
            Dim colName As String = GrdSingleRowInfo.Cell(row, SingleRowGrid.ColNameEnum.ColName).Text

            If row >= SingleRowGrid.RowNameEnum.Parameter1 AndAlso row <= SingleRowGrid.RowNameEnum.Parameter6 Then
                Dim command As String = GrdSingleRowInfo.Cell(SingleRowGrid.RowNameEnum.Command, SingleRowGrid.ColNameEnum.ColValue).Text
                If String.IsNullOrEmpty(command) Then
                    UpdatePlanColNameTip(colName)
                Else
                    UpdateCommandTip(colName, command, row - SingleRowGrid.RowNameEnum.Parameter1)
                End If
            Else
                UpdatePlanColNameTip(colName)
            End If

        End Sub

        Private Sub SingleRowSelChange(row As Integer)
            If _lastSingleRowSelRow = row Then Return

            '新增命令切换处理

            GrdSingleRowInfo.Cell(_lastSingleRowSelRow, SingleRowGrid.ColNameEnum.ColName).FontBold = False

            GrdSingleRowInfo.Cell(row, SingleRowGrid.ColNameEnum.ColName).FontBold = True

            UpdateSingleRowTip(row) '更新列提示

            _lastSingleRowSelRow = row
        End Sub

        Private Sub GrdSingleRowInfo_SelChange(sender As Object, e As Grid.SelChangeEventArgs) Handles GrdSingleRowInfo.SelChange
            SingleRowSelChange(e.FirstRow)
        End Sub
#End Region

    End Class
End Namespace