Imports UTS_Core.Expression

Namespace UTSModule.Test.Command.ConverterCommand
    Public Class GetValueExecutor
        Inherits TestCommandExecutor

        Private ReadOnly _expression As String

        Sub New(command As TestCommand)
            MyBase.New(command)

            _expression = command.Parameter(0)
        End Sub


        Public Overrides Function Execute() As TestCommandReturn
            '检测有无需要计算的表达式
            If String.IsNullOrWhiteSpace(_expression) Then
                CommandReturn.RecordValue = String.Empty
                CommandReturn.ExecuteResultTipString = $"无表达式默认通过"
                CommandReturn.ExecuteResult = True
                Return CommandReturn
            End If

            '检测表达式的有效性
            If StringExpression.CheckExpressionString(_expression) = False Then
                CommandReturn.RecordValue = String.Empty
                CommandReturn.ExecuteResultTipString = $"无效的表达式[{_expression}]"
                CommandReturn.ExecuteResult = False
                Return CommandReturn
            End If

            Try
                CommandReturn.RecordValue = StringExpression.GetDoubleExpressionResult(_expression).ToString()
                CommandReturn.ExecuteResultTipString = $"表达式[{_expression}]计算的结果为：{CommandReturn.RecordValue}"
                CommandReturn.ExecuteResult = True
            Catch ex As Exception
                CommandReturn.RecordValue = String.Empty
                CommandReturn.ExecuteResult = False
                CommandReturn.ExecuteResultTipString = $"表达式[{_expression}]计算失败"
                Return CommandReturn
            End Try

            Return CommandReturn
        End Function

    End Class
End Namespace