Imports System.Drawing
Imports System.Windows.Forms

Imports UTS_Core.UTSModule.Test.Controls

Namespace UTSModule.Test.Command.SystemCommand
    Public Class ShowTipsExecutor
        Inherits TestCommandExecutor

        ''' <summary>显示提示的控件</summary>
        Public Shared TipControl As utsLabel

        ''' <summary>提示文本</summary>
        Private ReadOnly _tipTxt As String

        ''' <summary>背景颜色</summary>
        Private ReadOnly _bgColor As String

        ''' <summary>字体颜色</summary>
        Private ReadOnly _fontColor As String

        ''' <summary>字体名称</summary>
        Private ReadOnly _fontName As String

        ''' <summary>字体大小</summary>
        Private ReadOnly _fontSize As Integer

        ''' <summary>字体加粗</summary>
        Private ReadOnly _fontBold As Integer

        ''' <summary>显示模式</summary>
        Private ReadOnly _mode As Integer

        ''' <summary>模式参数</summary>
        Private ReadOnly _modeParam As String

        Private Function HexStringToColor(hexString As String) As Color
            Dim val As Integer = Convert.ToInt32(hexString, 16)
            Return Color.FromArgb(val >> 16 And &HFF, val >> 8 And &HFF, val And &HFF)
        End Function
        Sub New(command As TestCommand)
            MyBase.New(command)

            _tipTxt = TestCommand.Parameter(0)

            _bgColor = TestCommand.Parameter(1)

            _fontColor = TestCommand.Parameter(2)

            _fontName = TestCommand.Parameter(3)

            If Integer.TryParse(TestCommand.Parameter(4), _fontSize) = False Then
                _fontSize = 12 '默认字体大小
            End If

            If Integer.TryParse(TestCommand.Parameter(5), _fontBold) = False Then
                _fontBold = 0 '默认不加粗
            End If

            If Integer.TryParse(TestCommand.Parameter(6), _mode) = False Then
                _mode = 0 '默认正常显示
            End If

            If String.IsNullOrEmpty(TestCommand.Parameter(7)) Then
                _modeParam = "0:0:0:0:0"
            Else
                _modeParam = TestCommand.Parameter(7)
            End If


            CommandReturn.LowerLimit = "True"

            CommandReturn.UpperLimit = "True"

            CommandReturn.RecordValue = String.Empty

            CommandReturn.ExecuteResultTipString = String.Empty

            CommandReturn.ExecuteResult = True
        End Sub

        Public Overrides Function Execute() As TestCommandReturn
            If TipControl Is Nothing Then
                CommandReturn.ExecuteResult = False
                CommandReturn.RecordValue = "False"
                CommandReturn.ExecuteResultTipString = $"未指定控件"
                Return CommandReturn
            End If


            If TipControl.InvokeRequired Then
                Return CType(TipControl.Invoke(New Func(Of TestCommandReturn)(AddressOf Execute)), TestCommandReturn)
            End If

            Try
                '委托执行
                TipControl.Text = _tipTxt
                TipControl.BackColor = HexStringToColor(_bgColor)
                TipControl.ForeColor = HexStringToColor(_fontColor)

                If String.IsNullOrEmpty(_fontName) = False Then
                    If _fontBold = 1 Then
                        TipControl.Font = New Font(_fontName, _fontSize, FontStyle.Bold)
                    Else
                        TipControl.Font = New Font(_fontName, _fontSize)
                    End If
                End If

                TipControl.modePara = _modeParam
                TipControl.dispMode = _mode

                CommandReturn.ExecuteResult = True
                CommandReturn.RecordValue = "True"
                CommandReturn.ExecuteResultTipString = "提示文字设置成功"
            Catch ex As Exception
                CommandReturn.ExecuteResult = False
                CommandReturn.RecordValue = "False"
                CommandReturn.ExecuteResultTipString = $"未知错误:{ex.Message}"
            End Try

            Return CommandReturn
        End Function
    End Class
End Namespace
