Imports System.IO
Imports UTS_Core.Security

Namespace UTSModule.License

    ''' <summary>
    ''' 类的作用:获取服务器信息
    ''' 创建人员:ML
    ''' Log编号:1
    ''' 修改描述:创建
    ''' 修改日期:2019-10-15
    ''' 修改人员:ML
    ''' </summary>
    Public Class Server

        ''' <summary> Server文件字段枚举 </summary>
        Private Enum ServerKeyEnum

            ''' <summary>客户名</summary>
            VendorName

            ''' <summary>数据库服务器地址</summary>
            DataSource

            ''' <summary>数据库服务器端口</summary>
            DbPort

            ''' <summary>数据库名</summary>
            Database

            ''' <summary>数据库用户名</summary>
            DbUser

            ''' <summary>数据库用户密码</summary>
            DbPassword

            ''' <summary>Ftp地址</summary>
            FtpHost

            ''' <summary>Ftp端口</summary>
            FtpPort

            ''' <summary>Ftp用户名</summary>
            FtpUser

            ''' <summary>Ftp用户密码</summary>
            FtpPassword

        End Enum

        ''' <summary> Server文件校验返回枚举值</summary>
        Public Enum ServerCheckCodeEnum

            ''' <summary>未找到Server</summary>
            NoServer

            ''' <summary>无法打开Server</summary>
            CantOpenServer

            ''' <summary>无效的Server</summary>
            InvalidServer

            ''' <summary>校验通过</summary>
            CheckPass

        End Enum

        ''' <summary>存储Server校验返回值</summary>
        Private _checkCode As ServerCheckCodeEnum

        ''' <summary>存储Server所有信息</summary>
        Private _server As Dictionary(Of String, String)

        ''' <summary>服务器密钥</summary>
        Private ReadOnly _serverAesKey As String = "^5GHo96tr5ff&*Hphg7665^fyu&uOhj0j[0[(jOIhI&g77FgghO*hhHY8h(*H&-987OygO8yg*yS$&G&aG9*&6g96*&7^RA65s76r*&&G(*(7(Gyg7#7Ff7F*&(*&&^"

        '''<summary>Server所有可用字段</summary>
        Private ReadOnly _serverKey() As String = {"VendorName", "DataSource", "DataPort", "Database", "DbUser", "DbPassword", "FtpHost", "FtpPort", "FtpUser", "FtpPassword"}

        ''' <summary>
        ''' 获取客户名
        ''' </summary>
        ''' <returns>客户名</returns>
        Public ReadOnly Property VendorName() As String
            Get
                Return _server.Item(_serverKey(ServerKeyEnum.VendorName))
            End Get
        End Property

        ''' <summary>
        ''' 获取服务器名
        ''' </summary>
        ''' <returns>服务器名</returns>
        Public ReadOnly Property DataSource() As String
            Get
                Return _server.Item(_serverKey(ServerKeyEnum.DataSource))
            End Get
        End Property

        ''' <summary>
        ''' 获取服务器端口
        ''' </summary>
        ''' <returns>服务器端口</returns>
        Public ReadOnly Property DatabasePort() As String
            Get
                Return _server.Item(_serverKey(ServerKeyEnum.DbPort))
            End Get
        End Property

        ''' <summary>
        ''' 获取数据库名
        ''' </summary>
        ''' <returns>数据库名</returns>
        Public ReadOnly Property Database() As String
            Get
                Return _server.Item(_serverKey(ServerKeyEnum.Database))
            End Get
        End Property

        ''' <summary>
        ''' 获取数据库用户名
        ''' </summary>
        ''' <returns>数据库用户名</returns>
        Public ReadOnly Property DatabaseUser() As String
            Get
                Return _server.Item(_serverKey(ServerKeyEnum.DbUser))
            End Get
        End Property

        ''' <summary>
        ''' 获取数据库用户密码
        ''' </summary>
        ''' <returns>数据库用户密码</returns>
        Public ReadOnly Property DatabasePassword() As String
            Get
                Return _server.Item(_serverKey(ServerKeyEnum.DbPassword))
            End Get
        End Property

        ''' <summary>
        ''' 获取服务器端口
        ''' </summary>
        ''' <returns>服务器端口</returns>
        Public ReadOnly Property FtpPort() As String
            Get
                Return _server.Item(_serverKey(ServerKeyEnum.FtpPort))
            End Get
        End Property

        ''' <summary>
        ''' 获取数据库名
        ''' </summary>
        ''' <returns>数据库名</returns>
        Public ReadOnly Property FtpHost() As String
            Get
                Return _server.Item(_serverKey(ServerKeyEnum.FtpHost))
            End Get
        End Property

        ''' <summary>
        ''' 获取数据库用户名
        ''' </summary>
        ''' <returns>数据库用户名</returns>
        Public ReadOnly Property FtpUser() As String
            Get
                Return _server.Item(_serverKey(ServerKeyEnum.FtpUser))
            End Get
        End Property

        ''' <summary>
        ''' 获取数据库用户密码
        ''' </summary>
        ''' <returns>数据库用户密码</returns>
        Public ReadOnly Property FtpPassword() As String
            Get
                Return _server.Item(_serverKey(ServerKeyEnum.FtpPassword))
            End Get
        End Property

        ''' <summary>
        ''' 读取Server文件所有内容，并进行解密,将解密后的内容存入Server字典中
        ''' </summary>
        ''' <param name="serverPath">Server文件完整路径(含文件名)</param>
        ''' <returns>读取Server文件的状态</returns>
        Public Function ReadServerFile(ByVal serverPath As String) As ServerCheckCodeEnum
            Dim tmp As String
            Dim fm As FileStream
            Dim sr As StreamReader
            If File.Exists(serverPath) Then
                fm = New FileStream(serverPath, FileMode.Open)
                sr = New StreamReader(fm)
                _checkCode = ServerCheckCodeEnum.CheckPass
                _server = New Dictionary(Of String, String)
                Try
                    tmp = Aes128.DecryptStr(sr.ReadToEnd, _serverAesKey) 'utf8解密字符串
                    ' tmp = ClsAes128.DecryptStr(sr.ReadToEnd, _serverAesKey, Unicode) 'Unicode解密字符串
                    Dim lines() As String = tmp.Split(Chr(13))‘回车切割
                    For i As Integer = 0 To lines.Length - 1
                        tmp = lines(i).Trim(Chr(10))'去除换行
                        Dim str As String() = tmp.Split("="c)
                        If str.Length = 2 Then
                            _server.Add(Trim(str(0)), Trim(str(1)))
                            Debug.Print($"Keys : {_server.Keys.ElementAt(_server.Count - 1)} Value : {_server.Values.ElementAt(_server.Count - 1)}")
                        Else
                            _checkCode = ServerCheckCodeEnum.InvalidServer
                        End If
                    Next
                Catch ex As Exception
                    _checkCode = ServerCheckCodeEnum.InvalidServer
                Finally
                    fm.Dispose()
                    sr.Close()
                End Try
            Else
                _checkCode = ServerCheckCodeEnum.NoServer
            End If
            Return _checkCode
        End Function

        ''' <summary>
        ''' 校验Server
        ''' </summary>
        ''' <param name="serverPath">Server文件完整路径(含文件名)</param>
        ''' <returns>校验是否通过</returns>
        Public Function CheckoutServer(ByVal serverPath As String) As ServerCheckCodeEnum
            If ReadServerFile(serverPath) = ServerCheckCodeEnum.CheckPass Then
                '校验厂商(根据是否存在厂商数据库校验)
                If My.Computer.Network.Ping(_server.Item(_serverKey(ServerKeyEnum.DataSource))) Then

                End If

            End If
            Return _checkCode
        End Function

    End Class
End Namespace