Imports System.Drawing
Imports UTS_Core.UTSModule.DbTableModel.Manage.ErrCodeTable

Namespace UTSModule.Production
    Public Class ErrCodeManager
        ''' <summary>错误代码管理器，全局唯一</summary>
        Private Shared _manager As ErrCodeManager

        ''' <summary>初始化测试器线程锁</summary>
        Private Shared ReadOnly InitLock As New Object()


        Private ReadOnly _errCodeList As List(Of ErrCode)

        Private ReadOnly _defaultErrCode As ErrCode

        ''' <summary>
        ''' 创建单例管理器,并初始化管理器内容
        ''' </summary>
        ''' <returns></returns>
        Public Shared Function CreateManager() As ErrCodeManager
            If _manager Is Nothing Then
                SyncLock InitLock
                    Threading.Thread.MemoryBarrier()
                    If _manager Is Nothing Then
                        _manager = New ErrCodeManager
                        _manager.InitializeErrCode()
                    End If
                End SyncLock
            End If
            Return _manager
        End Function

        Private Sub New()
            _errCodeList = New List(Of ErrCode)()

            _defaultErrCode = New ErrCode()
        End Sub

        Public Sub InitializeErrCode()
            Dim dtTable As DataTable = DbConnect.DbConnector.GetAllErrCode()
            _errCodeList.Clear()

            For Each row As DataRow In dtTable.Rows
                Dim code As String = row($"{ColNamesEnum.ErrCode}").ToString()
                Dim msg As String = row($"{ColNamesEnum.ErrMsg}").ToString()
                Dim color As Color = HexStringToColor(row($"{ColNamesEnum.ErrColor}").ToString())
                _errCodeList.Add(New ErrCode(code, msg, color))
            Next
        End Sub

        Public Function IndexOf(code As String) As Integer
            For i As Integer = 0 To _errCodeList.Count - 1
                If _errCodeList(i).CodeEqual(code) Then Return i
            Next
            Return -1
        End Function

        Default Public ReadOnly Property Item(index As Integer) As ErrCode
            Get
                If index = -1 OrElse index >= _errCodeList.Count Then
                    Return _defaultErrCode
                End If

                Return _errCodeList(index)
            End Get
        End Property

        Default Public ReadOnly Property Item(code As String) As ErrCode
            Get
                Return Item(IndexOf(code))
            End Get
        End Property


        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

        ''' <summary>
        ''' 提取错误代码与错误消息字符串中的错误代码
        ''' </summary>
        ''' <returns></returns>
        Public Shared Function CodeMsgToCode(codeMsg As String) As String
            Dim length As Integer = codeMsg.IndexOf(":"c)
            If length > 0 Then Return Mid(codeMsg, 1, codeMsg.IndexOf(":"c))
            Return codeMsg
        End Function

        Private Function Count() As Integer
            Return _errCodeList.Count
        End Function

        Public Function GetAllCodes() As String()
            Dim arr(_errCodeList.Count - 1) As String
            For i As Integer = 0 To _errCodeList.Count - 1
                arr(i) = _errCodeList(i).Code
            Next
            Return arr
        End Function

        Public Function GetAllCodeAndMsg() As String()
            Dim arr(_errCodeList.Count - 1) As String
            For i As Integer = 0 To _errCodeList.Count - 1
                arr(i) = $"{_errCodeList(i).Code}:{_errCodeList(i).Msg}"
            Next
            Return arr
        End Function
    End Class
End Namespace