Imports System.Linq
Imports System.Net
Imports System.Text.RegularExpressions

Public Class frmLogin

    ' TODO: 제공된 사용자 이름과 암호를 사용하여 사용자 지정 인증을 수행하는 코드를 삽입합니다
    ' (자세한 내용은 http://go.microsoft.com/fwlink/?LinkId=35339 참조).  
    ' 그러면 사용자 지정 보안 주체가 현재 스레드의 보안 주체에 다음과 같이 첨부될 수 있습니다. 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' 여기서 CustomPrincipal은 인증을 수행하는 데 사용되는 IPrincipal이 구현된 것입니다. 
    ' 나중에 My.User는 CustomPrincipal 개체에 캡슐화된 사용자 이름, 표시 이름 등의
    ' ID 정보를 반환합니다.
    Private Const LocalHostIP As String = "192.168.0.250"
    Private Const IntServerHubDNS As String = "jomt.iptime.org"
    Private Const Ext_802_HubDNS As String = "" ''"jomt802.iptime.org"

    Private IntServerHubIPaddr As IPHostEntry = Dns.GetHostEntry(IntServerHubDNS)
    Private Ext_802_HubIPaddr As IPHostEntry = Dns.GetHostEntry(Ext_802_HubDNS)

    Public IntServerHubIP As String = IntServerHubIPaddr.AddressList(0).ToString
    Public Ext_802_HubIP As String = Ext_802_HubIPaddr.AddressList(0).ToString
    Public Ext_DeviceIP As String

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Try
            UserInfo.UserID = ""
            UserInfo.UserPW = ""
            UserInfo.IDType = ""
            UserInfo.ExtPermition = ""

            StrCmd = "select UserPW,IDType,ExtPermition from jomtTesterUserIDTbl where UserID = '" & UsernameTextBox.Text & "';"
            If UserDBQueryReader(StrCmd) Then
                UserInfo.UserID = UsernameTextBox.Text
                UserInfo.UserPW = sqlDataQuery(0)
                UserInfo.IDType = sqlDataQuery(1)
                UserInfo.ExtPermition = sqlDataQuery(2)

                If PasswordTextBox.Text = UserInfo.UserPW Then
                    If GetLoginStatus() <= LOGIN_STATUS.CHECK_IN_FAIL Then
                        Select Case UserInfo.IDType
                            Case USER_ID_TYPE.ADMIN
                                GoTo loginSuccess
                            Case USER_ID_TYPE.TD_IQC
                                GoTo login_Not_Permition
                            Case USER_ID_TYPE.TD_WRITE
                                GoTo login_Not_Permition
                            Case USER_ID_TYPE.PRODUCT_AGING
                                GoTo login_Not_Permition
                            Case USER_ID_TYPE.PRODUCT_WRITE
                                RunMode = FORM_TYPE.WRITE_MODE
                                SetLoginStatus(LOGIN_STATUS.CHECK_IN_PROGRAM)
                                GoTo loginSuccess
                            Case USER_ID_TYPE.PRODUCT_READ
                                RunMode = FORM_TYPE.READ_MODE
                                SetLoginStatus(LOGIN_STATUS.CHECK_IN_PROGRAM)
                                GoTo loginSuccess
                            Case USER_ID_TYPE.QC_READ_WRITE
                                GoTo login_Not_Permition
                            Case USER_ID_TYPE.QC_SHIPMENT
                                GoTo login_Not_Permition
                            Case Else
                                GoTo loginFail
                        End Select

                    ElseIf GetLoginStatus() >= LOGIN_STATUS.ENTER_PROGRAM Then
                        If UserInfo.ExtPermition = USER_PERMITION_TYPE.MANAGER Then
                            SetLoginStatus(LOGIN_STATUS.CHECK_IN_SETUP)
                            GoTo loginSuccess
                        Else
                            GoTo login_Not_Permition
                        End If
                    End If
                End If
            End If
            GoTo loginFail

loginFail:
            PasswordTextBox.Text = ""
            SetLoginStatus(LOGIN_STATUS.CHECK_IN_FAIL)
            MsgBox("아이디 및 비밀번호를 확인하세요!", vbExclamation, MainForm.TITLE_NAME)
            Exit Sub
login_Not_Permition:
            PasswordTextBox.Text = ""
            SetLoginStatus(LOGIN_STATUS.CHECK_IN_SETUP_FAIL)
            MsgBox("프로그램 사용권한이 없습니다!", vbExclamation, MainForm.TITLE_NAME)
            Exit Sub
loginSuccess:

            PasswordTextBox.Text = ""
            Me.Close()

        Catch ex As Exception
            MsgBox("DUALSONIC Software Error: " + vbCrLf + CStr(Err.Number) + ", " + Err.Source + ", " + Err.Description, vbExclamation, MainForm.TITLE_NAME)
        End Try
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim TmpHubIP As String

        Try
            Me.Text = MainForm.PROGRAM_TITLE_NAME
            If MainForm.FirstLogin = True Then
                TmpHubIP = (New WebClient()).DownloadString("http://checkip.dyndns.org/")
                TmpHubIP = (New Regex("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")).Matches(TmpHubIP)(0).ToString()
                Ext_DeviceIP = TmpHubIP.ToString

                If Ext_DeviceIP = IntServerHubIP Or Ext_DeviceIP = Ext_802_HubIP Then
                    Exit Sub
                End If

                MsgBox("허용된 장소가 아니므로 프로그램을 종료합니다!", vbExclamation, MainForm.TITLE_NAME)
                Me.Close()
            End If
        Catch ex As Exception
            MsgBox("DUALSONIC Software Error: " + vbCrLf + CStr(Err.Number) + ", " + Err.Source + ", " + Err.Description, vbExclamation, MainForm.TITLE_NAME)
            Me.Close()
        End Try
    End Sub

End Class
