Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Namespace ScanToolUI
   Public Class frmScantool : Inherits System.Windows.Forms.Form
      Private Dim _disposed As Boolean = False
      <System.Diagnostics.DebuggerNonUserCode()> _
      Public Sub New()
         MyBase.New()
         'This call is required by the Windows Form Designer.
         InitializeComponent()
         'Add any initialization after the InitializeComponent() call
         If (DesignMode) Then Exit Sub
         optRpt = New System.Collections.Generic.SortedList(Of Integer, System.Windows.Forms.RadioButton)
         optRpt.Add(7, _optRpt_7)
         optRpt.Add(6, _optRpt_6)
         optRpt.Add(5, _optRpt_5)
         optRpt.Add(4, _optRpt_4)
         optRpt.Add(3, _optRpt_3)
         optRpt.Add(0, _optRpt_0)
         optRpt.Add(2, _optRpt_2)
         optRpt.Add(1, _optRpt_1)
         Form_Load(Nothing, Nothing)
         Form_Resize(Nothing, Nothing)

      End Sub
      'Form overrides dispose to clean up the component list.
      <System.Diagnostics.DebuggerNonUserCode()> _
      Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
         If _disposed Then
            Return
         End If
         If Disposing Then
            If Not components Is Nothing Then
               components.Dispose()
            End If
         End If
         _disposed = True
         MyBase.Dispose(Disposing)
      End Sub
      Dim WithEvents scanControl As ScanToolLib.clsScanControl = Nothing
      Private Sub cmdReport_Click(ByVal sender As Object, ByVal e As EventArgs)
         Const FUNC_NAME As String = "cmdReport_Click"
         Try
            gmRTL.Core.GlobalException.ClearErrorObject()

            scanControl.chkNoCase = chkNoCase.CheckState = System.Windows.Forms.CheckState.Checked
            scanControl.optRpt = optRptCaption()
            scanControl.txtContentFilter = txtContentFilter.Text
            scanControl.txtFolderFilter = txtFolderFilter.Text
            scanControl.txtFilFilter = txtFilFilter.Text
            scanControl.txtRootPath = txtRootPath.Text
            scanControl.txtSaveTo = txtSaveTo.Text
            scanControl.runReport()

         Catch exc As System.Exception
            gmRTL.Core.GlobalException.SetErrorObject(exc)
         End Try

         If (Err.Number <> 0) Then
            setStatus("Completed with error " & TypeName(Me) & "::" & FUNC_NAME & " " & Environment.NewLine & Err.Description)
         End If
      End Sub
      Private Sub cmdView_Click(ByVal sender As Object, ByVal e As EventArgs)
         Const FUNC_NAME As String = "cmdView_Click"
         Try
            gmRTL.Core.GlobalException.ClearErrorObject()
            modScanToolUI.ShellEx(txtSaveTo.Text)
         Catch exc As System.Exception
            gmRTL.Core.GlobalException.SetErrorObject(exc)
         End Try

         If (Err.Number <> 0) Then
            setStatus("Completed with error " & TypeName(Me) & "::" & FUNC_NAME & " " & Environment.NewLine & Err.Description)
         End If
      End Sub
      Private Sub Dir1_Change(ByVal sender As Object, ByVal e As EventArgs)
         txtRootPath.Text = Dir1.Path
      End Sub
      Private Sub Drive1_Change(ByVal sender As Object, ByVal e As EventArgs)
         Dir1.Path = Drive1.Drive
      End Sub
      Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs)
         scanControl = New ScanToolLib.clsScanControl
         txtRootPath.Text = Dir1.Path
         optRpt_Click(_optRpt_0,New EventArgs())
      End Sub
      Private Sub Form_Resize(ByVal sender As Object, ByVal e As EventArgs)
         txtStatus.Width = Me.ClientRectangle.Width - txtStatus.Left
         txtStatus.Top = Me.ClientRectangle.Height - txtStatus.Height
      End Sub
      Private Sub mnuFileExit_Click(ByVal sender As Object, ByVal e As EventArgs)
         Me.Close()
      End Sub
      Private Sub mnuFileOpen_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuFileOpen.Click ' UPGRADE_INFO: Hand-written code
         Const FUNC_NAME As String = "mnuFileOpen_Click"
         Try
            Dim CommonDialog1 As OpenFileDialog = new OpenFileDialog()
            CommonDialog1.Filter = "ScanTool Config Files (*.Scantool.xml)|*.Scantool.xml"
            CommonDialog1.FileName = ""
            CommonDialog1.ShowDialog()
            If (CommonDialog1.FileName = "") Then
               Exit Sub
            End If

            scanControl.loadScantoolConfig(CommonDialog1.FileName)

            setOptRptfromString(scanControl.optRpt)
            txtRootPath.Text = scanControl.txtRootPath
            txtFilFilter.Text = scanControl.txtFilFilter
            txtFolderFilter.Text = scanControl.txtFolderFilter
            txtContentFilter.Text = scanControl.txtContentFilter
            txtSaveTo.Text = scanControl.txtSaveTo
            chkNoCase.CheckState = IIf(scanControl.chkNoCase,System.Windows.Forms.CheckState.Checked,System.Windows.Forms.CheckState.Unchecked)

            setStatus("LOADED CONFIG: " & CommonDialog1.FileName)

         Catch exc As Exception
            MessageBox.Show("Error Opening File " & TypeName(Me) & "::" & FUNC_NAME & " " & Environment.NewLine & Err.Description, "", MessageBoxButtons.OK)
         End Try
      End Sub
      Private Sub mnuFileSave_Click(ByVal sender As Object, ByVal e As EventArgs)
         Const FUNC_NAME As String = "mnuFileSave_Click"
         Try
            gmRTL.Core.GlobalException.ClearErrorObject()

            '' UPGRADE_TODO: CommonDialog1.CancelError = False
            Dim CommonDialog1 As New SaveFileDialog()
            CommonDialog1.Filter = "ScanTool Config Files (*.Scantool.xml)|*.Scantool.xml"
            CommonDialog1.FileName = ""
            CommonDialog1.ShowDialog()
            If (CommonDialog1.FileName = "") Then
               Exit Sub
            End If

            scanControl.saveScantoolConfig(CommonDialog1.FileName)

            setStatus("SAVED CONFIG: " & CommonDialog1.FileName)


         Catch exc As System.Exception
            gmRTL.Core.GlobalException.SetErrorObject(exc)
         End Try
         If (Err.Number <> 0) Then
            System.Windows.Forms.MessageBox.Show("Opening File " & TypeName(Me) & "::" & FUNC_NAME & " " & Environment.NewLine & Err.Description, "", MessageBoxButtons.OK)
         End If
      End Sub
      Private Sub mnuHelp_Click(ByVal sender As Object, ByVal e As EventArgs)
         Dim helpFilePath As String = ""
         helpFilePath = gmRTL.Core.App.DefInstance.Path & "\ScantoolHelp.htm"

         If (FileSystem.Dir(helpFilePath,FileAttribute.Normal) = "") Then
            System.Windows.Forms.MessageBox.Show("Help File not found:" & Environment.NewLine & helpFilePath, "", MessageBoxButtons.OK,MessageBoxIcon.Warning)
         Else
            modScanToolUI.ShellEx(helpFilePath)
         End If

      End Sub
      Private Sub optRpt_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
         Dim optRpt As System.Windows.Forms.RadioButton
         optRpt = CType(sender,System.Windows.Forms.RadioButton)
         Dim Index As Integer = Me.optRpt.Keys(Me.optRpt.IndexOfValue(optRpt))
         setStatus(optRpt.Text & ":" & ToolTip1.GetToolTip(optRpt))
      End Sub
      Public Sub scanControl_status(ByVal sender As Object, ByVal e As ScanToolLib.clsScanControl.statusEventArgs) Handles scanControl.status_Event
         Dim msg As String
         msg = e.msg
         setStatus(msg)
      End Sub
      Private Sub setStatus(ByVal msg As String)
         txtStatus.Text = msg
         txtStatus.Refresh
      End Sub
      Private Sub setOptRptfromString(ByVal str As String)
         Dim i As Integer = 0
         For i = 0 To optRpt.Count - 1 Step 1
            If (optRpt(i).Text = str) Then
               optRpt(i).Checked = True
               Exit For
            End If
         Next
      End Sub
      Private Sub optRpt_Click(ByVal sender As Object, ByVal e As EventArgs)
         Dim optRpt As System.Windows.Forms.RadioButton
         optRpt = CType(sender,System.Windows.Forms.RadioButton)
         Dim Index As Integer = Me.optRpt.Keys(Me.optRpt.IndexOfValue(optRpt))
         If Not CType(sender, RadioButton).Checked Then Return
         Select Case optRpt.Text
            Case "DirList"
               txtFolderFilter.Text = "*"
               txtFilFilter.Text = "*"
               txtContentFilter.Text = ""
            Case "COMGuid"
               txtFolderFilter.Text = "*"
               txtFilFilter.Text = "*.ocx,*.exe,*.dll,*.olb,*.tlb"
            Case "VBPRef"
               txtFolderFilter.Text = "*"
               txtFilFilter.Text = "*.vbp"
            Case "VBPBin"
               txtFolderFilter.Text = "*"
               txtFilFilter.Text = "*.vbp"
            Case "VBPMod"
               txtFolderFilter.Text = "*"
               txtFilFilter.Text = "*.vbp"
            Case "VBPSource"
               txtFolderFilter.Text = "*"
               txtFilFilter.Text = "*.vbp"
            Case "VBPCount"
               txtFolderFilter.Text = "*"
               txtFilFilter.Text = "*.vbp"
            Case "FolderCnt"
               txtFolderFilter.Text = "*"
               txtFilFilter.Text = "*"
         End Select

         txtSaveTo.Text = optRpt.Text & ".tab"

      End Sub
      Private Function optRptCaption() As String
         optRptCaption = ""
         Dim i As Integer = 0
         For i = 0 To optRpt.Count - 1 Step 1
            If (optRpt(i).Checked) Then
               optRptCaption = optRpt(i).Text
               Exit For
            End If
         Next
      End Function
   End Class
End Namespace
