Imports System.Text
Imports UTS_Core.Database.DbExecutor
Imports UTS_Core.UTSModule.Service

Public Class FrmSyncTasks
    Implements ITaskForm

    Public Property DbType() As DbTypeEnum = DbTypeEnum.Sqlite

    ''' <summary>
    ''' 显示窗体
    ''' </summary>
    ''' <param name="parentControl">把本窗体打包放在其他容器内</param>
    Public Sub ShowForm(parentControl As Control) Implements ITaskForm.ShowForm
        FormBorderStyle = FormBorderStyle.None
        TopLevel = False
        Dock = DockStyle.Fill
        Parent = parentControl

        Show()
    End Sub

    Private Sub FrmSyncTasks_Load(sender As Object, e As EventArgs) Handles Me.Load
        TBoTasksType.Text = ServiceTask.ServiceTaskTypeEnum.DbSync.ToString()

        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>
    ''' 根据类型不同，快速生成ConnJsonString
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub BtnRapidGenerating_Click(sender As Object, e As EventArgs) Handles BtnRapidGenerating.Click
        TBoConnStringSync.Text = RapidGenerating()

    End Sub

    ''' <summary>
    ''' 快捷生成数据库连接字符串
    ''' </summary>
    ''' <returns></returns>
    Private Function RapidGenerating() As String
        Dim connStr As New StringBuilder
        FrmTasksDbType.DbType = DbType
        If FrmTasksDbType.ShowDialog = DialogResult.OK Then
            connStr.Append(FrmTasksDbType.ConnectionParam.ToString())
            connStr = connStr.Replace("\", "/")
        End If

        Return UTS_Core.Security.Aes128.EncryptStr(connStr.ToString, UTS_Core.Security.Aes128.ServerAesKey)
    End Function


    Public Sub SetParam(params As Dictionary(Of String, String)) Implements ITaskForm.SetParam
        For Each param As KeyValuePair(Of String, String) In params
            Select Case param.Key
                Case "Name"
                    TBoTasksName.Text = param.Value
                Case "Interval"
                    NudTasksInterval.Value = CInt(param.Value)
                Case "LocalDbType"
                    CboDbTypeName.SelectedIndex = CInt([Enum].Parse(GetType(DbTypeEnum), param.Value))
                Case "LocalConnString"
                    TBoConnStringSync.Text = param.Value
            End Select
        Next

    End Sub

    Public Function GetParams() As Dictionary(Of String, String) Implements ITaskForm.GetParams
        Dim jsonParam As New Dictionary(Of String, String)
        jsonParam.Clear()
        jsonParam.Add("Type", TBoTasksType.Text)
        jsonParam.Add("Name", TBoTasksName.Text)
        jsonParam.Add("Interval", NudTasksInterval.Value.ToString)
        jsonParam.Add("LocalDbType", CboDbTypeName.Text)
        jsonParam.Add("LocalConnString", TBoConnStringSync.Text)
        Return jsonParam
    End Function

    Private Sub CboDbTypeName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboDbTypeName.SelectedIndexChanged
        DbType = CType([Enum].Parse(GetType(DbTypeEnum), CboDbTypeName.SelectedIndex.ToString()), DbTypeEnum)
        TBoConnStringSync.Text = String.Empty
    End Sub


End Class