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 clsDirList
   {
      //  clsDirList: Generic Directory Search

      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);
         }
      }
      private string mtxtFilter = "";
      private string[] mtxtPats = Array.Empty<string>();
      private int mtxtPatCnt = 0;
      public bool noCase = false;
      public string header()
      {
         string header = "";

         if (mtxtFilter.Len() == 0)
         {
            header = "PATH" + "\t" + "FILE" + "\t" + "EXT" + "\t" + "SIZE" + "\t" + "CREATED";
         }
         else
         {
            header = "PATH" + "\t" + "FILE" + "\t" + "LINE" + "\t" + "TEXT";
         }
         return header;
      }
      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;


            txtfil = modScanToolLib.openInputText(fil.FullName);

            int i = 0;
            if (mtxtFilter.Len() == 0)
            {
               string sExt = "";
               i = filname.InStrRev(".",-1,StringComparison.CurrentCulture);
               if (i > 0)
               {
                  sExt = filname.Mid((i + 1));
               }
               else
               {
                  sExt = "-";
               }
               modScanToolLib.writeRpt(rpt,filpath + "\t" + filname + "\t" + sExt + "\t" + fil.Length + "\t" + fil.CreationTime.ToStr() + "\r\n");
            }
            else
            {
                    //  FILTER FOR SPECIFIC TEXT
               int l = 0;
               string s = "";
               while (!(txtfil.Peek() == -1))
               {
                  l = l + 1;
                  s = txtfil.ReadLine();
                  for(i = 0; i <= mtxtPatCnt - 1; i += 1)
                  {
                     string ss = "";
                     string pt = "";
                     if (noCase)
                     {
                        ss = s.LCase();
                        pt = mtxtPats[i].LCase();
                     }
                     else
                     {
                        ss = s;
                        pt = mtxtPats[i];
                     }
                     if (ss.Like(pt))
                     {
                        s = s.ReplaceStr("\t"," ",1,-1,StringComparison.CurrentCulture);

                        modScanToolLib.writeRpt(rpt,filpath + "\t" + filname + "\t" + l + "\t" + s + "\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);
         }

      }
      public string contentFilter
      {
         get
         {
            return mtxtFilter;
         }
         set
         {
            mtxtFilter = value;
            mtxtPats = value.LCase().Split("|");
            mtxtPatCnt = mtxtPats.GetUpperBound(0) + 1;
         }
      }
   }
}
