using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using VKontakte;

namespace Gsome
{
	public partial class SplashScreen : Form
	{
		public SplashScreen()
		{
			this.InitializeComponent();
			this.lblVersion.Text = "Версия: " + Application.ProductVersion + "\r\nВоруй-Убивай edition";
		}

		protected override void OnPaintBackground(PaintEventArgs e)
		{
			Graphics graphics = e.Graphics;
			Rectangle rect = new Rectangle(1, 1, base.ClientRectangle.Width - 2, base.ClientRectangle.Height - 2);
			using (LinearGradientBrush linearGradientBrush = new LinearGradientBrush(rect, ProfessionalColors.ButtonSelectedGradientBegin, ProfessionalColors.ButtonSelectedGradientEnd, LinearGradientMode.Vertical))
			{
				graphics.FillRectangle(linearGradientBrush, rect);
			}
			graphics.DrawRectangle(SplashScreen.penBorder, rect);
			base.OnPaint(e);
		}

		protected override void OnShown(EventArgs e)
		{
			base.OnShown(e);
			this.pbAuthorization.Maximum = Accounts.Items.Count;
			Application.DoEvents();
			List<VKAccount> list = new List<VKAccount>();
			for (int i = 0; i < Accounts.Items.Count; i++)
			{
				VKAccount vkaccount = Accounts.Items[i];
				this.lblStatus.Text = "Авторизация: " + vkaccount;
				Application.DoEvents();
				try
				{
					VKClient vkclient = VKClient.Create(vkaccount.EMail, vkaccount.Pass);
					Accounts.Items[i] = vkclient.Account;
					VKClients.Items.Add(vkclient);
					this.pbAuthorization.SetNormalStyle();
				}
				catch (AuthorizationFailedException)
				{
					this.pbAuthorization.SetErrorStyle();
					list.Add(vkaccount);
				}
				this.pbAuthorization.Progress++;
				Application.DoEvents();
			}
			this.pbAuthorization.SetNormalStyle();
			this.lblStatus.Text = "Авторизация завершена";
			foreach (VKAccount item in list)
			{
				Accounts.Items.Remove(item);
			}
			Accounts.Save();
			base.Close();
		}

		private void pctLogo_Resize(object sender, EventArgs e)
		{
		}

		private static Pen penBorder = new Pen(ProfessionalColors.ButtonSelectedHighlightBorder, 2f);
	}
}
