Imports System.Deployment.Application
Imports System.IO
Imports System.Reflection
Imports iMESExceptionManager

Module modTest

    Public Const defInteger As Integer = -999   '宣告整數使用的常數，用以辨識參數是否有傳入Function中。
    Public Const defString As String = "Null"   '宣告字串使用的常數，用以辨識參數是否有傳入Function中。
    Public Const defDateTime As Date = #12/31/1900#  '宣告日期使用的常數，用以辨識參數是否有傳入Function中。
    Public Const defSingle As Single = -999  '宣告單精準數使用的常數，用以辨識參數是否有傳入Function中。
    Public Const defDouble As Double = -999  '宣告雙精準數使用的常數，用以辨識參數是否有傳入Function中。
    Public Const strAddTagLabel As String = "extrabase"  'Add by py for Combine Addition XML doc
    Public Const strAddTagName As String = "ExtraBase"    'Add by py for Combine Addition XML doc
    Public Const defWinErrCode As String = "0000-899999"
    Public Const defNullDate As Date = #12/31/1910#  '宣告日期使用的常數，用以辨識參數是否要變更為Null。
    Public Const defNullInteger As Integer = -9999   '宣告數值使用的常數，用以辨識參數是否要變更為Null。

    Sub Main()
        Dim WinFormInfo As System.Windows.Forms.SystemInformation

        '*****************************************************************************************************************        

        Dim ArgumentStr() As String = Split(Microsoft.VisualBasic.Command()) '取得傳給此執行檔的參數(應用程式路徑,UserID,Password)
        Dim i As Integer
        Dim App_Path As String = ""

        'Modify By Peter 2005/06/21 Add /WSH
        For i = LBound(ArgumentStr) To UBound(ArgumentStr)
            If InStr(UCase(ArgumentStr(i)), "/DIR:") > 0 Then
                App_Path = Mid(Trim(ArgumentStr(i)), 6, Trim(ArgumentStr(i)).Length)
            ElseIf InStr(UCase(ArgumentStr(i)), "/UID:") > 0 Then
                gUserNo = Mid(Trim(ArgumentStr(i)), 6, Trim(ArgumentStr(i)).Length)
            ElseIf InStr(UCase(ArgumentStr(i)), "/UNA:") > 0 Then
                gUserName = Mid(Trim(ArgumentStr(i)), 6, Trim(ArgumentStr(i)).Length)
            ElseIf InStr(UCase(ArgumentStr(i)), "/LVL:") > 0 Then
                gUserLevel = Mid(Trim(ArgumentStr(i)), 6, Trim(ArgumentStr(i)).Length)
            ElseIf InStr(UCase(ArgumentStr(i)), "/LAN:") > 0 Then
                gLanguageMode = Mid(Trim(ArgumentStr(i)), 6, Trim(ArgumentStr(i)).Length)
            ElseIf InStr(UCase(ArgumentStr(i)), "/WSH:") > 0 Then
                gMESWebServiceHost = Mid(Trim(ArgumentStr(i)), 6, Trim(ArgumentStr(i)).Length)
            ElseIf InStr(UCase(ArgumentStr(i)), "/CSH:") > 0 Then
                gCUSWebServiceHost = Mid(Trim(ArgumentStr(i)), 6, Trim(ArgumentStr(i)).Length)
            ElseIf InStr(UCase(ArgumentStr(i)), "/ENV:") > 0 Then
                gSettingMode = Mid(Trim(ArgumentStr(i)), 6, Trim(ArgumentStr(i)).Length)
            ElseIf InStr(UCase(ArgumentStr(i)), "/SSH:") > 0 Then
                gSFCWebServiceHost = Mid(Trim(ArgumentStr(i)), 6, Trim(ArgumentStr(i)).Length)
            ElseIf InStr(UCase(ArgumentStr(i)), "/ESH:") > 0 Then
                gERPWebServiceHost = Mid(Trim(ArgumentStr(i)), 6, Trim(ArgumentStr(i)).Length)
            ElseIf InStr(UCase(ArgumentStr(i)), "/SSL:") > 0 Then
                gEnableSSL = IIf(Mid(Trim(ArgumentStr(i)), 6, Trim(ArgumentStr(i)).Length).ToUpper = "TRUE", True, False)
            End If
        Next

        '*****************************************************************************************************************
        If App_Path = "" Then
            gAppPath = Application.StartupPath
        Else
            gAppPath = App_Path
        End If

        If gUserNo = "" And gUserName = "" Then

            Using obj As New frmCOMChkPassword
                obj.ShowDialog()
            End Using

        Else
            gReturnKeyValue = gUserNo
        End If

        '用來做為是否可以進入系統的判斷
        If gReturnKeyValue <> "" Then
            '取得電腦名稱
            gComputerName = WinFormInfo.ComputerName

            iMESExceptionManager.ExceptionManager.LanguageMode = gLanguageMode

            ' 2014/12/26 YF, Coding時套用Pro5 Style
            Infragistics.Win.AppStyling.StyleManager.Load(New MemoryStream(Global.Test.My.Resources.Resources.iMES))
            gEnableStyle = True

            '進入之系統
            Dim fMain As New MDI_Panel
            fMain.ShowDialog()

        End If

    End Sub

    Public Function LoadAssembly(ByVal pFileName As String) As Assembly

        Dim mainAssembly As System.Reflection.Assembly
        Dim myfieldinfo() As System.Reflection.FieldInfo
        Dim strAppBase As String = AppDomain.CurrentDomain.BaseDirectory
        Dim strFilePath As String
        Dim strNamespace As String

        Try

            '判斷是否使用ClickOnce
            If ApplicationDeployment.IsNetworkDeployed Then
                'ClickOnce架構下將strAppBase改指回Smart Client
                Dim tempUri As System.Uri = ApplicationDeployment.CurrentDeployment.UpdateLocation
                strAppBase = tempUri.ToString.Replace("/ClickOnce/MESSeries.application", "")
            End If

            strFilePath = strAppBase & "/" & pFileName

            '檢查檔案是否存在
            If (Not funCheckFileExist(strFilePath)) Then
                Throw New iMESException.MESException("0000-202115", strFilePath)
            End If

            mainAssembly = Assembly.LoadFrom(strFilePath)

            'strNamespace = mainAssembly.GetExportedTypes(0).Namespace

            '設定值予Module Public變數
            'myfieldinfo = mainAssembly.GetType(strNamespace & ".modWIN").GetFields()

            'For i As Integer = 0 To myfieldinfo.Length - 1
            '    If myfieldinfo(i).Name.ToUpper = "gUserNo".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gUserNo)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gUserName".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gUserName)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gComputerName".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gComputerName)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gUserLevel".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gUserLevel)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gLanguageMode".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gLanguageMode)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gAppPath".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gAppPath)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gMESWebServiceHost".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gMESWebServiceHost)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gCUSWebServiceHost".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gCUSWebServiceHost)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gSFCWebServiceHost".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gSFCWebServiceHost)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gERPWebServiceHost".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gERPWebServiceHost)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gSettingMode".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gSettingMode)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gEnterpriseNo".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gEnterpriseNo)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gEnableSSL".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gEnableSSL)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gConfigFile".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gConfigFile)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gEnableStyle".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), True)
            '    ElseIf myfieldinfo(i).Name.ToUpper = "gSYSmode".ToUpper Then
            '        myfieldinfo(i).SetValue(myfieldinfo(i), gSYSmode)
            '    End If
            'Next

        Catch ex As Exception
            mainAssembly = Nothing
            ExceptionManager.iMESShowDialog(defWinErrCode, "Load Assembly Fail!" & vbCrLf & ex.Message, ex.StackTrace)
        End Try

        Return mainAssembly

    End Function

    '檢查檔案是否存在
    Private Function funCheckFileExist(ByVal strFilePath As String) As Boolean

        'Network架構下
        If ApplicationDeployment.IsNetworkDeployed OrElse
           Application.ExecutablePath.ToLower.StartsWith("http") Then
            '暫不檢查
        ElseIf IO.File.Exists(strFilePath) = False Then
            '檔案架構下
            Return False
        End If

        Return True

    End Function

End Module
