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

namespace ScanToolUI
{
   public partial class frmScantool : Window
   {
      public System.Collections.Generic.SortedList<int, System.Windows.Controls.RadioButton> optRpt = null;
      public frmScantool()
      {
         //
         // Required for Windows Form Designer support
         //
         InitializeComponent();
         //
         // TODO: Add any constructor code after InitializeComponent call
         //
         optRpt = new System.Collections.Generic.SortedList<int, 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);
      }
      private ScanToolLib.clsScanControl _scanControl;
      ScanToolLib.clsScanControl scanControl
      {
         get
         {
            return this._scanControl;
         }
         set
         {
            if (this._scanControl != value)
            {
               this._scanControl = value;
               if(this._scanControl != null)
               {
                  this._scanControl.status_Event += this.scanControl_status;
               }
            }
         }
      }
      private void cmdReport_Click(object sender, RoutedEventArgs e)
      {
         const string FUNC_NAME = "cmdReport_Click";
         try
         {
            gmRTL.Core.GlobalException.Clear();

            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(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
         }

         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            setStatus("Completed with error " + this.GetTypeName() + "::" + FUNC_NAME + " " + "\r\n" + gmRTL.Core.GlobalException.Instance.Description);
         }
      }
      private void cmdView_Click(object sender, RoutedEventArgs e)
      {
         const string FUNC_NAME = "cmdView_Click";
         try
         {
            gmRTL.Core.GlobalException.Clear();
            modScanToolUI.ShellEx(txtSaveTo.Text);
         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
         }

         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            setStatus("Completed with error " + this.GetTypeName() + "::" + FUNC_NAME + " " + "\r\n" + gmRTL.Core.GlobalException.Instance.Description);
         }
      }
      private void Dir1_Change(object sender, EventArgs e)
      {
         txtRootPath.Text = Dir1.Path;
      }
      private void Drive1_Change(object sender, EventArgs e)
      {
         Dir1.Path = Drive1.Drive;
      }
      private void Form_Load(object sender, RoutedEventArgs e)
      {
         scanControl = new ScanToolLib.clsScanControl();
         txtRootPath.Text = Dir1.Path;
         optRpt_Click(_optRpt_0,new RoutedEventArgs());
      }
      private void Form_Resize(object sender, SizeChangedEventArgs e)
      {
         txtStatus.Width = this.Surface.ScaleWidth - (int)Canvas.GetLeft(txtStatus);
         Canvas.SetTop(txtStatus,(double)(this.Surface.ScaleHeight - txtStatus.Height));
      }
      private void mnuFileExit_Click(object sender, RoutedEventArgs e)
      {
         this.Close();
      }
      private void mnuFileOpen_Click(object sender, EventArgs e) // UPGRADE_INFO: Hand-written code
      {

         const string FUNC_NAME = "mnuFileOpen_Click";
         try
         {
            // Hand-Written Code
            var CommonDialog1 = new System.Windows.Forms.OpenFileDialog();
            CommonDialog1.Filter = "ScanTool Config Files (*.Scantool.xml)|*.Scantool.xml";
            CommonDialog1.FileName = "";
            CommonDialog1.ShowDialog();
            if (CommonDialog1.FileName == "")
            {
               return;
            }

            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(Exception exc)
         {
            System.Windows.MessageBox.Show("Error Opening File " + this.GetType().Name + "::" + FUNC_NAME + " " + Environment.NewLine + exc.Message, "Error", MessageBoxButton.OK);
         }
      }
      private void mnuFileSave_Click(object sender, RoutedEventArgs e)
      {
         const string FUNC_NAME = "mnuFileSave_Click";
         try
         {
            gmRTL.Core.GlobalException.Clear();

            // UPGRADE_TODO: CommonDialog1.CancelError = false;
            var CommonDialog1 = new System.Windows.Forms.SaveFileDialog();
            CommonDialog1.Filter = "ScanTool Config Files (*.Scantool.xml)|*.Scantool.xml";
            CommonDialog1.FileName = "";
            CommonDialog1.ShowDialog();
            if (CommonDialog1.FileName == "")
            {
               return;
            }

            scanControl.saveScantoolConfig(CommonDialog1.FileName);

            setStatus("SAVED CONFIG: " + CommonDialog1.FileName);


         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
         }
         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            System.Windows.MessageBox.Show("Opening File " + this.GetTypeName() + "::" + FUNC_NAME + " " + "\r\n" + gmRTL.Core.GlobalException.Instance.Description, "", MessageBoxButton.OK);
         }
      }
      private void mnuHelp_Click(object sender, RoutedEventArgs e)
      {
         string helpFilePath = "";
         helpFilePath = gmRTL.Core.App.DefInstance.Path + "\\ScantoolHelp.htm";

         if (gmRTL.Core.FileSystemHelper.Dir(helpFilePath,gmRTL.Core.FileSystemHelper.FileAttribute.Normal) == "")
         {
            System.Windows.MessageBox.Show("Help File not found:" + "\r\n" + helpFilePath, "", MessageBoxButton.OK,MessageBoxImage.Warning);
         }
         else
         {
            modScanToolUI.ShellEx(helpFilePath);
         }

      }
      private void optRpt_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
      {
         System.Windows.Controls.RadioButton optRpt;
         optRpt = (System.Windows.Controls.RadioButton)sender;
         int Index = this.optRpt.Keys[this.optRpt.IndexOfValue(optRpt)];
         setStatus(optRpt.Content.ToString() + ":" + optRpt.ToolTip);
      }
      public void scanControl_status(object sender, ScanToolLib.clsScanControl.statusEventArgs e)
      {
         string msg;
         msg = e.msg;
         setStatus(msg);
      }
      private void setStatus(string msg)
      {
         txtStatus.Text = msg;
         txtStatus.Refresh();
      }
      private void setOptRptfromString(string str)
      {
         int i = 0;
         for(i = 0; i <= optRpt.Count - 1; i += 1)
         {
            if (optRpt[i].Content.ToString() == str)
            {
               optRpt[i].IsChecked = true;
               break;  // Exit For
            }
         }
      }
      private void optRpt_Click(object sender, RoutedEventArgs e)
      {
         System.Windows.Controls.RadioButton optRpt;
         optRpt = (System.Windows.Controls.RadioButton)sender;
         int Index = this.optRpt.Keys[this.optRpt.IndexOfValue(optRpt)];
         if (!((RadioButton)(sender)).IsChecked == true) return;
         switch(optRpt.Content.ToString())
         {
         case "DirList":
            txtFolderFilter.Text = "*";
            txtFilFilter.Text = "*";
            txtContentFilter.Text = "";
            break;
         case "COMGuid":
            txtFolderFilter.Text = "*";
            txtFilFilter.Text = "*.ocx,*.exe,*.dll,*.olb,*.tlb";
            break;
         case "VBPRef":
            txtFolderFilter.Text = "*";
            txtFilFilter.Text = "*.vbp";
            break;
         case "VBPBin":
            txtFolderFilter.Text = "*";
            txtFilFilter.Text = "*.vbp";
            break;
         case "VBPMod":
            txtFolderFilter.Text = "*";
            txtFilFilter.Text = "*.vbp";
            break;
         case "VBPSource":
            txtFolderFilter.Text = "*";
            txtFilFilter.Text = "*.vbp";
            break;
         case "VBPCount":
            txtFolderFilter.Text = "*";
            txtFilFilter.Text = "*.vbp";
            break;
         case "FolderCnt":
            txtFolderFilter.Text = "*";
            txtFilFilter.Text = "*";
            break;
         }

         txtSaveTo.Text = optRpt.Content.ToString() + ".tab";

      }
      private string optRptCaption()
      {
         string optRptCaption = "";

         int i = 0;
         for(i = 0; i <= optRpt.Count - 1; i += 1)
         {
            if (optRpt[i].IsChecked.Value)
            {
               optRptCaption = optRpt[i].Content.ToString();
               break;  // Exit For
            }
         }
         return optRptCaption;
      }
   }
}
