Imports Microsoft.Win32.Registry

Public Class FrmRegistry

    ''' <summary>
    ''' 软件存放在注册表的根路径
    ''' </summary>
    Private ReadOnly _appConfig As String = $"software\AUTS"
    Private ReadOnly _dataServiceName As String = $"AUTS_DataService"
    Private ReadOnly _updateServiceName As String = $"AUTS_UpdateService"


    ''' <summary>
    ''' License可访问的公共路径
    ''' </summary>
    Private ReadOnly _licensePath As String = $"C:\AUTS\Licnese"


    ''' <summary>
    ''' 数据库文件夹路径
    ''' </summary>
    Private ReadOnly _localDbDirPath As String = $"C:\AUTS\localDb"


    ''' <summary>
    ''' 数据服务的路径
    ''' </summary>
    Private ReadOnly _dataServicePath As String = $"C:\AUTS\DataService"


    ''' <summary>
    ''' 更新服务的路径
    ''' </summary>
    Private ReadOnly _updateServicePath As String = $"C:\AUTS\UpdateService"


    ''' <summary>
    ''' 自定义选择文件夹
    ''' </summary>
    ''' <returns></returns>
    Private Function SelectFolderString() As String
        Dim txtString As String = String.Empty
        Using folder As New FolderBrowserDialog
            If folder.ShowDialog() = DialogResult.OK Then
                txtString = folder.SelectedPath
            End If
        End Using
        Return txtString
    End Function

    ''' <summary>
    ''' 自定义选择文件
    ''' </summary>
    ''' <returns></returns>
    Private Function SelectFileString() As String
        Dim txtString As String = String.Empty
        Using folder As New OpenFileDialog
            If folder.ShowDialog() = DialogResult.OK Then
                txtString = folder.FileName
            End If
        End Using
        Return txtString
    End Function


    ''' <summary>
    ''' License自定义文件夹路径
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub BtnLicense_Click(sender As Object, e As EventArgs) Handles BtnLicense.Click
        TBoLicnese.Text = SelectFileString()
    End Sub


    ''' <summary>
    ''' LocalDb自定义文件夹路径
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub BtnLocalDb_Click(sender As Object, e As EventArgs) Handles BtnLocalDb.Click
        TBoLocalDb.Text = SelectFileString()
    End Sub


    ''' <summary>
    ''' DataServicePath自定义文件夹路径
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub BtnDataSerPath_Click(sender As Object, e As EventArgs) Handles BtnDataSerPath.Click
        TBoDataSerPath.Text = SelectFolderString()
    End Sub


    ''' <summary>
    ''' UpDateServicePath自定义文件夹路径
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub BtnUpDateSerPath_Click(sender As Object, e As EventArgs) Handles BtnUpDateSerPath.Click
        TBoUpDateSerPath.Text = SelectFolderString()
    End Sub

    Private Sub FrmRegistry_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Registry.RootDirExists() = False Then
            MsgBox("AUTS RootDir Not Exists !")
            Return
        End If

        '获取本地注册表设定值
        Dim licensePath As String = Registry.LicensePath
        Dim localdbPath As String = Registry.LocalDb


        Dim dsName As String = Registry.DataServiceName
        Dim dsVer As String = Registry.DataServiceVersion
        Dim dsDir As String = $"{Registry.DataServiceDirPath}\{dsVer}"
        Dim dsFile As String = Registry.DataServiceExeName

        Dim usName As String = Registry.UpdateServiceName
        Dim usVer As String = Registry.UpdateServiceVersion
        Dim usDir As String = $"{Registry.UpdateServiceDirPath}\{usVer}"
        Dim usFile As String = Registry.UpdateServiceExeName


        TBoLicnese.Text = licensePath
        TBoLocalDb.Text = localdbPath
        TBoDataServiceVersion.Text = dsDir
        TBoUpdateServiceVersion.Text = usDir
        TBoDataSerPath.Text = dsDir
        TBoUpDateSerPath.Text = usDir

        Console.WriteLine($"dsName:{dsName},dsFile:{dsFile},usName:{usName},usFile:{usFile}!")
    End Sub


    ''' <summary>
    ''' 应用键值
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub BtnSaveTheKeyValue_Click(sender As Object, e As EventArgs) Handles BtnSaveTheKeyValue.Click
        '设置键值
        Dim valLicense As String = TBoLicnese.Text
        Dim valLocalDb As String = TBoLocalDb.Text
        Dim valDSVer As String = TBoDataServiceVersion.Text
        Dim valUSVer As String = TBoUpdateServiceVersion.Text
        Dim valDataSer As String = TBoDataSerPath.Text
        Dim valUpdateSer As String = TBoUpDateSerPath.Text

        '创建项
        Registry.LicensePath = valLicense

        Registry.LocalDb = valLocalDb

        Registry.DataServiceName = _dataServiceName
        Registry.DataServiceVersion = valDSVer
        Registry.DataServiceDirPath = valDataSer
        Registry.DataServiceExeName = $"{_dataServiceName}.exe"

        Registry.UpdateServiceName = _updateServiceName
        Registry.UpdateServiceVersion = valUSVer
        Registry.UpdateServiceDirPath = valUpdateSer
        Registry.UpdateServiceExeName = $"{_updateServiceName}.exe"


        If MsgBox($"Save Settings Success!Do you want to close the settings form?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            DialogResult = DialogResult.Yes
        End If
    End Sub



    ''' <summary>
    ''' 默认值按键
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub BtnDefault_Click(sender As Object, e As EventArgs) Handles BtnDefault.Click
        '设置键值
        TBoLicnese.Text = _licensePath
        TBoLocalDb.Text = _localDbDirPath
        TBoDataSerPath.Text = _dataServicePath
        TBoUpDateSerPath.Text = _updateServicePath

    End Sub


    ''' <summary>
    ''' 关闭注册表窗体
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub BtnCloseFrmRegistry_Click(sender As Object, e As EventArgs) Handles BtnCloseFrmRegistry.Click
        DialogResult = DialogResult.No
    End Sub
End Class
