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 clsVBPRefReporter
   {
      //  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" + "LIBNAME" + "\t" + "LIBGUID" + "\t" + "MAJVERS" + "\t" + "MINVERS" + "\t" + "LIBPATH";
      }
      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 libname = "";
            libname = "-";
            string libguid = "";
            libguid = "-";
            string majvers = "";
            majvers = "-";
            string minvers = "";
            minvers = "-";
            string libpath = "";
            libpath = "-";


            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)
            {

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

               string[] inp = Array.Empty<string>();
               int i = 0;

               libguid = "";
               if (sLine.Like("reference=*"))
               {
                  inp = sLine.Split("#");
                  if (inp.GetUpperBound(0) == 4)
                  {
                     libguid = inp[0];
                     i = libguid.InStr("{",1,StringComparison.CurrentCulture);
                     libguid = libguid.Mid(i);
                     majvers = inp[1];
                     minvers = inp[2];
                     libpath = inp[3];
                     libname = inp[4];
                  }
               }
               else if (sLine.Like("object=*"))
               {
                  inp = sLine.Split("#");
                  if (inp.GetUpperBound(0) == 2)
                  {
                     libguid = inp[0];
                     i = libguid.InStr("{",1,StringComparison.CurrentCulture);
                     libguid = libguid.Mid(i);
                     majvers = inp[1];
                     minvers = inp[2];
                     inp = minvers.Split(";");
                     minvers = inp[0];
                     libpath = inp[1].Mid(2);
                     libname = libpath;
                  }
               }

               if (libguid.Len() > 0)
               {

                  modScanToolLib.writeRpt(rpt,filpath + "\t" + filname + "\t" + libname + "\t" + libguid + "\t" + majvers + "\t" + minvers + "\t" + libpath + "\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);
         }

      }
   }
}
