Imports System.ComponentModel

Public Class FrmParametros
    Private frmMenuPrincipal As FrmMenuPrincipal

    Public Sub New(frmMenuPrincipal As FrmMenuPrincipal)
        InitializeComponent()
        Me.frmMenuPrincipal = frmMenuPrincipal
        AddHandler frmMenuPrincipal.Notificaciones_evento, AddressOf MostrarNotificacion
    End Sub

    Private Sub MostrarNotificacion(mensaje As String)
        Me.Invoke(Sub()
                      If RichTextBoxNotificaciones.Lines.Count > 1000 Then RichTextBoxNotificaciones.Clear()
                      RichTextBoxNotificaciones.AppendText(mensaje + vbNewLine)
                      RichTextBoxNotificaciones.ScrollToCaret()
                  End Sub)
    End Sub

    Private Sub BtnGuardar_Click(sender As Object, e As EventArgs) Handles BtnGuardar.Click
        My.Settings.ActualizarMaestra = chActivarDaemon.Checked

        My.Settings.ConexionLocalDataSource = txtCnxLocalDataSource.Text
        My.Settings.ConexionLocalInitialCatalog = txtCnxLocalInitialCatalog.Text
        My.Settings.ConexionLocalUser = txtCnxLocalUser.Text
        My.Settings.ConexionLocalPassword = txtCnxLocalPass.Text

        My.Settings.ConexionMaestraDataSource = txtCnxMaestraDataSource.Text
        My.Settings.ConexionMaestraInitialCatalog = txtCnxMaestraInitialCatalog.Text
        My.Settings.ConexionMaestraUsuario = txtCnxMaestraUser.Text
        My.Settings.ConexionMaestraPassword = txtCnxMaestraPass.Text
        My.Settings.TiempoEspera = NumericUpDownTiempoEspera.Value

        My.Settings.Save()

        conexionLocal = CrearCadenaConexion(txtCnxLocalDataSource.Text, txtCnxLocalInitialCatalog.Text, txtCnxLocalUser.Text, txtCnxLocalPass.Text)

        conexionMaestra = CrearCadenaConexion(txtCnxMaestraDataSource.Text, txtCnxMaestraInitialCatalog.Text, txtCnxMaestraUser.Text, txtCnxMaestraPass.Text)

        actualizarBdMaestra = chActivarDaemon.Checked

        tiempoEspera = NumericUpDownTiempoEspera.Value

        UpdateEnrollUsersData()

        MsgBox("Guardado exitoso.")

        If Not BtnCargarEmp.Visible Then
            If Conexion.ValidarConexion(conexionMaestra) Then
                BtnCargarEmp.Visible = True
            Else
                BtnCargarEmp.Visible = False
            End If
        End If

    End Sub

    Private Sub BtnCargarEmp_Click(sender As Object, e As EventArgs) Handles BtnCargarEmp.Click
        Dim frm0 As New FrmIngresoContrato(Me)
        For Each frm1 As Form In Application.OpenForms
            If frm1.Name = frm0.Name Then
                frm1.Show()
                frm1.Activate()
                Dim screenSize As Size = Screen.PrimaryScreen.WorkingArea.Size
                frm1.Location = New Point(screenSize.Width / 2 - frm1.Width / 2, screenSize.Height / 2 - frm1.Height / 2)
                Exit Sub
            End If
        Next

        frm0.Show()
        frm0.Activate()
    End Sub

    Private Sub FrmParametros_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        chActivarDaemon.Checked = My.Settings.ActualizarMaestra

        txtCnxLocalDataSource.Text = My.Settings.ConexionLocalDataSource
        txtCnxLocalInitialCatalog.Text = My.Settings.ConexionLocalInitialCatalog
        txtCnxLocalUser.Text = My.Settings.ConexionLocalUser
        txtCnxLocalPass.Text = My.Settings.ConexionLocalPassword

        txtCnxMaestraDataSource.Text = My.Settings.ConexionMaestraDataSource
        txtCnxMaestraInitialCatalog.Text = My.Settings.ConexionMaestraInitialCatalog
        txtCnxMaestraUser.Text = My.Settings.ConexionMaestraUsuario
        txtCnxMaestraPass.Text = My.Settings.ConexionMaestraPassword

        NumericUpDownTiempoEspera.Value = My.Settings.TiempoEspera

        If Conexion.ValidarConexion(conexionMaestra) Then
            BtnCargarEmp.Visible = True
        Else
            BtnCargarEmp.Visible = False
        End If
    End Sub

    Private Sub FrmParametros_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        RemoveHandler frmMenuPrincipal.Notificaciones_evento, AddressOf MostrarNotificacion
    End Sub

    Private Sub btnLimpiarLogs_Click(sender As Object, e As EventArgs) Handles btnLimpiarLogs.Click
        RichTextBoxNotificaciones.Clear()
    End Sub

    Private Sub btnCopyClipBoard_Click(sender As Object, e As EventArgs) Handles btnCopyClipBoard.Click
        Clipboard.SetText(RichTextBoxNotificaciones.Text)
    End Sub

End Class