Imports System.Text.RegularExpressions

Module MdlVariables

    Sub New()
        Dim LocalDataSource = If(IsNothing(My.Settings.ConexionLocalDataSource), "", My.Settings.ConexionLocalDataSource)
        Dim LocalInitialCatalog = If(IsNothing(My.Settings.ConexionLocalInitialCatalog), "", My.Settings.ConexionLocalInitialCatalog)
        Dim LocalUser = If(IsNothing(My.Settings.ConexionLocalUser), "", My.Settings.ConexionLocalUser)
        Dim LocalPass = If(IsNothing(My.Settings.ConexionLocalPassword), "", My.Settings.ConexionLocalPassword)

        Dim MaestraDataSource = If(IsNothing(My.Settings.ConexionMaestraDataSource), "", My.Settings.ConexionMaestraDataSource)
        Dim MaestraInitialCatalog = If(IsNothing(My.Settings.ConexionMaestraInitialCatalog), "", My.Settings.ConexionMaestraInitialCatalog)
        Dim MaestraUser = If(IsNothing(My.Settings.ConexionMaestraUsuario), "", My.Settings.ConexionMaestraUsuario)
        Dim MaestraPass = If(IsNothing(My.Settings.ConexionMaestraPassword), "", My.Settings.ConexionMaestraPassword)

        tiempoEspera = If(IsNothing(My.Settings.TiempoEspera), 10000, My.Settings.TiempoEspera)

        conexionLocal = CrearCadenaConexion(LocalDataSource, LocalInitialCatalog, LocalUser, LocalPass)

        conexionMaestra = CrearCadenaConexion(MaestraDataSource, MaestraInitialCatalog, MaestraUser, MaestraPass)

        actualizarBdMaestra = If(IsNothing(My.Settings.ActualizarMaestra), False, My.Settings.ActualizarMaestra)
    End Sub


    Public conexionLocal As String = "Data Source = desarrollo.prixus.cl,49172 ;Initial Catalog= relojOnline;Persist Security Info=True;User ID= sa; Password= somosprixus.2410;Connect Timeout=40;Asynchronous Processing=true;Asynchronous Processing=true"

    Public conexionMaestra As String = "Data Source = desarrollo.prixus.cl,49172 ;Initial Catalog= PRIXUS_76377910;Persist Security Info=True;User ID= sa; Password= somosprixus.2410;Connect Timeout=40;Asynchronous Processing=true;Asynchronous Processing=true"

    Public actualizarBdMaestra As Boolean = False

    Public tiempoEspera As Integer = 10000

    Public idContrato As Integer

    Public mes As Integer

    Public periodo As Integer


#Region "RELOJ CONTROL"

    ''' Reloj Control:
    Private _EnrollUsersData As New List(Of DeviceUserData)

    Public Property EnrollUsersData As List(Of DeviceUserData)
        Get
            If IsNothing(_EnrollUsersData) Then _EnrollUsersData = New List(Of DeviceUserData)
            Return _EnrollUsersData
        End Get
        Set(value As List(Of DeviceUserData))
            _EnrollUsersData = value
        End Set
    End Property

    Public Event OnChange()

    Public Sub UpdateEnrollUsersData()
        If Conexion.ValidarConexion(conexionLocal) Then
            Dim conexion As New Conexion(conexionLocal)
            Dim usuariosEnrolados As New ClsUsuariosEnroladosListar(conexion)
            EnrollUsersData = usuariosEnrolados.Listar()
            RaiseEvent OnChange()
        Else
            MessageBox.Show("Error de conexión con BD local. Verifique cadena de conexión", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Public Function GetUserDeviceData(ByVal idUsuario) As DeviceUserData
        If (idUsuario < 1) Then Throw New Exception("El identificador del usuario no puede ser cero.")
        Dim result As DeviceUserData = EnrollUsersData.FirstOrDefault(Function(value) value.IdUser = idUsuario)
        If IsNothing(result) Then result = New DeviceUserData : result.IdUser = idUsuario
        Return result
    End Function

    Public Function ObtenerNombreDedo(ByVal numero As Integer)
        Dim resultado As String = ""

        Select Case numero
            Case 10
                resultado = "Meñique izquierdo"
            Case 9
                resultado = "Anular izquierdo"
            Case 8
                resultado = "Medio izquierdo"
            Case 7
                resultado = "Índice izquierdo"
            Case 6
                resultado = "Pulgar izquierdo"
            Case 5
                resultado = "Meñique derecho"
            Case 4
                resultado = "Anular derecho"
            Case 3
                resultado = "Medio derecho"
            Case 2
                resultado = "Índice derecho"
            Case 1
                resultado = "Pulgar derecho"
            Case Else
                resultado = "Desconocido"
        End Select

        Return resultado
    End Function


#End Region

#Region "UTILES"
    Public Function PreguntaSimple_Msgbox(Optional ByVal mensaje As String = " ¿ Está Seguro ? ", Optional ByVal titulo As String = "Confirmación") As Integer
        Dim resp = MessageBox.Show(mensaje, titulo, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2)
        Return resp
    End Function


    Public Function validar_Mail(ByVal sMail As String) As Boolean
        ' retorna true o false
        Return Regex.IsMatch(sMail, "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$")
    End Function

    Public Function CrearCadenaConexion(dataSource As String, initialCatalog As String, user As String, password As String) As String
        Dim resultado = ""
        resultado = $"Data Source= {dataSource};Min Pool Size=0;Max Pool Size=10024;Pooling=true;Initial Catalog={initialCatalog};Persist Security Info=False;User ID={user};Password={password};Connect Timeout=40"
        Return resultado
    End Function
#End Region

End Module