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.Explorer;
  using DarkMouse.Pixiv;
  using FMOD;
  
  namespace DarkMouse
  {
      public partial class FormPixivOverview : Form
      {
          static FormPixivOverview _Instance;
          public static FormPixivOverview Instance
          {
              get
              {
                  if (_Instance == null)
                  {
                      _Instance = new FormPixivOverview();
                  }
                  return _Instance;
              }
          }
  
          public delegate void InvokeUIDelegate(params string[] param);
  
          public void InvokeUI(params string[] param)
          {
              if (InvokeRequired)
              {
                  InvokeUIDelegate invokeUIDelegate = new InvokeUIDelegate(InvokeUI);
                  BeginInvoke(invokeUIDelegate, new object[] { param });
                  return;
              }
              bool enableRing = Config.GetValue<bool>("EnableRing");
              switch (param[0])
              {
                  case "PixivInfo":
                      lblStatus.Text = param[1];
                      break;
                  case "PixivDone":
                      if (enableRing)
                      {
                          FMODExWrapper.PlaySound("DoneRing");
                      }
                      SetPixivStatus(false);
                      break;
                  case "PixivStop":
                      if (enableRing)
                      {
                          FMODExWrapper.PlaySound("StopRing");
                      }
                      SetPixivStatus(false);
                      break;
                  case "AppendItem":
                      int offsetX = pnlOverview.DisplayRectangle.X;
                      int offsetY = pnlOverview.DisplayRectangle.Y;
                      long id = long.Parse(param[1]);
                      int cols = (pnlOverview.Width - 20) / 153;
                      int x = Ids.Count % cols;
                      int y = Ids.Count / cols;
                      Control[] ctls = new Control[3];
                      PictureBox pic = new PictureBox();
                      pic.Name = "pic" + id;
                      pic.Bounds = new Rectangle(153 * x + 3 + offsetX, 178 * y + 3 + offsetY, 150, 150);
                      pic.SizeMode = PictureBoxSizeMode.CenterImage;
                      pic.Image = Image.FromStream(Works[id]);
                      ctls[0] = pic;
                      pnlOverview.Controls.Add(pic);
                      CheckBox chk = new CheckBox();
                      chk.Name = "chk" + id;
                      chk.Bounds = new Rectangle(153 * x + 3 + offsetX, 178 * y + 159 + offsetY, 15, 14);
                      ctls[1] = chk;
                      pnlOverview.Controls.Add(chk);
                      Label lbl = new Label();
                      lbl.Name = "lbl" + id;
                      lbl.Bounds = new Rectangle(153 * x + 24 + offsetX, 178 * y + 161 + offsetY, 131, 12);
                      if (WorkTypes[id] == CGType.Single)
                      {
                          lbl.Text = "Single " + id;
                      }
                      else if (WorkTypes[id] == CGType.Manga)
                      {
                          lbl.Text = "Multiple " + id;
                          pic.Cursor = Cursors.Hand;
                          pic.Click += new EventHandler(Manga_Click);
                      }
                      else
                      {
                          lbl.Text = "Ugoku " + id;
                      }
                      ctls[2] = lbl;
                      pnlOverview.Controls.Add(lbl);
                      Ids.Add(id);
                      DisplayItems[id] = ctls;
                      break;
                  default:
                      break;
              }
          }
  
          public static Dictionary<long, MemoryStream> Works = new Dictionary<long, MemoryStream>();
          public static Dictionary<long, List<MemoryStream>> Mangas = new Dictionary<long, List<MemoryStream>>();
          public static Dictionary<long, CGType> WorkTypes = new Dictionary<long, CGType>();
  
          Artists Artist;
          List<long> Ids = new List<long>();
          Dictionary<long, Control[]> DisplayItems = new Dictionary<long, Control[]>();
  
          FormPixivOverview()
          {
              InitializeComponent();
          }
  
          private void FormPixivOverview_SizeChanged(object sender, EventArgs e)
          {
              if (pnlOverview.Width == 0)
              {
                  return;
              }
              int offsetX = pnlOverview.DisplayRectangle.X;
              int offsetY = pnlOverview.DisplayRectangle.Y;
              int cols = (pnlOverview.Width - 20) / 153;
              for (int i = 0; i < Ids.Count; i++)
              {
                  int x = i % cols;
                  int y = i / cols;
                  Control[] ctls = DisplayItems[Ids[i]];
                  ctls[0].Location = new Point(153 * x + 3 + offsetX, 178 * y + 3 + offsetY);
                  ctls[1].Location = new Point(153 * x + 3 + offsetX, 178 * y + 159 + offsetY);
                  ctls[2].Location = new Point(153 * x + 24 + offsetX, 178 * y + 161 + offsetY);
              }
          }
  
          private void FormPixivOverview_FormClosing(object sender, FormClosingEventArgs e)
          {
              e.Cancel = true;
              Hide();
          }
  
          private void btnSynchronize_Click(object sender, EventArgs e)
          {
              StartWork(false);
          }
  
          private void btnViewAll_Click(object sender, EventArgs e)
          {
              StartWork(true);
          }
  
          private void btnCancel_Click(object sender, EventArgs e)
          {
              PixivDownloader.Cancel = true;
          }
  
          private void btnDownload_Click(object sender, EventArgs e)
          {
              if (Ids.Count > 0)
              {
                  List<long> list = new List<long>();
                  for (int i = 0; i < Ids.Count; i++)
                  {
                      CheckBox chk = (CheckBox)DisplayItems[Ids[i]][1];
                      if (chk.Checked)
                      {
                          list.Add(long.Parse(chk.Name.Substring(3)));
                      }
                  }
                  FormMain.Instance.SetPixivStatus(true);
                  PixivQuest quest = new PixivQuest();
                  quest.Artist = Artist;
                  quest.Maximum = Ids[0];
                  quest.BatchList = list;
                  quest.DownloadPath = ItemList.DirPath;
                  quest.DownloadBreak = Config.GetValue<int>("DownloadBreak");
                  PixivDownloader.Quest = quest;
                  PixivDownloader.StartDownload();
              }
              Hide();
          }
  
          private void btnClear_Click(object sender, EventArgs e)
          {
              Ids.Clear();
              DisplayItems.Clear();
              pnlOverview.Controls.Clear();
              Works.Clear();
              Mangas.Clear();
              WorkTypes.Clear();
          }
  
          public void Manga_Click(object sender, EventArgs e)
          {
              PictureBox pic = (PictureBox)sender;
              long id = long.Parse(pic.Name.Substring(3));
              FormPixivManga.Instance.SetManga(id);
              FormPixivManga.Instance.Display();
              FormPixivManga.Instance.ShowDialog(this);
          }
  
          public void SetPixivStatus(bool working)
          {
              FormMain.Instance.SetPixivStatus(working);
              btnSynchronize.Enabled = !working;
              btnViewAll.Enabled = !working;
              btnCancel.Enabled = working;
              btnDownload.Enabled = !working;
              btnClear.Enabled = !working;
          }
  
          public void SetArtist(Artists artist)
          {
  
gt:             if (Artist != null && Artist.PixivId.Equals(artist.PixivId))