context: using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.IO;
  using System.Text;
  using System.Windows.Forms;
  using DarkMouse.Data;
  using DarkMouse.EH;
  using DarkMouse.Explorer;
  using DarkMouse.Explorer.Zip;
  using DarkMouse.Pixiv;
  using DarkMouse.Properties;
  using DarkMouse.Util;
  
  namespace DarkMouse
  {
      public partial class FormMain : Form
      {
          public FormMain()
          {
              InitializeComponent();
  
              MouseWheel += new MouseEventHandler(FormMain_MouseWheel);
  
              InitUI();
          }
  
          private void FormMain_Shown(object sender, EventArgs e)
          {
              ItemList.Initialize();
              tmrSelection.Start();
          }
  
          private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
          {
              e.Cancel = btnPixivCancel.Enabled;
          }
  
          private void FormMain_Paint(object sender, PaintEventArgs e)
          {
              Renderer.Paint(e.Graphics, GetPaintArea());
          }
  
          private void FormMain_SizeChanged(object sender, EventArgs e)
          {
              if (WindowState == FormWindowState.Maximized)
              {
                  Config.SetValue("Maximized", "true");
              }
              else if (WindowState == FormWindowState.Normal)
              {
                  Config.SetValue("Maximized", "false");
                  Config.SetValue("WindowX", Left.ToString());
                  Config.SetValue("WindowY", Top.ToString());
                  Config.SetValue("WindowWidth", Width.ToString());
                  Config.SetValue("WindowHeight", Height.ToString());
              }
              else
              {
                  return;
              }
              Invalidate();
          }
  
          private void FormMain_LocationChanged(object sender, EventArgs e)
          {
              Config.SetValue("WindowX", Left.ToString());
              Config.SetValue("WindowY", Top.ToString());
          }
  
          private void FormMain_MouseDoubleClick(object sender, MouseEventArgs e)
          {
              if (e.Button == MouseButtons.Left && Renderer.BigMode)
              {
                  Renderer.SwitchMode();
              }
          }
  
          private void FormMain_KeyDown(object sender, KeyEventArgs e)
          {
              if (Renderer.BigMode)
              {
                  switch (e.KeyCode)
                  {
                      case Keys.Enter:
                          Renderer.SwitchMode();
                          break;
                      case Keys.Up:
                      case Keys.Down:
                      case Keys.Left:
                      case Keys.Right:
                          Renderer.Move(e.KeyCode);
                          break;
                      case Keys.PageDown:
                          ItemList.Wheel(-1);
                          break;
                      case Keys.PageUp:
                          ItemList.Wheel(1);
                          break;
                      default:
                          break;
                  }
              }
          }
  
          private void FormMain_MouseDown(object sender, MouseEventArgs e)
          {
              if (e.Button == MouseButtons.Left && Renderer.BigMode)
              {
                  Cursor.Hide();
                  Renderer.Moving = true;
                  Renderer.Start = e.Location;
              }
          }
  
          private void FormMain_MouseMove(object sender, MouseEventArgs e)
          {
              if (e.Button == MouseButtons.Left && Renderer.Moving)
              {
                  Renderer.Move(e.Location);
              }
          }
  
          private void FormMain_MouseUp(object sender, MouseEventArgs e)
          {
              if (e.Button == MouseButtons.Left)
              {
                  Cursor.Show();
                  Renderer.Moving = false;
              }
          }
  
          private void FormMain_MouseWheel(object sender, MouseEventArgs e)
          {
              if (!Renderer.BigMode)
              {
                  return;
              }
              if ((Control.ModifierKeys & Keys.Control) != 0)
              {
                  Renderer.Zoom(e.Delta);
              }
              else
              {
                  ItemList.Wheel(e.Delta);
              }
          }
  
          private void picUp_Click(object sender, EventArgs e)
          {
              ItemList.Up();
          }
  
          private void tbPath_KeyDown(object sender, KeyEventArgs e)
          {
              if (e.KeyCode == Keys.Enter)
              {
                  if (Directory.Exists(tbPath.Text.Trim()))
                  {
                      ItemList.Jump(tbPath.Text.Trim().Replace("/", "\\").TrimEnd('\\') + "\\", false);
                  }
              }
          }
  
          private void listMain_MouseDoubleClick(object sender, MouseEventArgs e)
          {
              if (e.Button == MouseButtons.Left && listMain.SelectedIndices.Count > 0)
              {
                  ItemList.Navigate(listMain.SelectedIndices[0]);
              }
          }
  
          private void listMain_KeyDown(object sender, KeyEventArgs e)
          {
              switch (e.KeyCode)
              {
                  case Keys.Enter:
                      if (listMain.SelectedIndices.Count > 0)
                      {
                          ItemList.Navigate(listMain.SelectedIndices[0]);
                      }
                      break;
                  case Keys.Delete:
                      ItemList.Remove();
                      break;
                  case Keys.N:
                      if (e.Control)
                      {
                          ItemList.NewFolder();
                      }
                      break;
                  case Keys.F2:
                      if (listMain.SelectedIndices.Count > 0)
                      {
                          listMain.SelectedItems[0].BeginEdit();
                      }
                      break;
                  case Keys.F5:
                      ItemList.Refresh();
                      break;
                  case Keys.A:
                      if (e.Control)
                      {
                          for (int i = 0; i < listMain.Items.Count; i++)
                          {
                              listMain.Items[i].Selected = true;
                          }
                      }
                      break;
                  case Keys.C:
                      if (e.Control)
                      {
                          ItemList.Copy(false);
                      }
                      break;
                  case Keys.X:
                      if (e.Control)
                      {
                          ItemList.Copy(true);
                      }
                      break;
                  case Keys.V:
                      if (e.Control)
                      {
                          ItemList.Paste();
                      }
                      break;
                  default:
                      break;
              }
          }
  
          private void listMain_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
          {
              Config.SetValue("NameWidth", listMain.Columns[0].Width.ToString());
              Config.SetValue("SizeWidth", listMain.Columns[1].Width.ToString());
          }
  
          private void listMain_SelectedIndexChanged(object sender, EventArgs e)
          {
              SetSelectionInfo();
          }
  
          private void listMain_AfterLabelEdit(object sender, LabelEditEventArgs e)
          {
              e.CancelEdit = true;
              if (ZipUtil.InZip || e.Item < 0 || e.Label == null || e.Label.Trim().Length == 0 || e.Label.Trim().Equals(ItemList.Items[e.Item].Name, StringComparison.CurrentCultureIgnoreCase))
              {
                  return;
              }
              string name = e.Label.Trim();
              if (!ItemList.Items[e.Item].IsFile)
              {
                  if (!Directory.Exists(ItemList.DirPath + name))
                  {
                      Directory.Move(ItemList.DirPath + ItemList.Items[e.Item].Name, ItemList.DirPath + name);
                  }
              }
              else
              {
                  if (!File.Exists(ItemList.DirPath + name))
                  {
                      File.Move(ItemList.DirPath + ItemList.Items[e.Item].Name, ItemList.DirPath + name);
                  }
              }
          }
  
          private void btnOptions_Click(object sender, EventArgs e)
          {
              FormOptions.Instance.ShowDialog(this);
          }
  
          private void btnNewFolder_Click(object sender, EventArgs e)
          {
              ItemList.NewFolder();
          }
  
          private void btnRename_Click(object sender, EventArgs e)
          {
              if (listMain.SelectedIndices.Count > 0)
              {
                  listMain.SelectedItems[0].BeginEdit();
              }
          }
  
          private void btnRemove_Click(object sender, EventArgs e)
          {
              ItemList.Remove();
          }
  
          private void btnCut_Click(object sender, EventArgs e)
          {
              ItemList.Copy(true);
          }
  
          private void btnCopy_Click(object sender, EventArgs e)
          {
              ItemList.Copy(false);
          }
  
          private void btnPaste_Click(object sender, EventArgs e)
          {
              ItemList.Paste();
          }
  
          private void btnSearch_Click(object sender, EventArgs e)
          {
              FormSearch.Instance.Show();
          }
  
          private void btnZoomOut_Click(object sender, EventArgs e)
          {
              if (Renderer.BigMode)
              {
                  Renderer.Zoom(-1);
              }
          }
  
          private void btnZoomIn_Click(object sender, EventArgs e)
          {
              if (Renderer.BigMode)
              {
                  Renderer.Zoom(1);
              }
          }
  
          private void lblAutoSize_Click(object sender, EventArgs e)
          {
              btnZoomOut.Enabled = Renderer.Auto;
              btnZoomIn.Enabled = Renderer.Auto;
              Renderer.Auto = !Renderer.Auto;
              Renderer.OffsetX = 0;
              Renderer.OffsetY = 0;
              if (Renderer.Auto)
              {
                  lblAutoSize.ForeColor = Color.Green;
              }
              else
              {
                  Renderer.Scale = 1;
                  lblAutoSize.ForeColor = Color.Black;
              }
              if (Renderer.BigMode)
              {
                  Invalidate();
              }
          }
  
          private void btnPlay_Click(object sender, EventArgs e)
          {
              if (tmrAnime.Enabled)
              {
                  Animator.Playing = !Animator.Playing;
                  SetPlayStatus();
              }
          }
  
          private void btnForward_Click(object sender, EventArgs e)
          {
              if (tmrAnime.Enabled)
              {
                  Animator.Forward = !Animator.Forward;
                  SetPlayStatus();
              }
          }
  
          private void btnPixiv_ButtonClick(object sender, EventArgs e)
          {
              if (btnPixivCancel.Enabled)
              {
                  return;
              }
              Artists artist = Artists.FindArtist();
              if (artist != null)
              {
                  PixivQuest quest = new PixivQuest();
                  quest.Artist = artist;
                  quest.From = 0;
                  long to = 0;
                  long.TryParse(artist.LatestCG, out to);
                  quest.To = to;
                  quest.Tag = "";
                  quest.DownloadPath = ItemList.DirPath;
                  quest.DownloadBreak = Config.GetValue<int>("DownloadBreak");
                  PixivDownloader.Quest = quest;
                  PixivDownloader.StartDownload();
              }
          }
  
          private void miPixivLogin_Click(object sender, EventArgs e)
          {
              SetPixivStatus(true);
              PixivDownloader.StartLogin();
          }
  
          private void miPixivDownload_Click(object sender, EventArgs e)
          {
              Artists artist = Artists.FindArtist();
              if (artist != null)
              {
                  FormPixivDownload.Instance.InitDownload(artist);
                  FormPixivDownload.Instance.ShowDialog(this);
              }
          }
  
          private void miBatchDownload_Click(object sender, EventArgs e)
          {
              if (ItemList.DirPath.Length == 0 || ZipUtil.InZip)
              {
                  return;
              }
              FormPixivDownload.Instance.InitBatch();
              FormPixivDownload.Instance.ShowDialog(this);
          }
  
          private void miDatabase_Click(object sender, EventArgs e)
          {
              FormArtists.Instance.Show();
          }
  
          private void miPixivCheck_Click(object sender, EventArgs e)
          {
              Artists artist = Artists.FindArtist();
              if (artist != null)
              {
                  PixivQuest quest = new PixivQuest();
                  quest.Artist = artist;
                  quest.DownloadPath = ItemList.DirPath;
                  PixivDownloader.Quest = quest;
                  PixivDownloader.StartCheck();
              }
          }
  
          private void miNovels_Click(object sender, EventArgs e)
          {
              Artists artist = Artists.FindArtist();
              if (artist != null)
              {
                  FormPixivNovels.Instance.SetArtist(artist);
                  FormPixivNovels.Instance.Show();
              }
          }
  
          private void btnPixivOverview_Click(object sender, EventArgs e)
          {
              Artists artist = Artists.FindArtist();
              if (artist != null)
              {
                  FormPixivOverview.Instance.Show();
                  FormPixivOverview.Instance.SetArtist(artist);
              }
          }
  
          private void btnPixivNovel_Click(object sender, EventArgs e)
          {
              Artists artist = Artists.FindArtist();
              if (artist != null)
              {
                  using (DB db = new DB())
                  {
                      long to = 0;
                      if (MessageBox.Show("Force the downloader to check all of the artist's novels?", "DarkMouse", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                      {
                          object obj = db.GetData("SELECT MAX([NovelId]) AS MID FROM [ArtistNovels] WHERE [PixivId] = '" + artist.PixivId + "'");
                          if (obj != null)
                          {
                              to = long.Parse(obj.ToString());
                          }
                      }
                      PixivQuest quest = new PixivQuest();
                      quest.Artist = artist;
                      quest.To = to;
                      quest.DownloadBreak = Config.GetValue<int>("DownloadBreak");
                      PixivDownloader.Quest = quest;
                      PixivDownloader.StartDownloadNovels();
                  }
              }
          }
  
          private void btnPixivCancel_Click(object sender, EventArgs e)
          {
              PixivDownloader.Cancel = true;
          }
  
          private void btnPack_Click(object sender, EventArgs e)
          {
              if (ZipUtil.InZip || listMain.SelectedIndices.Count == 0 || !ItemList.Items[listMain.SelectedIndices[0]].IsFile || !FileUtil.IsFileAImage(ItemList.Items[listMain.SelectedIndices[0]].Name) || !FileUtil.IsFileAMangaImage(ItemList.Items[listMain.SelectedIndices[0]].Name))
              {
                  return;
              }
              try
              {
                  string temp = Guid.NewGuid().ToString().Replace("-", "");
                  Directory.CreateDirectory(ItemList.DirPath + temp);
                  if (!Directory.Exists(ItemList.DirPath + temp))
                  {
                      MessageBox.Show("I/O error", "DarkMouse", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                      return;
                  }
                  List<int> id = FileUtil.GetIntArray(ItemList.Items[listMain.SelectedIndices[0]].Name);
                  List<FileInfo> files = new List<FileInfo>();
                  foreach (string image in Directory.GetFiles(ItemList.DirPath, id[0] + "_*", SearchOption.TopDirectoryOnly))
                  {
                      FileInfo fi = new FileInfo(image);
                      fi.MoveTo(ItemList.DirPath + temp + "\\" + fi.Name);
                      files.Add(fi);
                  }
                  int p = 0;
                  List<FileInfo> nfiles = FormBatchRename.GetDefaultNames(files, out p);
                  for (int i = 0; i < files.Count; i++)
                  {
                      File.Move(ItemList.DirPath + temp + "\\" + files[i].Name, ItemList.DirPath + temp + "\\" + nfiles[i].Name);
                  }
                  Archiver.Compress(ItemList.DirPath + temp + "\\", ItemList.DirPath + temp + "\\" + id[0] + ".zip");
                  File.Move(ItemList.DirPath + temp + "\\" + id[0] + ".zip", ItemList.DirPath + id[0] + ".zip");
                  Directory.Delete(ItemList.DirPath + temp);
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Error: " + ex.Message, "DarkMouse", MessageBoxButtons.OK, MessageBoxIcon.Warning);
              }
          }
  
          private void btnProfile_Click(object sender, EventArgs e)
          {
              Artists artist = Artists.FindArtist();
              if (artist != null)
              {
                  FormArtistDetail.Instance.LoadArtist(artist);
                  FormArtistDetail.Instance.ShowDialog(this);
              }
          }
  
          private void btnLocate_Click(object sender, EventArgs e)
          {
              Artists artist = null;
              if (Clipboard.ContainsText())
              {
                  List<int> list = FileUtil.GetIntArray(Clipboard.GetText());
                  if (list.Count > 0)
                  {
                      artist = Artists.FindArtist(list[list.Count - 1].ToString(), true);
                  }
              }
              if (artist == null)
              {
                  FormInput.Instance.Init("Type the keyword below:", "");
                  if (FormInput.Instance.ShowDialog(this) == DialogResult.OK)
                  {
                      artist = Artists.FindArtist(FormInput.Instance.Data, false);
                  }
              }
              if (artist != null)
              {
                  string path = Config.GetValue<string>("BaseDirectory") + artist.Rating + "\\" + artist.ArtistName + "\\";
                  if (Directory.Exists(path + "CG\\"))
                  {
                      path += "CG\\";
                  }
                  if (Directory.Exists(path))
                  {
                      ItemList.Jump(path, true);
                  }
                  else
                  {
                      MessageBox.Show("Can't find: " + path, "DarkMouse", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                  }
              }
          }
  
          private void btnEH_ButtonClick(object sender, EventArgs e)
          {
              if (btnEHCancel.Enabled)
              {
                  return;
              }
              if (Config.GetValue<string>("EHMemberId").Length == 0 || Config.GetValue<string>("EHPassHash").Length == 0)
              {
                  MessageBox.Show("Login first", "DarkMouse", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                  return;
              }
              if (ItemList.DirPath.Length == 0 || ZipUtil.InZip)
              {
                  return;
              }
              FormEHDownload.Instance.ShowDialog(this);
          }
  
          private void miEHLogin_Click(object sender, EventArgs e)
          {
              SetEHStatus(true);
              EHDownloader.StartLogin();
          }
  
          private void miEHRedirect_Click(object sender, EventArgs e)
          {
              if (EHDownloader.Quest != null && Directory.Exists(EHDownloader.Quest.DownloadPath))
              {
                  ItemList.Jump(EHDownloader.Quest.DownloadPath, true);
              }
          }
  
          private void btnEHCancel_Click(object sender, EventArgs e)
          {
              EHDownloader.Cancel = true;
          }
  
          private void btnBatchRename_Click(object sender, EventArgs e)
          {
              if (ZipUtil.InZip || (listMain.SelectedIndices.Count == 0 && listMain.Items.Count == 0))
              {
                  return;
              }
              List<FileInfo> files = new List<FileInfo>();
              if (listMain.SelectedIndices.Count == 0)
              {
                  for (int i = 0; i < ItemList.Items.Count; i++)
                  {
                      if (ItemList.Items[i].IsFile)
                      {
                          files.Add(new FileInfo(ItemList.Items[i].Name));
                      }
                  }
              }
              else
              {
                  for (int i = 0; i < listMain.SelectedIndices.Count; i++)
                  {
                      if (ItemList.Items[listMain.SelectedIndices[i]].IsFile)
                      {
                          files.Add(new FileInfo(ItemList.Items[listMain.SelectedIndices[i]].Name));
                      }
                  }
              }
  
              if (files.Count < 2)
              {
                  return;
              }
  
              FormBatchRename.Instance.InitFiles(files);
              FormBatchRename.Instance.ShowDialog(this);
          }
  
          private void btnExtract_Click(object sender, EventArgs e)
          {
              if (ZipUtil.InZip || listMain.SelectedIndices.Count == 0 || !ItemList.Items[listMain.SelectedIndices[0]].IsFile || !FileUtil.IsFileAArchive(ItemList.Items[listMain.SelectedIndices[0]].Name))
              {
                  return;
              }
              try
              {
                  string temp = "Temp";
                  if (Directory.Exists(ItemList.DirPath + temp))
                  {
                      int count = 1;
                      while (Directory.Exists(ItemList.DirPath + temp + "(" + count + ")"))
                      {
                          count++;
                      }
                      temp = "Temp" + "(" + count + ")";
                  }
                  Directory.CreateDirectory(ItemList.DirPath + temp);
                  Archiver.Extract(ItemList.DirPath + ItemList.Items[listMain.SelectedIndices[0]].Name, ItemList.DirPath + temp + "\\");
                  ItemList.Jump(ItemList.DirPath + temp + "\\", false);
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Error: " + ex.Message, "DarkMouse", MessageBoxButtons.OK, MessageBoxIcon.Warning);
              }
          }
  
          private void btnCompress_Click(object sender, EventArgs e)
          {
              if (ZipUtil.InZip || ItemList.Items.Count == 0 || MessageBox.Show("Press Yes to continue", "DarkMouse", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
              {
                  return;
              }
              try
              {
                  Archiver.Compress(ItemList.DirPath, ItemList.DirPath + "Temp.zip");
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Error: " + ex.Message, "DarkMouse", MessageBoxButtons.OK, MessageBoxIcon.Warning);
              }
          }
  
          private void btnPdf_Click(object sender, EventArgs e)
          {
              if (listMain.SelectedIndices.Count == 0 || ZipUtil.InZip)
              {
                  return;
              }
              string pdf = "";
              List<string> files = new List<string>();
              for (int i = 0; i < listMain.SelectedIndices.Count; i++)
              {
                  Item item = ItemList.Items[listMain.SelectedIndices[i]];
                  if (item.IsFile && item.Name.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase) && pdf.Length == 0)
                  {
                      pdf = ItemList.DirPath + item.Name;
                  }
                  if (item.IsFile && FileUtil.IsFileAImage(item.Name))
                  {
                      files.Add(ItemList.DirPath + item.Name);
                  }
              }
              if (pdf.Length == 0 && files.Count == 0)
              {
                  MessageBox.Show("Select a pdf file or some image files first", "DarkMouse", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                  return;
              }
              else if (pdf.Length > 0)
              {
                  FormPdf.Instance.InitExport(pdf);
              }
              else
              {
                  FormPdf.Instance.InitGenerate(files);
              }
              FormPdf.Instance.ShowDialog(this);
          }
  
          private void btnGIF_Click(object sender, EventArgs e)
          {
  
gt:             if (listMain.SelectedIndices.Count == 0)