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 clsCOMReporter
   {
      //  clsCOMReporter: Reports TypeLib information for COM Comnponents

      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 "COMPATH" + "\t" + "COMNAME" + "\t" + "EXT" + "\t" + "LIBGUID" + "\t" + "LIBNAME";
      }
      public void processFile(System.IO.FileInfo fil,System.IO.StreamWriter rpt)
      {
         const string FUNC_NAME = "processFile";

         string filpath = "";
         filpath = fil.Directory.FullName;
         string filname = "";
         filname = fil.Name;

         string libname = "";
         string libguid = "";
         gmRTL.TLI.TypeLibInfo tlinfo = null;

         gmRTL.Core.ErrorHandling.ResumeNext(() =>
         {
            gmRTL.TLI.TLIApplication tliApp = null;
            tliApp = new gmRTL.TLI.TLIApplication();
            tlinfo = tliApp.TypeLibInfoFromFile(fil.FullName);
            if (gmRTL.Core.GlobalException.Instance.Number == ((int)gmRTL.TLI.TliErrors.tliErrCantLoadLibrary))
            {
               libname = "-";
               libguid = "ERR=" + gmRTL.Core.GlobalException.Instance.Number + ": " + gmRTL.Core.GlobalException.Instance.Description;
               gmRTL.Core.GlobalException.Clear();
            }
            else
            {
               libname = tlinfo.Name;
               libguid = tlinfo.GUID;
            }
         });
         try
         {
            gmRTL.Core.GlobalException.Clear();

            string filextn = "";
            int i = 0;
            i = filname.InStrRev(".",-1,StringComparison.CurrentCulture);
            if (i > 0)
            {
               filextn = fil.Name.Mid((i + 1));
            }
            else
            {
               filextn = "-";
            }

            modScanToolLib.writeRpt(rpt,filpath + "\t" + filname + "\t" + filextn + "\t" + libguid + "\t" + libname + "\r\n");

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

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

      }
   }
}
