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 txtCode;

		private Button btnOk;

		private Button btnCancel;

		private TableLayoutPanel tlpImg;

		private TableLayoutPanel tlpButtons;

		private PictureBox imgCaptcha;

		private void InitializeComponent()
		{
			imgCaptcha = new System.Windows.Forms.PictureBox();
			txtCode = new System.Windows.Forms.TextBox();
			btnOk = new System.Windows.Forms.Button();
			btnCancel = new System.Windows.Forms.Button();
			tlpImg = new System.Windows.Forms.TableLayoutPanel();
			tlpButtons = new System.Windows.Forms.TableLayoutPanel();
			((System.ComponentModel.ISupportInitialize)imgCaptcha).BeginInit();
			tlpImg.SuspendLayout();
			tlpButtons.SuspendLayout();
			SuspendLayout();
			imgCaptcha.Anchor = System.Windows.Forms.AnchorStyles.None;
			imgCaptcha.BackColor = System.Drawing.Color.White;
			imgCaptcha.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			imgCaptcha.Location = new System.Drawing.Point(56, 3);
			imgCaptcha.Name = "imgCaptcha";
			imgCaptcha.Size = new System.Drawing.Size(130, 50);
			imgCaptcha.TabIndex = 0;
			imgCaptcha.TabStop = false;
			txtCode.Anchor = System.Windows.Forms.AnchorStyles.None;
			txtCode.Location = new System.Drawing.Point(56, 59);
			txtCode.Name = "txtCode";
			txtCode.Size = new System.Drawing.Size(130, 20);
			txtCode.TabIndex = 1;
			btnOk.Anchor = System.Windows.Forms.AnchorStyles.None;
			btnOk.Location = new System.Drawing.Point(10, 3);
			btnOk.Name = "btnOk";
			btnOk.Size = new System.Drawing.Size(100, 25);
			btnOk.TabIndex = 2;
			btnOk.Text = "OK";
			btnOk.UseVisualStyleBackColor = true;
			btnOk.Click += new System.EventHandler(btnOk_Click);
			btnCancel.Anchor = System.Windows.Forms.AnchorStyles.None;
			btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			btnCancel.Location = new System.Drawing.Point(131, 3);
			btnCancel.Name = "btnCancel";
			btnCancel.Size = new System.Drawing.Size(100, 25);
			btnCancel.TabIndex = 3;
			btnCancel.Text = "Отмена";
			btnCancel.UseVisualStyleBackColor = true;
			btnCancel.Click += new System.EventHandler(btnCancel_Click);
			tlpImg.AutoSize = true;
			tlpImg.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
			tlpImg.ColumnCount = 1;
			tlpImg.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
			tlpImg.Controls.Add(imgCaptcha, 0, 0);
			tlpImg.Controls.Add(txtCode, 0, 1);
			tlpImg.Dock = System.Windows.Forms.DockStyle.Top;
			tlpImg.Location = new System.Drawing.Point(0, 0);
			tlpImg.Name = "tlpImg";
			tlpImg.RowCount = 2;
			tlpImg.RowStyles.Add(new System.Windows.Forms.RowStyle());
			tlpImg.RowStyles.Add(new System.Windows.Forms.RowStyle());
			tlpImg.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20f));
			tlpImg.Size = new System.Drawing.Size(242, 82);
			tlpImg.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.Controls.Add(btnOk, 0, 0);
			tlpButtons.Controls.Add(btnCancel, 1, 0);
			tlpButtons.Dock = System.Windows.Forms.DockStyle.Bottom;
			tlpButtons.Location = new System.Drawing.Point(0, 85);
			tlpButtons.Name = "tlpButtons";
			tlpButtons.RowCount = 1;
			tlpButtons.RowStyles.Add(new System.Windows.Forms.RowStyle());
			tlpButtons.Size = new System.Drawing.Size(242, 31);
			tlpButtons.TabIndex = 5;
			base.AcceptButton = btnOk;
			base.CancelButton = btnCancel;
			base.ClientSize = new System.Drawing.Size(242, 116);
			base.Controls.Add(tlpButtons);
			base.Controls.Add(tlpImg);
			base.MaximizeBox = false;
			base.MinimizeBox = false;
			MinimumSize = new System.Drawing.Size(250, 150);
			base.Name = "CaptchaDialog";
			base.ShowIcon = false;
			base.ShowInTaskbar = false;
			base.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			Text = "Капча";
			base.TopMost = true;
			((System.ComponentModel.ISupportInitialize)imgCaptcha).EndInit();
			tlpImg.ResumeLayout(false);
			tlpImg.PerformLayout();
			tlpButtons.ResumeLayout(false);
			ResumeLayout(false);
			PerformLayout();
		}

		public CaptchaDialog()
		{
			InitializeComponent();
		}

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

		public static string Show(string cap, string captcha_id, WebProxy proxy, string cookies)
		{
			HttpWebRequest httpWebRequest = WebRequest.Create("http://vkontakte.ru//captcha.php?s=1&sid=" + captcha_id.ToString()) as HttpWebRequest;
			if (proxy != null)
			{
				httpWebRequest.Proxy = proxy;
			}
			httpWebRequest.AllowAutoRedirect = false;
			httpWebRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6";
			httpWebRequest.ContentType = "application/x-www-form-urlencoded";
			httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
			httpWebRequest.Headers.Add("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.7");
			httpWebRequest.Headers.Add("Accept-Language", "ru,en-us;q=0.7,en;q=0.3");
			httpWebRequest.Headers.Add("Cookie", cookies);
			HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
			Stream responseStream = httpWebResponse.GetResponseStream();
			CaptchaDialog captchaDialog = new CaptchaDialog();
			captchaDialog.Text = cap;
			Bitmap bitmap = new Bitmap(responseStream);
			captchaDialog.imgCaptcha.Image = bitmap;
			DialogResult dialogResult = captchaDialog.ShowDialog();
			string result = captchaDialog.txtCode.Text;
			captchaDialog.imgCaptcha.Image = null;
			bitmap.Dispose();
			captchaDialog.Dispose();
			httpWebResponse.Close();
			responseStream.Dispose();
			if (dialogResult != DialogResult.OK)
			{
				return null;
			}
			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;
		}
	}
}
