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 ScanToolLib
{
   public class clsVBPModReporter
   {
      //  clsVBPRefReporter: Report COM references for 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" + "MODTYPE" + "\t" + "MODNAME";
      }
      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 = "";


            txtfil = modScanToolLib.openInputText(fil.FullName);

            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

                  modScanToolLib.writeRpt(rpt,filpath + "\t" + filname + "\t" + modname + "\t" + modtype + "\r\n");


               }

            }

         }
         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);
         }

      }
   }
}
