using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Media;
using System.Net;
using System.Windows.Forms;

namespace VKontakte
{
	public class CaptchaDialog : Form
	{
		private TextBox txtKey;

		private Button btnOk;

		private Button btnCancel;

		private TableLayoutPanel tlpMain;

		private TableLayoutPanel tlpButtons;

		private PictureBox imgCaptcha;

		private Timer tmrCancel;

		private IContainer components;

		private static volatile int timeout = 30;

		private int count = Timeout;

		public static int Timeout
		{
			get
			{
				return timeout;
			}
			set
			{
				timeout = value;
			}
		}

		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
			imgCaptcha = new System.Windows.Forms.PictureBox();
			txtKey = new System.Windows.Forms.TextBox();
			btnOk = new System.Windows.Forms.Button();
			btnCancel = new System.Windows.Forms.Button();
			tlpMain = new System.Windows.Forms.TableLayoutPanel();
			tlpButtons = new System.Windows.Forms.TableLayoutPanel();
			tmrCancel = new System.Windows.Forms.Timer(components);
			((System.ComponentModel.ISupportInitialize)imgCaptcha).BeginInit();
			tlpMain.SuspendLayout();
			SuspendLayout();
			imgCaptcha.Anchor = System.Windows.Forms.AnchorStyles.None;
			imgCaptcha.BackColor = System.Drawing.Color.White;
			imgCaptcha.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			tlpMain.SetColumnSpan(imgCaptcha, 2);
			imgCaptcha.Location = new System.Drawing.Point(32, 3);
			imgCaptcha.Name = "imgCaptcha";
			imgCaptcha.Size = new System.Drawing.Size(130, 50);
			imgCaptcha.TabIndex = 0;
			imgCaptcha.TabStop = false;
			txtKey.Anchor = System.Windows.Forms.AnchorStyles.None;
			tlpMain.SetColumnSpan(txtKey, 2);
			txtKey.Location = new System.Drawing.Point(32, 59);
			txtKey.Name = "txtKey";
			txtKey.Size = new System.Drawing.Size(130, 20);
			txtKey.TabIndex = 1;
			btnOk.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
			btnOk.Location = new System.Drawing.Point(6, 90);
			btnOk.Name = "btnOk";
			btnOk.Size = new System.Drawing.Size(85, 25);
			btnOk.TabIndex = 2;
			btnOk.Text = "OK";
			btnOk.UseVisualStyleBackColor = true;
			btnOk.Click += new System.EventHandler(btnOk_Click);
			btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
			btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			btnCancel.Location = new System.Drawing.Point(103, 90);
			btnCancel.Name = "btnCancel";
			btnCancel.Size = new System.Drawing.Size(85, 25);
			btnCancel.TabIndex = 3;
			btnCancel.Text = "Отмена";
			btnCancel.UseVisualStyleBackColor = true;
			btnCancel.Click += new System.EventHandler(btnCancel_Click);
			tlpMain.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
			tlpMain.ColumnCount = 2;
			tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333f));
			tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333f));
			tlpMain.Controls.Add(btnOk, 0, 2);
			tlpMain.Controls.Add(imgCaptcha, 0, 0);
			tlpMain.Controls.Add(txtKey, 0, 1);
			tlpMain.Controls.Add(btnCancel, 1, 2);
			tlpMain.Dock = System.Windows.Forms.DockStyle.Fill;
			tlpMain.Location = new System.Drawing.Point(0, 0);
			tlpMain.Name = "tlpMain";
			tlpMain.RowCount = 3;
			tlpMain.RowStyles.Add(new System.Windows.Forms.RowStyle());
			tlpMain.RowStyles.Add(new System.Windows.Forms.RowStyle());
			tlpMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100f));
			tlpMain.Size = new System.Drawing.Size(194, 118);
			tlpMain.TabIndex = 4;
			tlpButtons.AutoSize = true;
			tlpButtons.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
			tlpButtons.ColumnCount = 2;
			tlpButtons.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50f));
			tlpButtons.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50f));
			tlpButtons.Dock = System.Windows.Forms.DockStyle.Bottom;
			tlpButtons.Location = new System.Drawing.Point(0, 118);
			tlpButtons.Name = "tlpButtons";
			tlpButtons.RowCount = 1;
			tlpButtons.RowStyles.Add(new System.Windows.Forms.RowStyle());
			tlpButtons.Size = new System.Drawing.Size(194, 0);
			tlpButtons.TabIndex = 5;
			tmrCancel.Interval = 1000;
			tmrCancel.Tick += new System.EventHandler(tmrCancel_Tick);
			base.AcceptButton = btnOk;
			base.CancelButton = btnCancel;
			base.ClientSize = new System.Drawing.Size(192, 116);
			base.Controls.Add(tlpButtons);
			base.Controls.Add(tlpMain);
			base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			base.MaximizeBox = false;
			base.MinimizeBox = false;
			MinimumSize = new System.Drawing.Size(200, 150);
			base.Name = "CaptchaDialog";
			base.ShowIcon = false;
			base.ShowInTaskbar = false;
			base.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			base.TopMost = true;
			((System.ComponentModel.ISupportInitialize)imgCaptcha).EndInit();
			tlpMain.ResumeLayout(false);
			tlpMain.PerformLayout();
			ResumeLayout(false);
			PerformLayout();
		}

		private void tmrCancel_Tick(object sender, EventArgs e)
		{
			if (count-- == 0)
			{
				base.DialogResult = DialogResult.Ignore;
			}
		}

		private CaptchaDialog()
		{
			InitializeComponent();
		}

		protected override void OnShown(EventArgs e)
		{
			base.OnShown(e);
			Activate();
			SystemSounds.Exclamation.Play();
			tmrCancel.Start();
		}

		internal static DialogResult Show(string caption, string sid, IWebProxy proxy, ref string key)
		{
			VKRequest vKRequest = VKRequest.Create("http://vkontakte.ru/captcha.php?s=1&sid=" + sid, proxy);
			HttpWebResponse response = vKRequest.GetResponse();
			Stream responseStream = response.GetResponseStream();
			CaptchaDialog captchaDialog = new CaptchaDialog();
			captchaDialog.Text = caption;
			Bitmap bitmap = new Bitmap(responseStream);
			captchaDialog.imgCaptcha.Image = bitmap;
			DialogResult result = captchaDialog.ShowDialog();
			if (captchaDialog.txtKey.Text.Length > 0)
			{
				key = captchaDialog.txtKey.Text;
			}
			captchaDialog.imgCaptcha.Image = null;
			bitmap.Dispose();
			captchaDialog.Dispose();
			responseStream.Close();
			return result;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}
	}
}
