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

namespace ScanToolLib
{
   public class clsVBPSrcReporter
   {
      //  clsVBPSrcReporter: Report Source Code for all modules referenced by a VBP file

      public class statusEventArgs : System.EventArgs
      {
         public string msg;
         public statusEventArgs(string msg)
         {
            this.msg = msg;
         }
      }
      public delegate void statusEventHandler(object sender,statusEventArgs e);
      public event System.EventHandler<statusEventArgs> status_Event;
      protected virtual void Onstatus_Event(string msg)
      {
         statusEventArgs e;
         if(status_Event != null)
         {
            e = new statusEventArgs(msg);
            status_Event?.Invoke(this,e);
         }
      }
      public string header()
      {
         return "VBPPATH" + "\t" + "VBPNAME" + "\t" + "MODNAME" + "\t" + "MODTIME" + "\t" + "MODTYPE" + "\t" + "LINENUM" + "\t" + "LINETYPE" + "\t" + "LINETEXT";
      }
      public void processFile(System.IO.FileInfo fil,System.IO.StreamWriter rpt)
      {

         const string FUNC_NAME = "processFile";
         System.IO.StreamReader txtfil = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();

            string filpath = "";
            filpath = fil.Directory.FullName;
            string filname = "";
            filname = fil.Name;
            string modtype = "";
            string modname = "";
            string modpath = "";


            txtfil = modScanToolLib.openInputText(fil.FullName);

            int l = 0;
            l = 1;

            string s = "";
            s = txtfil.ReadToEnd();

            string[] arr = Array.Empty<string>();
            arr = s.Split("\r\n");

            foreach(string v in arr)
            {

               modtype = "";
               string sLine = "";
               sLine = v.LCase();

               if (sLine.Like("form=*.frm") || sLine.Like("class=*.cls") || sLine.Like("module=*.bas") || sLine.Like("usercontrol=*.ctl") || sLine.Like("propertypage=*.pag"))
               {

                  sLine = v;

                  int j = 0;
                  int i = 0;

                  i = sLine.InStr("=",1,StringComparison.CurrentCulture) + 1;

                  modtype = sLine.Left((i - 2));

                  sLine = (sLine.Mid(i)).TrimB();     //  object name; path

                  j = sLine.InStrRev(";",-1,StringComparison.CurrentCulture);

                  if (j == 0)
                  {
                     j = 1;
                  }
                  else
                  {
                     j = j + 1;
                  }

                  sLine = (sLine.Mid(j)).TrimB();     //  object path

                  j = sLine.InStrRev("\\",-1,StringComparison.CurrentCulture);

                  if (j == 0)
                  {
                     j = 1;
                  }
                  else
                  {
                     j = j + 1;
                  }

                  modname = (sLine.Mid(j)).TrimB();     //  file name

                  if (j == 1)
                  {
                     modpath = filpath + "\\" + modname;
                  }
                  else
                  {
                     modpath = sLine;
                  }

                  addLines(rpt,filpath,filname,modpath,modname,modtype);

               }
               else
               {

                  modScanToolLib.writeRpt(rpt,filpath + "\t" + fil.Name + "\t" + fil.Name + "\t" + fil.LastWriteTime.ToStr() + "\t" + "Project" + "\t" + l + "\t" + "PRJ" + "\t" + v + "\r\n");

                  l = l + 1;

               }

            }

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
         }

         if (!(txtfil == null))
         {
            txtfil.Close();
         }
         txtfil = null;

         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            gmRTL.Core.ErrorHandling.RaiseGlobalException(gmRTL.Core.GlobalException.Instance.Number,String.Empty,this.GetTypeName() + "::" + FUNC_NAME + " " + "\r\n" + "[" + fil.FullName + "]" + "\r\n" + gmRTL.Core.GlobalException.Instance.Description);
         }

      }
      private void addLines(System.IO.StreamWriter rpt,string vbppath,string vbpname,string modpath,string modname,string modtype)
      {
         const string FUNC_NAME = "addLines";
         System.IO.StreamReader txtfil = null;
         System.IO.FileInfo fil = null;
         int l = 0;
         try
         {
            gmRTL.Core.GlobalException.Clear();



            // ----------------------------------------------------
            //  open the module
            // ----------------------------------------------------

            txtfil = modScanToolLib.openInputText(modpath);

            // ----------------------------------------------------
            //  parse the module into an array of lines
            // ----------------------------------------------------

            string s = "";
            s = txtfil.ReadToEnd();

            string[] arr = Array.Empty<string>();
            arr = s.Split("\r\n");

            // ----------------------------------------------------
            //  default the line type for forms/ctls to GUI since
            //  the GUI spec is always at the top of the form/ctl.
            // ----------------------------------------------------

            string linetype = "";

            if (modtype == "Form" || modtype == "UserControl")
            {
               linetype = "GUI";
            }
            else
            {
               linetype = "COD";
            }

            // ----------------------------------------------------
            //  get modified date
            // ----------------------------------------------------

            DateTime LastModDate = DateTime.MinValue;
            fil = new System.IO.FileInfo(modpath);
            LastModDate = fil.LastWriteTime;

            // ----------------------------------------------------
            //  process each line
            // ----------------------------------------------------

            l = 1;

            foreach(string v in arr)
            {

               string sLine = "";
               sLine = v.ReplaceStr("\t","    ",1,-1,StringComparison.CurrentCulture);

               string trimmedLine = "";
               trimmedLine = sLine.TrimB();


               string sType = "";
               if (trimmedLine.Left(1) == "'")
               {
                  sType = "CMN";
               }
               else if (trimmedLine.Len() == 0)
               {
                  sType = "BLN";
               }
               else
               {
                  sType = linetype;
               }

               modScanToolLib.writeRpt(rpt,vbppath + "\t" + vbpname + "\t" + modname + "\t" + LastModDate.ToStr() + "\t" + modtype + "\t" + l + "\t" + sType + "\t" + sLine + "\r\n");

               l = l + 1;

               if (linetype == "GUI")
               {
                  if (sLine == "End")
                  {
                     linetype = "COD";
                  }
               }

            }

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
         }

         if (!(txtfil == null))
         {
            txtfil.Close();
         }
         fil = null;
         txtfil = null;

         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {

            modScanToolLib.writeRpt(rpt,vbppath + "\t" + vbpname + "\t" + modname + "\t" + DateTime.Now.ToStr() + "\t" + modtype + "\t" + l + "\t" + "ERR" + "\t" + modpath + ":" + gmRTL.Core.GlobalException.Instance.Description + "\r\n");
         }

      }
   }
}
