Imports System.Configuration
Imports System.Text.RegularExpressions

Public Class FormSetting

    Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

    Private Sub FormSetting_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim AppSettings = ConfigurationManager.AppSettings
        TextBoxPLCIp.Text = AppSettings("PLC_IP")
        TextBoxPLCRack.Text = AppSettings("PLC_RACK")
        TextBoxPLCSlot.Text = AppSettings("PLC_SLOT")
        TextBoxDBComm.Text = AppSettings("DB_COMM")
        TextBoxLettQrIP.Text = AppSettings("CAMERA_IP")

        If AppSettings("RES_COUNT_ST_SHIFT") = "True" Then
            CheckBoxResCount.Checked = True
        Else
            CheckBoxResCount.Checked = False
        End If

        If AppSettings("LOG_REC") = "True" Then
            CheckBoxLogRec.Checked = True
        Else
            CheckBoxLogRec.Checked = False
        End If

    End Sub

    Private Sub TextBoxPLCIp_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBoxPLCIp.LostFocus
        Dim rx As New Regex("^(?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(?=\.?\d)\.)){4}$")
        If Not rx.IsMatch(TextBoxPLCIp.Text) Then
            MessageBox.Show("L'indirizzo IP non è del formato appropriato!")
            TextBoxPLCIp.Text = ""
        End If
    End Sub

    Private Sub TextBoxLettQrIP_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBoxLettQrIP.LostFocus
        Dim rx As New Regex("^(?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(?=\.?\d)\.)){4}$")
        If Not rx.IsMatch(TextBoxLettQrIP.Text) Then
            MessageBox.Show("L'indirizzo IP non è del formato appropriato!")
            TextBoxLettQrIP.Text = ""
        End If
    End Sub

    Private Sub TextBoxDBComm_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxDBComm.KeyPress
        ' Accept only Int number with lengh max 4 char
        Dim KeyAscii As Short = Asc(e.KeyChar)
        If KeyAscii < 48 And KeyAscii <> 24 And KeyAscii <> 8 Then
            KeyAscii = 0
        ElseIf KeyAscii > 57 Then
            KeyAscii = 0
        End If
        If e.KeyChar = "0" And TextBoxDBComm.TextLength = 0 Then
            KeyAscii = 0
        End If
        If TextBoxDBComm.TextLength > 3 And KeyAscii <> 8 Then
            KeyAscii = 0
        End If
        e.KeyChar = Chr(KeyAscii)
    End Sub

    Private Sub TextBoxPLCRack_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxPLCRack.KeyPress
        ' Accept only Int number with lengh max 1 char
        Dim KeyAscii As Short = Asc(e.KeyChar)
        If KeyAscii < 48 And KeyAscii <> 24 And KeyAscii <> 8 Then
            KeyAscii = 0
        ElseIf KeyAscii > 57 Then
            KeyAscii = 0
        End If
        If TextBoxPLCRack.TextLength > 0 And KeyAscii <> 8 Then
            KeyAscii = 0
        End If
        e.KeyChar = Chr(KeyAscii)
    End Sub

    Private Sub TextBoxPLCSlot_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxPLCSlot.KeyPress
        ' Accept only Int number with lengh max 1 char
        Dim KeyAscii As Short = Asc(e.KeyChar)
        If KeyAscii < 48 And KeyAscii <> 24 And KeyAscii <> 8 Then
            KeyAscii = 0
        ElseIf KeyAscii > 57 Then
            KeyAscii = 0
        End If
        If TextBoxPLCSlot.TextLength > 0 And KeyAscii <> 8 Then
            KeyAscii = 0
        End If
        e.KeyChar = Chr(KeyAscii)
    End Sub

    Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click

        If TextBoxPLCIp.Text = "" Then
            MsgBox("Il campo IP PLC è vuoto, correggere e riprovare")
            Exit Sub
        End If

        If TextBoxPLCRack.Text = "" Then
            MsgBox("Il campo Rack PLC è vuoto, correggere e riprovare")
            Exit Sub
        End If
        If TextBoxPLCSlot.Text = "" Then
            MsgBox("Il campo Slot PLC è vuoto, correggere e riprovare")
            Exit Sub
        End If
        If TextBoxDBComm.Text = "" Then
            MsgBox("Il campo DB Comm è vuoto, correggere e riprovare")
            Exit Sub
        End If

        If TextBoxLettQrIP.Text = "" Then
            MsgBox("Il campo IP Lettore QR è vuoto, correggere e riprovare")
            Exit Sub
        End If

        config.AppSettings.Settings("PLC_IP").Value = TextBoxPLCIp.Text
        config.AppSettings.Settings("PLC_RACK").Value = TextBoxPLCRack.Text
        config.AppSettings.Settings("PLC_SLOT").Value = TextBoxPLCSlot.Text
        config.AppSettings.Settings("DB_COMM").Value = TextBoxDBComm.Text
        config.AppSettings.Settings("CAMERA_IP").Value = TextBoxLettQrIP.Text

        If CheckBoxResCount.Checked = True Then
            config.AppSettings.Settings("RES_COUNT_ST_SHIFT").Value = "True"
        Else
            config.AppSettings.Settings("RES_COUNT_ST_SHIFT").Value = "False"
        End If

        If CheckBoxLogRec.Checked = True Then
            config.AppSettings.Settings("LOG_REC").Value = "True"
        Else
            config.AppSettings.Settings("LOG_REC").Value = "False"
        End If

        config.Save(ConfigurationSaveMode.Modified)
        ConfigurationManager.RefreshSection("appSettings")

        Select Case MsgBox("Configurazione Salvata, Riavviare L'Applicazione?", MsgBoxStyle.YesNoCancel, "Riavvia")
            Case MsgBoxResult.Yes
                Form1.CloseProcess()
                Application.Restart()
            Case MsgBoxResult.No
                Me.Close()
        End Select

    End Sub

End Class