context: using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Collections.Specialized;
  using System.Drawing;
  using System.IO;
  using System.Text;
  using System.Windows.Forms;
  using DarkMouse.Explorer.Zip;
  using DarkMouse.Util;
  
  namespace DarkMouse.Explorer
  {
      public class ItemList
      {
          public static string DirPath = "";
          public static List<Item> Items = new List<Item>();
  
          public static string LatestPath = "";
          public static string LatestFile = "";
          public static bool Updating = false;
  
          public static int DirCount = 0;
          public static int FileCount = 0;
          public static long SizeCount = 0;
  
          public static void Initialize()
          {
              DirPath = Config.GetValue<string>("LatestPath");
              LatestPath = DirPath;
              LatestFile = "";
              if (DirPath.Length > 0 && !Directory.Exists(DirPath))
              {
                  DirPath = "";
                  LatestPath = "";
                  Config.SetValue("LatestPath", DirPath);
                  Config.SetValue("LatestFile", "");
              }
  
              Refresh();
  
              FormMain.Instance.Select(Config.GetValue<string>("LatestFile"));
          }
  
          public static void Navigate(int index)
          {
              Item item = (Item)Items[index];
  
              if (item.IsFile)
              {
                  if (FileUtil.IsFileAImage(item.Name))
                  {
                      Renderer.SwitchMode();
                  }
                  else if (item.Name.EndsWith(".frames", StringComparison.CurrentCultureIgnoreCase) && !ZipUtil.InZip)
                  {
                      if (!FormMain.Instance.GetTimer().Enabled)
                      {
                          Animator.InitUgoira();
                      }
                      Renderer.SwitchMode();
                  }
                  else if (item.Name.EndsWith(".zip", StringComparison.CurrentCultureIgnoreCase) && !ZipUtil.InZip)
                  {
                      LoadResult result = ZipUtil.Load(DirPath + item.Name, "", false);
                      if (result == LoadResult.NoPassword)
                      {
                          FormInput.Instance.Init("Please type the password below:", "");
                          if (FormInput.Instance.ShowDialog(FormMain.Instance) == DialogResult.OK)
                          {
                              result = ZipUtil.Load(DirPath + item.Name, FormInput.Instance.Data, false);
                              if (result != LoadResult.Success)
                              {
                                  MessageBox.Show("Password incorrect", "DarkMouse", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                  return;
                              }
                          }
                      }
                      else if (result != LoadResult.Success)
                      {
                          MessageBox.Show("Open file error", "DarkMouse", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                          return;
                      }
                      ZipUtil.InZip = true;
                      Refresh();
                  }
                  else
                  {
                      FileUtil.ShellOpen(DirPath, item.Name);
                  }
              }
              else
              {
                  if (ZipUtil.InZip)
                  {
                      ZipUtil.ZipPath += item.Name + "\\";
                  }
                  else
                  {
                      if (!Directory.Exists(DirPath + item.Name.TrimEnd('\\') + "\\"))
                      {
                          MessageBox.Show("Can't open selected directory", "DarkMouse", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                          return;
                      }
                      DirPath = DirPath + item.Name.TrimEnd('\\') + "\\";
                  }
                  Refresh();
              }
          }
  
          public static void Up()
          {
              string parent;
              if (!ZipUtil.InZip)
              {
                  if (DirPath.Length > 0)
                  {
                      int p = DirPath.TrimEnd('\\').LastIndexOf('\\');
                      if (p < 0)
                      {
                          parent = DirPath;
                          DirPath = "";
                      }
                      else
                      {
                          parent = DirPath.Substring(p + 1).TrimEnd('\\');
                          DirPath = DirPath.Substring(0, p + 1);
                      }
                      if (DirPath.Length > 0 && !Directory.Exists(DirPath))
                      {
                          DirPath = "";
                          parent = "";
                      }
                  }
                  else
                  {
                      return;
                  }
              }
              else
              {
                  if (ZipUtil.ZipPath.Length == 0)
                  {
                      parent = ZipUtil.FileName;
                      ZipUtil.InZip = false;
                      ZipUtil.FileName = "";
                      ZipUtil.Dispose();
                      if (!Directory.Exists(DirPath))
                      {
                          DirPath = "";
                          parent = "";
                      }
                  }
                  else
                  {
                      int p = ZipUtil.ZipPath.TrimEnd('\\').LastIndexOf('\\');
                      if (p < 0)
                      {
                          parent = ZipUtil.ZipPath.TrimEnd('\\');
                          ZipUtil.ZipPath = "";
                      }
                      else
                      {
                          parent = ZipUtil.ZipPath.Substring(p + 1).TrimEnd('\\');
                          ZipUtil.ZipPath = ZipUtil.ZipPath.Substring(0, p + 1);
                      }
                  }
              }
              Refresh();
              FormMain.Instance.Select(parent);
          }
  
          public static void Jump(string path, bool focusLast)
          {
              if (DirPath.StartsWith(path, StringComparison.CurrentCultureIgnoreCase))
              {
                  return;
              }
  
              if (ZipUtil.InZip)
              {
                  ZipUtil.InZip = false;
                  ZipUtil.FileName = "";
                  ZipUtil.Dispose();
              }
              DirPath = path;
              Refresh();
  
              if (!focusLast)
              {
                  FormMain.Instance.SetSelectionInfo();
              }
              else
              {
                  int p = Items.Count - 1;
                  if (Renderer.BigMode)
                  {
                      for (int i = Items.Count - 1; i >= 0; i--)
                      {
                          if (Items[i].IsFile && FileUtil.IsFileAImage(Items[i].Name))
                          {
                              p = i;
                              break;
                          }
                      }
                  }
                  if (p >= 0)
                  {
                      if (Renderer.BigMode && !FileUtil.IsFileAImage(Items[p].Name))
                      {
                          Renderer.SwitchMode();
                      }
                      FormMain.Instance.Select(p);
                  }
              }
          }
  
          public static void Select(int index)
          {
              Item item = (Item)Items[index];
  
              if (FileUtil.IsFileAImage(item.Name) && item.IsFile)
              {
                  try
                  {
                      if (!ZipUtil.InZip)
                      {
                          Renderer.ImageStream = new MemoryStream(File.ReadAllBytes(DirPath + item.Name));
                          Renderer.Image = Image.FromStream(Renderer.ImageStream);
                      }
                      else
                      {
                          Renderer.Image = ZipUtil.GetImage(item.Name);
                      }
                      if (Renderer.Image != null && ImageAnimator.CanAnimate(Renderer.Image))
                      {
                          Animator.InitGif();
                      }
                  }
                  catch (Exception)
                  {
                      Renderer.Dispose();
                  }
              }
              else if (item.Name.EndsWith(".frames", StringComparison.CurrentCultureIgnoreCase) && !ZipUtil.InZip && item.IsFile)
              {
                  if (Renderer.BigMode || Config.GetValue<bool>("EnableUgoira"))
                  {
                      Animator.InitUgoira();
                  }
              }
          }
  
          public static void Wheel(int delta)
          {
              int current = FormMain.Instance.GetListView().SelectedIndices[0];
              int index = current;
              if (delta > 0)
              {
                  for (int i = current - 1; i >= 0; i--)
                  {
                      if (Items[i].IsFile && (FileUtil.IsFileAImage(Items[i].Name) || (Items[i].Name.EndsWith(".frames", StringComparison.CurrentCultureIgnoreCase) && Config.GetValue<bool>("EnableUgoira"))))
                      {
                          index = i;
                          break;
                      }
                  }
              }
              else
              {
                  for (int i = current + 1; i < Items.Count; i++)
                  {
                      if (Items[i].IsFile && (FileUtil.IsFileAImage(Items[i].Name) || (Items[i].Name.EndsWith(".frames", StringComparison.CurrentCultureIgnoreCase) && Config.GetValue<bool>("EnableUgoira"))))
                      {
                          index = i;
                          break;
                      }
                  }
              }
              if (index != current)
              {
                  FormMain.Instance.Select(index);
              }
          }
  
          public static void Refresh()
          {
              Watcher.Stop();
  
              DirCount = 0;
              FileCount = 0;
              SizeCount = 0;
  
              Items.Clear();
              FormMain.Instance.GetListView().Items.Clear();
  
              if (!ZipUtil.InZip)
              {
                  if (DirPath.Length == 0)
                  {
                      DriveInfo[] dis = DriveInfo.GetDrives();
                      foreach (DriveInfo di in dis)
                      {
                          AddDrive(di.Name);
                      }
                  }
                  else
                  {
                      DirectoryInfo parent = new DirectoryInfo(DirPath);
                      DirectoryInfo[] dis = parent.GetDirectories();
                      foreach (DirectoryInfo di in dis)
                      {
                          if ((di.Attributes & FileAttributes.System) != 0)
                          {
                              continue;
                          }
                          AddDir(di.Name);
                      }
                      FileInfo[] fis = parent.GetFiles();
                      foreach (FileInfo fi in fis)
                      {
                          if ((fi.Attributes & FileAttributes.System) != 0 && !fi.Name.Equals("thumbs.db", StringComparison.CurrentCultureIgnoreCase))
                          {
                              continue;
                          }
                          AddFile(fi);
                      }
                  }
              }
              else
              {
                  foreach (Entry entry in ZipUtil.GetList())
                  {
                      if (!entry.IsFile)
                      {
                          AddDir(entry.Name);
                      }
                      else
                      {
                          AddFile(entry);
                      }
                  }
              }
  
              Items.Sort();
              for (int i = 0; i < Items.Count; i++)
              {
                  AddItemToListView(Items[i]);
              }
  
              FormMain.Instance.SetExplorerInfo();
              Watcher.Start();
          }
  
          public static void AddDrive(string name)
          {
              Item item = new Item();
              item.Name = name;
              item.IconIndex = IconUtil.GetDrive(item.Name);
              Items.Add(item);
              DirCount++;
          }
  
          public static void AddDir(string name)
          {
              Item item = new Item();
              item.Name = name;
              item.IconIndex = 0;
              Items.Add(item);
              DirCount++;
          }
  
          public static void AddFile(FileInfo fi)
          {
              Item item = new Item();
              item.Name = fi.Name;
              try
              {
  
                  item.RealSize = fi.Length;
              }
              catch (Exception)
              {
                  item.RealSize = 0;
              }
              try
              {
                  item.IconIndex = IconUtil.GetFile(fi.FullName);
              }
              catch (Exception)
              {
                  item.IconIndex = IconUtil.GetExtension(fi.FullName);
              }
              item.Size = FileUtil.GetSizeString(item.RealSize, 2);
              item.IsFile = true;
              Items.Add(item);
              FileCount++;
              SizeCount += item.RealSize;
          }
  
          public static void AddFile(Entry entry)
          {
              Item item = new Item();
              item.Name = entry.Name;
              item.Size = FileUtil.GetSizeString(entry.Size, 2);
              item.IconIndex = IconUtil.GetExtension(entry.Name);
              item.IsFile = true;
              item.RealSize = entry.Size;
              Items.Add(item);
              FileCount++;
              SizeCount += entry.Size;
          }
  
          public static void AddItemToListView(Item item)
          {
              ListViewItem lvi = new ListViewItem(new string[] { item.Name, item.Size });
              lvi.ImageIndex = item.IconIndex;
              if (item.IsFile)
              {
                  Color bgColor = Color.White;
                  Color fgColor = Color.Black;
                  FileInfo fi = new FileInfo(item.Name);
                  switch (fi.Extension.ToLower())
                  {
                      case ".png":
  
gt:                         bgColor = Color.FromArgb(232, 222, 220);