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

        Private ReadOnly _exp As String

        Private ReadOnly _startString As String

        Private ReadOnly _stringLenght As String

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

            _exp = command.Parameter(0)
            _startString = command.Parameter(1)
            _stringLenght = command.Parameter(2)
        End Sub

        Public Overrides Function Execute() As TestCommandReturn
            If String.IsNullOrEmpty(_exp) Then

                CommandReturn.RecordValue = ""
                CommandReturn.ExecuteResultTipString = $"源字符串不能为空"
                CommandReturn.ExecuteResult = False

                Return CommandReturn
            End If

            Dim startIndex As Integer
            If Integer.TryParse(_startString, startIndex) = False Then
                CommandReturn.RecordValue = ""
                CommandReturn.ExecuteResultTipString = $"无效的起始位置"
                CommandReturn.ExecuteResult = False

                Return CommandReturn
            End If


            If String.IsNullOrWhiteSpace(_stringLenght) Then
                Try
                    CommandReturn.RecordValue = _exp.Substring(startIndex)
                    CommandReturn.ExecuteResultTipString = $"获取的字符串结果为：{CommandReturn.RecordValue}"
                    CommandReturn.ExecuteResult = True
                Catch ex As Exception
                    CommandReturn.RecordValue = ""
                    CommandReturn.ExecuteResultTipString = $"{ex.Message}"
                    CommandReturn.ExecuteResult = False
                End Try

            Else
                Dim length As Integer

                If Integer.TryParse(_stringLenght, length) = False Then
                    CommandReturn.RecordValue = ""
                    CommandReturn.ExecuteResultTipString = $"无效的字符长度"
                    CommandReturn.ExecuteResult = False

                    Return CommandReturn
                End If


                Try
                    CommandReturn.RecordValue = _exp.Substring(startIndex, length)
                    CommandReturn.ExecuteResultTipString = $"获取的字符串结果为：{CommandReturn.RecordValue}"
                    CommandReturn.ExecuteResult = True
                Catch ex As Exception
                    CommandReturn.RecordValue = ""
                    CommandReturn.ExecuteResultTipString = $"{ex.Message}"
                    CommandReturn.ExecuteResult = False
                End Try
            End If

            Return CommandReturn
        End Function
    End Class
End Namespace