Imports IntraSell_DLL
Imports IntraSell_Net_Base

Public Class Setup

    Private CONN_STRING_TEMPLATE = "Server=#SERVER#;Port=#PORT#;User Id=#USERNAME#;password=#PASSWORD#;Persist Security Info=True;Database=#DATABASE#;charset=utf8;"

    Private Sub ButtonSetUpMySQL_Click(sender As Object, e As System.EventArgs) Handles ButtonSetUpMySQL.Click
        SetUpMySqlServer()
    End Sub

    Private Sub Setup_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        Try
            ParentBindingNavigator.Visible = False
            'Load from app.config 
            'conn string = server=localhost;User Id=root;password=a1060;Persist Security Info=True;database=intrasell_daten_2
            Dim connStr = My.MySettings.Default.intrasell_daten_2_ConnectionString
            TextBox1.Text = Extract(connStr, "server", "localhost")
            TextBox2.Text = Extract(connStr, "User Id")
            TextBox3.Text = Extract(connStr, "password")
            TextBox4.Text = Extract(connStr, "Port", "3306")
            TextBox5.Text = Extract(connStr, "database", "intrasell_daten_2")
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    Private Sub Form_Verlassen() Handles MyBase.Verlassen
        Me.Close()
    End Sub

    Private Sub ButtonConnect_Click(sender As Object, e As System.EventArgs) Handles ButtonConnect.Click
        Try

            'Build CString
            Dim cTest As String = CONN_STRING_TEMPLATE
            cTest = Replace(cTest, "#SERVER#", TextBox1.Text)
            cTest = Replace(cTest, "#USERNAME#", TextBox2.Text)
            cTest = Replace(cTest, "#PASSWORD#", TextBox3.Text)
            cTest = Replace(cTest, "#PORT#", TextBox4.Text)
            cTest = Replace(cTest, "#DATABASE#", TextBox5.Text)
            'CHECK CONN 
            Dim c As New MySql.Data.MySqlClient.MySqlConnection(cTest)
            Try
                c.Open()
                SetConnState(True)
            Catch ex As Exception
                SetConnState(False)
                MsgBox(ex.Message, vbCritical)
                Exit Sub
            End Try
            'SAVE
            AppConfigFileSettings.UpdateAppSettings("IntraSell_Net.My.MySettings.intrasell_daten_2_ConnectionString", cTest)
            My.Settings.Reload()
            BackupConfig()

            '
            Dim settings = New ClassSettings()
            'If IsNull(settings.Read("intrasell_daten_2_ConnectionString")) Then
            settings.Write("intrasell_daten_2_ConnectionString", cTest)
            'Else
            ' settings.Default.RegKassaConnectionString = settings.Read("intrasell_daten_2_ConnectionString")
            'End If
            My.Application.Me_Startup(Nothing, Nothing)
            ResetConnection(cTest, True)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    ''' <summary>
    ''' Return the value for the argument 
    ''' </summary>
    ''' <param name="connStr"></param>
    ''' <param name="paramName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Extract(connStr As String, paramName As String, Optional defaultWert As String = "") As String
        For Each s In Split(connStr, ";")
            If s.ToLower().Contains(paramName.ToLower()) Or s.ToLower.Equals(paramName.ToLower()) Then
                Dim res = s.Split("=")(1)
                If IsNull(res) Or res = "" Then
                    Return defaultWert
                Else
                    Return res
                End If

            End If
        Next
        Return defaultWert
    End Function

    Private Sub SetConnState(p1 As Boolean)
        TextBox1.ForeColor = IIf(p1, Color.Green, Color.Red)
        TextBox2.ForeColor = TextBox1.ForeColor
        TextBox3.ForeColor = TextBox1.ForeColor
        TextBox4.ForeColor = TextBox1.ForeColor
        TextBox5.ForeColor = TextBox1.ForeColor
    End Sub

End Class