Imports System.Text

Namespace DebugLog
    Public Class DebugPrintClass
        Enum DebugPrintType
            System
            Comport
            Database
            NetWork
            Ftp
        End Enum

        ''' <summary>
        ''' 显示所有数据,优先度最高
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property ShowAllData() As Boolean = True

        ''' <summary>
        ''' 是否显示系统信息
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property ShowSystemData() As Byte = 1

        ''' <summary>
        ''' 是否显示串口信息
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property ShowComportData() As Byte = 1

        ''' <summary>
        ''' 是否显示数据库信息
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property ShowDatabaseData() As Byte = 1

        ''' <summary>
        ''' 是否显示网络信息
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property ShowNetWorkData() As Byte = 1

        ''' <summary>
        ''' 是否显示Ftp信息
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property ShowFtpData() As Byte = 1

        ''' <summary>
        ''' 显示信息的集合,对应数据位为1则打印，为0不打印
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property ShowDataType() As Integer = ShowSystemData >> DebugPrintType.System Or
                                                           ShowComportData >> DebugPrintType.Comport Or
                                                           ShowDatabaseData >> DebugPrintType.Database Or
                                                           ShowNetWorkData >> DebugPrintType.NetWork Or
                                                           ShowFtpData >> DebugPrintType.Ftp


        ''' <summary>
        ''' 是否添加时间前缀
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property ShowTime() As Boolean = True

        ''' <summary>
        ''' 是否显示与上包显示的间隔
        ''' </summary>
        ''' <returns></returns>
        Public Shared Property ShowTimeSpan() As Boolean = True


        Private Shared _lastShowTime As DateTime = Now

        Private Shared _timeSpan As TimeSpan

        ''' <summary>
        ''' 打印调试信息
        ''' </summary>
        ''' <param name="printString">需要打印的信息</param>
        Private Shared Sub DebugPrint(printString As String)
            Console.WriteLine(printString)
        End Sub


        ''' <summary>
        ''' 打印调试信息
        ''' </summary>
        ''' <param name="type">打印信息类型</param>
        ''' <param name="printString">需要打印的信息</param>
        Public Shared Sub DebugPrint(type As DebugPrintType, printString As String)
            If ShowAllData = False Then Return
            If ((ShowDataType >> type) And 1) = 0 Then Return

            _timeSpan = Now - _lastShowTime
            _lastShowTime = Now

            Dim msgBuilder As New StringBuilder
            If ShowTime Then msgBuilder.Append($"{Now:yyyy-MM-dd HH:mm:ss} - ")
            If ShowTimeSpan Then msgBuilder.Append($"{_timeSpan.TotalMilliseconds,8} - ")
            msgBuilder.Append($"{type} - ")
            msgBuilder.Append($"{printString}")

            DebugPrint(msgBuilder.ToString())
        End Sub


        ''' <summary>
        '''  打印调试信息
        ''' </summary>
        ''' <param name="type">打印信息类型</param>
        ''' <param name="tip">需要打印信息的提示前缀</param>
        ''' <param name="printString">需要打印的信息</param>
        Public Shared Sub DebugPrint(type As DebugPrintType, tip As String, printString As String)
            DebugPrint(type, $"{tip}:{printString}")
        End Sub

    End Class
End Namespace