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.Explorer;
  using DarkMouse.Pixiv;
  
  namespace DarkMouse
  {
      public partial class FormArtists : Form
      {
          static FormArtists _Instance;
          public static FormArtists Instance
          {
              get
              {
                  if (_Instance == null)
                  {
                      _Instance = new FormArtists();
                  }
                  return _Instance;
              }
          }
  
          List<Artists> ArtistList;
          int PageSize = 20, PageNumber = 1, TotalPages = 1, TotalCount = 0;
          Filter CurrentFilter;
  
          FormArtists()
          {
              InitializeComponent();
              ddlRating.SelectedIndex = 0;
              CurrentFilter = new Filter();
              RefreshList();
          }
  
          private void FormArtists_VisibleChanged(object sender, EventArgs e)
          {
              if (Visible)
              {
                  if (Config.GetValue<string>("BaseDirectory").Length == 0)
                  {
                      if (folderMain.ShowDialog(this) == DialogResult.OK)
                      {
                          Config.SetAndSave("BaseDirectory", folderMain.SelectedPath);
                      }
                      else
                      {
                          MessageBox.Show("You must setup the base directory to continue", "DarkMouse", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                          Hide();
                      }
                  }
              }
          }
  
          private void FormArtists_FormClosing(object sender, FormClosingEventArgs e)
          {
              e.Cancel = true;
              Hide();
          }
  
          private void btnAdd_Click(object sender, EventArgs e)
          {
              AddArtist();
          }
  
          private void btnEdit_Click(object sender, EventArgs e)
          {
              if (listArtists.SelectedIndices.Count > 0)
              {
                  EditArtist(listArtists.SelectedIndices[0]);
              }
          }
  
          private void btnRemove_Click(object sender, EventArgs e)
          {
              if (listArtists.SelectedIndices.Count > 0)
              {
                  RemoveArtist(listArtists.SelectedIndices[0]);
              }
          }
  
          private void btnFirst_Click(object sender, EventArgs e)
          {
              if (PageNumber > 1)
              {
                  PageNumber = 1;
                  RefreshList();
              }
          }
  
          private void btnPrevious_Click(object sender, EventArgs e)
          {
              if (PageNumber > 1)
              {
                  PageNumber--;
                  RefreshList();
              }
          }
  
          private void btnNext_Click(object sender, EventArgs e)
          {
              if (PageNumber < TotalPages)
              {
                  PageNumber++;
                  RefreshList();
              }
          }
  
          private void btnLast_Click(object sender, EventArgs e)
          {
              if (PageNumber < TotalPages)
              {
                  PageNumber = TotalPages;
                  RefreshList();
              }
          }
  
          private void btnNavigate_Click(object sender, EventArgs e)
          {
              JumpArtist();
          }
  
          private void btnSearch_Click(object sender, EventArgs e)
          {
              CurrentFilter = new Filter();
              if (ddlRating.SelectedIndex > 0)
              {
                  CurrentFilter.Data = new List<string>();
                  CurrentFilter.Data.Add("Rating");
                  CurrentFilter.Data.Add("=");
                  CurrentFilter.Data.Add(ddlRating.SelectedItem.ToString());
              }
              if (tbKeyword.Text.Trim().Length > 0)
              {
                  string value = "%" + tbKeyword.Text.Trim() + "%";
                  CurrentFilter.Or = new List<Filter>();
                  CurrentFilter.Or.Add(new Filter("ArtistName", "LIKE", value));
                  CurrentFilter.Or.Add(new Filter("PixivId", "LIKE", value));
                  CurrentFilter.Or.Add(new Filter("Remark", "LIKE", value));
              }
              RefreshList();
          }
  
          private void listArtists_MouseDoubleClick(object sender, MouseEventArgs e)
          {
              if (e.Button == MouseButtons.Left)
              {
                  JumpArtist();
              }
          }
  
          void RefreshList()
          {
              using (DB db = new DB())
              {
                  Dictionary<string, object> grid = Entity.FindObjectsByConditions<Artists>(db, CurrentFilter, new Pagination(PageSize, PageNumber), null);
                  ArtistList = (List<Artists>)grid["rows"];
                  TotalCount = (int)grid["total"];
                  TotalPages = TotalCount / PageSize;
                  if (TotalCount % PageSize != 0 || TotalPages == 0)
                  {
                      TotalPages++;
                  }
                  if (PageNumber > TotalPages)
                  {
                      PageNumber = TotalPages;
                  }
                  lblPager.Text = PageNumber + " / " + TotalPages + " | " + TotalCount;
                  listArtists.Items.Clear();
                  for (int i = 0; i < ArtistList.Count; i++)
                  {
                      ListViewItem item = new ListViewItem(new string[] { ArtistList[i].Id.ToString(), ArtistList[i].ArtistName, ArtistList[i].PixivId, ArtistList[i].Rating, ArtistList[i].CreationDate });
                      listArtists.Items.Add(item);
                  }
              }
          }
  
          void AddArtist()
          {
              FormArtistDetail.Instance.Reset();
              if (FormArtistDetail.Instance.ShowDialog(this) == DialogResult.OK)
              {
                  RefreshList();
              }
          }
  
          void EditArtist(int index)
          {
              FormArtistDetail.Instance.LoadArtist(ArtistList[index]);
              if (FormArtistDetail.Instance.ShowDialog(this) == DialogResult.OK)
              {
                  RefreshList();
              }
          }
  
          void RemoveArtist(int index)
          {
              if (MessageBox.Show("Are you sure to remove this artist? (Physical path WILL NOT be removed.)", "DarkMouse", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
              {
                  using (DB db = new DB())
                  {
                      Entity.Delete(db, ArtistList[index]);
                  }
                  RefreshList();
              }
          }
  
          void JumpArtist()
          {
  
gt:             if (listArtists.SelectedIndices.Count > 0)