Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Drawing
Imports VBNET = Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Imports gmRTL.Core
Imports gmRTL.WPF

Namespace ScanToolUI
   Public Partial Class frmScantool : Inherits Window
      Public optRpt As System.Collections.Generic.SortedList(Of Integer, System.Windows.Controls.RadioButton) = Nothing
      <System.Diagnostics.DebuggerNonUserCode()>
      Public Sub New()
      MyBase.New()
         '
         ' Required for Windows Form Designer support
         '
         InitializeComponent()
         '
         ' TODO: Add any constructor code after InitializeComponent call
         '
         optRpt = New System.Collections.Generic.SortedList(Of Integer, System.Windows.Controls.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)
      End Sub
      Dim WithEvents scanControl As ScanToolLib.clsScanControl = Nothing
      Private Sub cmdReport_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
         Const FUNC_NAME As String = "cmdReport_Click"
         Try
            gmRTL.Core.GlobalException.ClearErrorObject()

            scanControl.chkNoCase = chkNoCase.IsChecked.Value = true
            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 RoutedEventArgs)
         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 RoutedEventArgs)
         scanControl = New ScanToolLib.clsScanControl
         txtRootPath.Text = Dir1.Path
         optRpt_Click(_optRpt_0,New RoutedEventArgs())
      End Sub
      Private Sub Form_Resize(ByVal sender As Object, ByVal e As SizeChangedEventArgs)
         txtStatus.Width = Me.Surface.ScaleWidth - Canvas.GetLeft(txtStatus)
         Canvas.SetTop(txtStatus,Me.Surface.ScaleHeight - txtStatus.Height)
      End Sub
      Private Sub mnuFileExit_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
         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
            ' Hand-Written Code
            Dim CommonDialog1 As System.Windows.Forms.OpenFileDialog = new System.Windows.Forms.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.IsChecked = scanControl.chkNoCase

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

         Catch exc As Exception
            System.Windows.MessageBox.Show("Error Opening File " & Me.GetType().Name & "::" & FUNC_NAME & " " & Environment.NewLine & exc.Message, "", "Error", MessageBoxButton.OK)
         End Try
      End Sub
      Private Sub mnuFileSave_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
         Const FUNC_NAME As String = "mnuFileSave_Click"
         Try
            gmRTL.Core.GlobalException.ClearErrorObject()

            '' UPGRADE_TODO: CommonDialog1.CancelError = False
            Dim CommonDialog1 As System.Windows.Forms.SaveFileDialog   = New System.Windows.Forms.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.MessageBox.Show("Opening File " & TypeName(Me) & "::" & FUNC_NAME & " " & Environment.NewLine & Err.Description, "", MessageBoxButton.OK)
         End If
      End Sub
      Private Sub mnuHelp_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
         Dim helpFilePath As String = ""
         helpFilePath = gmRTL.Core.App.DefInstance.Path & "\ScantoolHelp.htm"

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

      End Sub
      Private Sub optRpt_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs)
         Dim optRpt As System.Windows.Controls.RadioButton
         optRpt = CType(sender,System.Windows.Controls.RadioButton)
         Dim Index As Integer = Me.optRpt.Keys(Me.optRpt.IndexOfValue(optRpt))
         setStatus(optRpt.Content.ToString() & ":" & optRpt.ToolTip)
      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).Content.ToString() = str) Then
               optRpt(i).IsChecked = True
               Exit For
            End If
         Next
      End Sub
      Private Sub optRpt_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
         Dim optRpt As System.Windows.Controls.RadioButton
         optRpt = CType(sender,System.Windows.Controls.RadioButton)
         Dim Index As Integer = Me.optRpt.Keys(Me.optRpt.IndexOfValue(optRpt))
         If Not CType(sender, RadioButton).IsChecked Then Return
         Select Case optRpt.Content.ToString()
            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.Content.ToString() & ".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).IsChecked.Value) Then
               optRptCaption = optRpt(i).Content.ToString()
               Exit For
            End If
         Next
      End Function
   End Class
End Namespace
