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 clsVBPBinReporter
   {
      //  clsVBPBinReporter: Report binary build product 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" + "EXETYPE" + "\t" + "EXENAME" + "\t" + "MAJVERS" + "\t" + "MINVERS";
      }
      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 exetype = "";
            exetype = "-";
            string exename = "";
            exename = "-";
            string majvers = "";
            majvers = "-";
            string minvers = "";
            minvers = "-";


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

               if (sLine.Like("type=*"))
               {
                  exetype = parse1(sLine);
               }
               else if (sLine.Like("exename32*"))
               {
                  exename = parse1(sLine);
               }
               else if (sLine.Like("majorver*"))
               {
                  majvers = parse1(sLine);
               }
               else if (sLine.Like("minorver*"))
               {
                  minvers = parse1(sLine);
               }

            }

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

      }
      private string parse1(string sLine)
      {
         string parse1 = "";


         const string Q = "\"";
         int i = 0;
         int j = 0;

         j = sLine.InStrRev("\\",-1,StringComparison.CurrentCulture);
         if (j > 0)
         {
            j = j + 1;
         }
         else
         {
            j = sLine.InStr("=",1,StringComparison.CurrentCulture) + 1;
         }

         parse1 = (sLine.Mid(j)).TrimB().ReplaceStr(Q,"",1,-1,StringComparison.CurrentCulture);

         return parse1;
      }
   }
}
