using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using TD.SandDock;
using TD.SandDock.Rendering;
using VKontakte;

namespace Gsome
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			this.InitializeComponent();
			this.Text = string.Format("ГСОМЕ v{0} альфа (by exe-dealer)", Application.ProductVersion);
			this.trayIcon.Icon = base.Icon;
			foreach (VKClient vkclient in VKClients.Items)
			{
				this.lstAccounts.Nodes.Add(vkclient.Account.ToString()).Checked = true;
			}
			this.tmrCounters.Start();
		}

		private void btnCreatePageActivate_Click(object sender, EventArgs e)
		{
			this.tpCreate.Activate();
		}

		private void tmrCounters_Tick(object sender, EventArgs e)
		{
			AttackControl attackControl = this.tpconMain.ActiveDocument as AttackControl;
			if (attackControl != null && !attackControl.IsPrepearing)
			{
				this.lblWall.Text = attackControl.WallMessagesCount.ToString().PadLeft(5);
				this.lblTopics.Text = attackControl.TopicsCount.ToString().PadLeft(5);
				this.lblTopicsAnswers.Text = attackControl.TopicsAnswersCount.ToString().PadLeft(5);
				this.lblAlbums.Text = attackControl.AlbumsCount.ToString().PadLeft(5);
				this.lblAlbumsPhotos.Text = attackControl.PhotosCount.ToString().PadLeft(5);
				this.lblAudio.Text = attackControl.AudiosCount.ToString().PadLeft(5);
				this.lblVideos.Text = attackControl.VideosCount.ToString().PadLeft(5);
				this.lblVideosComments.Text = attackControl.VideosCommentsCount.ToString().PadLeft(5);
			}
			else
			{
				this.lblWall.Text = "  -  ";
				this.lblTopics.Text = "  -  ";
				this.lblTopicsAnswers.Text = "  -  ";
				this.lblAlbums.Text = "  -  ";
				this.lblAlbumsPhotos.Text = "  -  ";
				this.lblAudio.Text = "  -  ";
				this.lblVideos.Text = "  -  ";
				this.lblVideosComments.Text = "  -  ";
			}
			if (attackControl != null && attackControl.IsPrepearing)
			{
				this.pbPrepearing.Visible = true;
				this.pbPrepearing.Value = attackControl.PrepearingProgress;
				this.tmrCounters.Interval = 50;
				return;
			}
			this.pbPrepearing.Visible = false;
			this.tmrCounters.Interval = 200;
		}

		private void btnClearCach_Click(object sender, EventArgs e)
		{
			foreach (VKClient vkclient in VKClients.Items)
			{
				vkclient.ClearCach();
			}
		}

		private void btnCreate_Click(object sender, EventArgs e)
		{
			string text = this.txtAimId.Text;
			int num = 0;
			Match match = Regex.Match(text, "http://vkontakte\\.ru/club([\\p{Nd}]{1,8})");
			if (!match.Success || !int.TryParse(match.Groups[1].Value, out num))
			{
				match = Regex.Match(text, "щачло_попячтса([\\p{Nd}]{1,8})");
				if (match.Success && int.TryParse(match.Groups[1].Value, out num))
				{
					num *= -1;
				}
				else
				{
					if (!int.TryParse(text.Trim(), out num))
					{
						this.errProvider.SetError(this.txtAimId, "ID группы введен не верно");
						return;
					}
					num = Math.Abs(num);
				}
			}
			if (num == 0)
			{
				this.errProvider.SetError(this.txtAimId, "ID группы введен не верно");
				return;
			}
			num *= -1;
			List<VKClient> list = new List<VKClient>();
			for (int i = 0; i < this.lstAccounts.Nodes.Count; i++)
			{
				if (this.lstAccounts.Nodes[i].Checked)
				{
					list.Add(VKClients.Items[i]);
				}
			}
			if (list.Count == 0)
			{
				this.errProvider.SetError(this.lstAccounts, "Ни один аккаунт не выбран.");
				return;
			}
			try
			{
				AttackControl attackControl = AttackControl.Create(num, list);
				this.tpconMain.AddDocument(attackControl);
				attackControl.Activate();
				attackControl.Prepare();
			}
			catch (BlankErrorException ex)
			{
				this.errProvider.SetError(this.txtAimId, ex.Message);
			}
		}

		private void tpconMain_ActiveDocumentChanged(object sender, ActiveDocumentEventArgs e)
		{
			this.btnCreatePageActivate.Enabled = (e.NewActiveDocument != this.tpCreate);
		}

		private void txtAimId_TextChanged(object sender, EventArgs e)
		{
			if (this.errProvider.GetError(this.txtAimId) != null)
			{
				this.errProvider.SetError(this.txtAimId, null);
			}
		}

		private void lstAccounts_AfterCheck(object sender, TreeViewEventArgs e)
		{
			if (this.errProvider.GetError(this.lstAccounts) != null)
			{
				this.errProvider.SetError(this.lstAccounts, null);
			}
		}

		private void txtAimId_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.KeyData == Keys.Return)
			{
				this.btnCreate_Click(null, null);
			}
		}

		private void btnGoToBase_Click(object sender, EventArgs e)
		{
			try
			{
				Process.Start("http://onotolearmy.0pk.ru/");
			}
			catch
			{
			}
		}

		private void btnMail2Author_Click(object sender, EventArgs e)
		{
			try
			{
				Process.Start("mailto:exe-dealer@yandex.ru");
			}
			catch
			{
			}
		}

		protected override void OnResize(EventArgs e)
		{
			if (base.WindowState == FormWindowState.Minimized)
			{
				this.trayIcon.Visible = true;
				base.ShowInTaskbar = false;
				string statusString = this.GetStatusString();
				string tipText = "Двойной клик, чтобы восстановить.\r\nПравая кнопка, чтобы вызвать это окно." + ((statusString.Length > 0) ? ("\r\n\r\n" + statusString) : string.Empty);
				this.trayIcon.ShowBalloonTip(10, this.Text, tipText, ToolTipIcon.Info);
			}
			base.OnResize(e);
		}

		private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			base.WindowState = FormWindowState.Normal;
			base.ShowInTaskbar = true;
			this.trayIcon.Visible = false;
		}

		private void trayIcon_MouseClick(object sender, MouseEventArgs e)
		{
			string statusString = this.GetStatusString();
			string tipText = "Двойной клик, чтобы восстановить." + ((statusString.Length > 0) ? ("\r\n\r\n" + statusString) : string.Empty);
			if (e.Button == MouseButtons.Right)
			{
				this.trayIcon.ShowBalloonTip(10, this.Text, tipText, ToolTipIcon.Info);
			}
		}

		private string GetStatusString()
		{
			StringBuilder stringBuilder = new StringBuilder();
			List<string> list = new List<string>();
			List<int> list2 = new List<int>();
			List<int[]> list3 = new List<int[]>();
			foreach (DockControl dockControl in this.tpconMain.Documents)
			{
				AttackControl attackControl;
				if ((attackControl = (dockControl as AttackControl)) != null)
				{
					int[] array;
					if (!list2.Contains(attackControl.AimId))
					{
						list2.Add(attackControl.AimId);
						list.Add(attackControl.TabText);
						list3.Add(array = new int[8]);
					}
					else
					{
						array = list3[list2.IndexOf(attackControl.AimId)];
					}
					array[0] += attackControl.WallMessagesCount;
					array[1] += attackControl.TopicsCount;
					array[2] += attackControl.TopicsAnswersCount;
					array[3] += attackControl.AlbumsCount;
					array[4] += attackControl.PhotosCount;
					array[5] += attackControl.AudiosCount;
					array[6] += attackControl.VideosCount;
					array[7] += attackControl.VideosCommentsCount;
				}
			}
			for (int j = 0; j < list2.Count; j++)
			{
				stringBuilder.AppendLine(list[j]);
				foreach (int num in list3[j])
				{
					stringBuilder.Append(num.ToString().PadLeft(4) + " ");
				}
				stringBuilder.AppendLine();
			}
			return stringBuilder.ToString();
		}

		private void trayIcon_BalloonTipClicked(object sender, EventArgs e)
		{
			this.trayIcon_MouseClick(null, new MouseEventArgs(MouseButtons.Right, 1, 0, 0, 0));
		}
	}
}
