using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Data;
using VBNET = Microsoft.VisualBasic;
using gmRTL.Core;
using Microsoft.VisualBasic.CompilerServices;
using System.Linq;
using System.Collections.Generic;

namespace VBSCAN
{
   public partial class frmVbScan : System.Windows.Forms.Form
   {
      private bool _disposed = false;
      public frmVbScan()
      {
         if(m_vb6FormDefInstance == null)
         {
            m_vb6FormDefInstance = this;
         }
         //
         // Required for Windows Form Designer support
         //
         InitializeComponent();

         //
         // TODO: Add any constructor code after InitializeComponent call
         //
         if (DesignMode) return;
         optDtlView = new System.Collections.Generic.SortedList<int, System.Windows.Forms.RadioButton>();
         optDtlView.Add(1, _optDtlView_1);
         optDtlView.Add(0, _optDtlView_0);
         Form_Load(null, null);
         Form_Resize(null, null);
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
      protected override void Dispose(bool disposing)
      {
         if (_disposed)
         {
            return;
         }
         if (disposing)
         {
            if (components != null)
            {
               components.Dispose();
            }
         }
         _disposed = true;
         base.Dispose(disposing);
      }

      private void chkICase_Click(object sender, EventArgs e)
      {
         modVBSCAN.giCase = ((int)chkICase.CheckState);
      }
      private void chkiCmnts_Click(object sender, EventArgs e)
      {
         modVBSCAN.giCmnts = ((int)chkiCmnts.CheckState);
      }
      private void cmdGO_Click(object sender, EventArgs e)
      {
         int rc = 0;

         rc = SetSrch();
         if (rc == 0)
         {
            Console.Beep();
            System.Windows.Forms.MessageBox.Show("Please enter search strings.", "", MessageBoxButtons.OK);
            txtSrch.Focus();
            return;
         }

         if (modVBSCAN.gsMAKName == "")
         {
            Console.Beep();
            mnuOpen_Click(null,new EventArgs());
            if (modVBSCAN.gsMAKName == "")
            {
               return;
            }
         }

         modVBSCAN.gNumHits = 0;
         modVBSCAN.giStopScan = 0;
         OL1.Focus();
         System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
         cmdGo.Enabled = false;

         rc = modVBSCAN.rdPrjFile(modVBSCAN.gsMAKName);

         cmdGo.Enabled = true;
         System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;

         if (modVBSCAN.giStopScan == 1)
         {
            System.Windows.Forms.MessageBox.Show("Scan Aborted by User", "", MessageBoxButtons.OK);
            gmRTL.Core.ErrorHandling.RaiseGlobalException(17,"Scan Aborted by User",String.Empty);
         }
         else
         {
            if (rc == 0)
            {
               OL1.AddItem("[Scan completed successfully. " + modVBSCAN.gNumHits + " hits.]");
               OL1.set_Indent(OL1.ListCount - 1,1);
               OL1.set_ItemData(OL1.ListCount - 1,-1);
            }
            else
            {
               gmRTL.Core.ErrorHandling.RaiseGlobalException(17,"Scan failed. Some modules could not be read.",String.Empty);
            }
         }
      }
      private void Form_Load(object sender, EventArgs e)
      {
         modVBSCAN.giCase = ((int)chkICase.CheckState);

         modVBSCAN.giCmnts = ((int)chkiCmnts.CheckState);

         if (_optDtlView_0.Checked)
         {
            modVBSCAN.gsDtlView = _optDtlView_0.Text;
         }
         else
         {
            modVBSCAN.gsDtlView = _optDtlView_1.Text;
         }

      }
      private void Form_Resize(object sender, EventArgs e)
      {

         gmRTL.Core.ErrorHandling.ResumeNext(() =>
         {
            OL1.Width = this.ClientRectangle.Width - OL1.Left - 10;
            OL1.Height = this.ClientRectangle.Height - OL1.Top - 10;

         });
      }
      private void mnuDoc_Click(object sender, EventArgs e)
      {
         try
         {

            txtSrch.Text = modVBSCAN.gsDocComment;
            chkiCmnts.CheckState = System.Windows.Forms.CheckState.Unchecked;
            chkICase.CheckState = System.Windows.Forms.CheckState.Checked;

            if (OL1.ListCount < 1)
            {
               cmdGO_Click(cmdGo,new EventArgs());
            }

            if (modVBSCAN.gsDocFile == "")
            {
               Console.Beep();
               mnuDocOpt_Click(null,new EventArgs());
               if (modVBSCAN.gsDocFile == "")
               {
                  return;
               }
            }

            string sFile = "";
            sFile = modVBSCAN.gsDocFile;
            modVBSCAN.MakeHtm(sFile);
            System.Windows.Forms.MessageBox.Show("HTML saved in " + sFile, "", MessageBoxButtons.OK);


         }
         catch{}
         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            System.Windows.Forms.MessageBox.Show(gmRTL.Core.GlobalException.Instance.Description, "", MessageBoxButtons.OK);
         }
      }
      private void mnuDocOpt_Click(object sender, EventArgs e)
      {
         frmOptions.DefInstance.ShowDialog();
      }
      private void mnuExit_Click(object sender, EventArgs e)
      {
         Environment.Exit(0);
      }
      private void mnuHelp_Click(object sender, EventArgs e)
      {
         string sMsg = "";

         sMsg = "";
         sMsg = sMsg + "Open a VB project file (*.mak); enter the pattern(s) to search for; and click 'scan' to start search (ESC to abort)." + 10.Char() + 10.Char();
         sMsg = sMsg + "The modules containing the pattern will be flagged with a plus sign. Click on a plus to see more detail" + 10.Char() + 10.Char();
         sMsg = sMsg + "Double click on a function or subroutine to inspect its code.";

         System.Windows.Forms.MessageBox.Show(sMsg, "", MessageBoxButtons.OK);
      }
      private void mnuOpen_Click(object sender, EventArgs e)
      {

         CMD1.Filter = "Project Files|*.MAK;*.VBP";
         CMD1.DialogTitle = "Open VB Project File";
         CMD1.FilterIndex = 1;
         CMD1.FileName = "";
         CMD1.CancelError = false;
         CMD1.ShowOpen();
         if (CMD1.FileName != "")
         {
            modVBSCAN.gsMAKName = CMD1.FileName;
            frmVbScan.DefInstance.Text = "VB VBScan: " + modVBSCAN.gsMAKName;
            modVBSCAN.gsMAKPath = modVBSCAN.gsMAKName.Left(modVBSCAN.RInStr(modVBSCAN.gsMAKName,"\\"));
         }
      }
      private void mnuSave_Click(object sender, EventArgs e)
      {
         string sFile = "";
         sFile = "c:\\temp\\vbscan.txt";

         if (modVBSCAN.SaveCSR(sFile) != 0)
         {
            System.Windows.Forms.MessageBox.Show("Error saving VBScan Report", "", MessageBoxButtons.OK);
         }
         else
         {
            gmRTL.Core.InteractionHelper.Shell("notepad " + sFile,gmRTL.Core.Enums.AppWinStyle.NormalFocus);
         }

      }
      private void OL1_Click(object sender, EventArgs e)
      {
         System.Diagnostics.Debug.WriteLine("Click ListIndex=" + OL1.ListIndex + "  -- " + OL1.FullPath(OL1.ListIndex));
      }
      private void OL1_DblClick(object sender, EventArgs e)
      {
         int rc = 0;
         int i = 0;
         i = OL1.ListIndex;
         int O = 0;
         int l = 0;
         string sf = "";
         sf = OL1.List(i).Left(3);
         string sMod = "";
         string sRtn = "";


         if (OL1.ItemData(i) == -1)
         {
            return;
         }

         sf = "";
         for(O = i; O >= 0; O += -1)
         {
            if (OL1.ItemData(O) > 0)
            {
               sf = modVBSCAN.gsModPath[OL1.ItemData(O)] + "\\" + OL1.List(i);
               break;  // Exit For
            }
         }


         if (OL1.Indent(i) == 1)
         {
            sRtn = "General";
         }
         else if (OL1.Indent(i) == 2)
         {
            sRtn = OL1.List(i);
         }
         else
         {
            for(O = i; O >= 0; O += -1)
            {
               if (OL1.Indent(O) == 2)
               {
                  sRtn = OL1.List(O);
                  break;  // Exit For
               }
            }
         }

         l = sf.Len();
         i = modVBSCAN.RInStr(sf,"\\");
         if (i > 0)
         {
            sMod = sf.Left((i - 1));
         }

         System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

         if (sRtn == "General")
         {
            modVBSCAN.rdGeneral(sMod);
         }
         else
         {
            modVBSCAN.rdRoutine(sMod,sRtn);
         }

         System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
         if (rc == 0)
         {
            l = sMod.Len();
            i = modVBSCAN.RInStr(sMod,"\\");
            if (i > 0)
            {
               sMod = sMod.Right((l - i));
            }
            if (modVBSCAN.gsDtlView == "List")
            {
               frmRtnLst.DefInstance.Text = "VB VBScan: " + sMod + "\\" + sRtn;
               frmRtnLst.DefInstance.Show();
            }
            else
            {
               frmRtnOL.DefInstance.Text = "VB VBScan: " + sMod + "\\" + sRtn;
               frmRtnOL.DefInstance.Show();
            }

         }
         else
         {
            System.Windows.Forms.MessageBox.Show("Unable to Load " + sf, "", MessageBoxButtons.OK);
         }

      }
      private void OL1_KeyDown(object sender, KeyEventArgs e)
      {
         int keyCode;
         keyCode = e.KeyValue;
         if (keyCode == 27)
         {
            modVBSCAN.giStopScan = 1;
         }
      }
      private int SetSrch()
      {
         int SetSrch = 0;

         try
         {
            SetSrch = 0;
            int l = 0;
            int i = 0;
            string sTxt = "";
            string sbuf = "";
            string c = "";
            int wc = 0;

            sTxt = txtSrch.Text.TrimB();
            l = sTxt.Len();
            if (l == 0)
            {
               return SetSrch;
            }

            for(i = 1; i <= l + 1; i += 1)
            {
               c = sTxt.Mid(i, 1);
               if (c != 13.Char() && i <= l)
               {
                  sbuf = sbuf + c;
               }
               else
               {
                  if (sbuf.Len() > 0 && sbuf.Left(1) != 13.Char())
                  {
                     i = i + 1;
                     wc = wc + 1;
                     Array.Resize(ref modVBSCAN.gsSrch, wc + 1);
                     modVBSCAN.gsSrch[wc] = sbuf;
                     sbuf = "";
                  }
               }
            }

            SetSrch = wc;
            return SetSrch;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
            System.Windows.Forms.MessageBox.Show("Error [SetSrch] " + gmRTL.Core.GlobalException.Instance.Description + " #" + gmRTL.Core.GlobalException.Instance.Number, "", MessageBoxButtons.OK);
         }
         return SetSrch;
      }
      private void optDtlView_Click(object sender, EventArgs e)
      {
         System.Windows.Forms.RadioButton optDtlView;
         optDtlView = (System.Windows.Forms.RadioButton)sender;
         int Index = this.optDtlView.Keys[this.optDtlView.IndexOfValue(optDtlView)];
         if (!((RadioButton)(sender)).Checked) return;
         modVBSCAN.gsDtlView = optDtlView.Text;
      }
   }
}
