Imports System.Data.Common
Imports UTS_Core.Database.DbExecutor

Public Class FrmTasksDbType
    ''' <summary>存Json数据的键值对</summary>
    Public ConnectionParam As DbConnectionStringBuilder

    ''' <summary>
    ''' 当前需要生成连接的数据库类型
    ''' </summary>
    ''' <returns></returns>
    Public Property DbType() As DbTypeEnum = DbTypeEnum.Sqlite


    ''' <summary>
    ''' 窗体启动
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub FrmTasksDbType_Load(sender As Object, e As EventArgs) Handles Me.Load
        ConnectionParam = New DbConnectionStringBuilder

        InitTabPages()

        CboDbTypeName.DropDownStyle = ComboBoxStyle.DropDownList
        CboDbTypeName.Items.Clear()
        CboDbTypeName.Items.Add(DbTypeEnum.Mysql.ToString())
        CboDbTypeName.Items.Add(DbTypeEnum.Sqlite.ToString())

        If CboDbTypeName.Items.Count > DbType Then
            CboDbTypeName.SelectedIndex = DbType
        End If
    End Sub


    ''' <summary>
    ''' 隐藏Page表头
    ''' </summary>
    Private Sub InitTabPages()
        TabDbTasks.SizeMode = TabSizeMode.Fixed '隐藏TabControl的表头
        TabDbTasks.ItemSize = New Size(0, 1)
    End Sub


    ''' <summary>
    ''' 类型变化(SelectIndex),改变页面
    ''' 如无必要，不要使用文本改变(TextChanged)
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub CboDbTypeName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboDbTypeName.SelectedIndexChanged
        TabDbTasks.SelectedIndex = CboDbTypeName.SelectedIndex

    End Sub


    ''' <summary>
    ''' 打开文件，辅助输入
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub BtnOpenFile_Click(sender As Object, e As EventArgs) Handles BtnOpenFile.Click
        Using fileDialog As New OpenFileDialog
            fileDialog.InitialDirectory = Application.StartupPath
            If fileDialog.ShowDialog() = DialogResult.OK Then
                TboDataSourseName.Text = fileDialog.FileName
            End If
        End Using
    End Sub


    ''' <summary>
    ''' OK按键
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Select Case CboDbTypeName.SelectedIndex
            Case DbTypeEnum.Sqlite 'sqlite
                ConnectionParam.Clear()
                ConnectionParam.Add("Data Source", TboDataSourseName.Text)
                ConnectionParam.Add("Password", TboPassWord.Text)
            Case DbTypeEnum.Mysql 'mysql
                ConnectionParam.Clear()
                ConnectionParam.Add("Server", TboDataSourseName.Text)
                ConnectionParam.Add("Port", TboPassWord.Text)
                ConnectionParam.Add("User id", TboPassWord.Text)
                ConnectionParam.Add("Password", TboPassWord.Text)
              
                ConnectionParam.Add("Database", TboPassWord.Text)
            Case Else
                Throw New Exception($"Unknown DbType!")
        End Select

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub


End Class
