using System;
using System.IO;
using System.Net;

namespace VKontakte
{
	internal class VKRequest
	{
		private HttpWebRequest base_request;

		public string Method
		{
			get
			{
				return base_request.Method;
			}
			set
			{
				base_request.Method = value;
			}
		}

		public WebHeaderCollection Headers
		{
			get
			{
				return base_request.Headers;
			}
			set
			{
				base_request.Headers = value;
			}
		}

		public IWebProxy Proxy
		{
			get
			{
				return base_request.Proxy;
			}
			set
			{
				base_request.Proxy = value;
			}
		}

		private VKRequest()
		{
		}

		public static VKRequest Create(string url, IWebProxy proxy)
		{
			return Create(url, null, post: false, null, proxy);
		}

		public static VKRequest Create(string url, bool post, string content_type, IWebProxy proxy)
		{
			return Create(url, null, post, content_type, proxy);
		}

		public static VKRequest Create(string url, string cookie, IWebProxy proxy)
		{
			return Create(url, cookie, post: false, null, proxy);
		}

		public static VKRequest Create(string url, string cookie, bool post, string content_type, IWebProxy proxy)
		{
			VKRequest vKRequest = new VKRequest();
			HttpWebRequest httpWebRequest = (vKRequest.base_request = WebRequest.Create(url) as HttpWebRequest);
			if (post)
			{
				httpWebRequest.Method = "POST";
			}
			httpWebRequest.AllowAutoRedirect = false;
			httpWebRequest.ServicePoint.Expect100Continue = false;
			httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
			httpWebRequest.Proxy = proxy;
			httpWebRequest.Timeout = 50000;
			httpWebRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6";
			httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
			httpWebRequest.ContentType = content_type;
			if (cookie != null)
			{
				httpWebRequest.Headers.Add("Cookie", cookie);
			}
			httpWebRequest.Headers.Add("Accept-Language", "ru,en-us;q=0.7,en;q=0.3");
			httpWebRequest.Headers.Add("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.7");
			return vKRequest;
		}

		public Stream GetRequestStream()
		{
			return base_request.GetRequestStream();
		}

		public HttpWebResponse GetResponse()
		{
			HttpWebResponse httpWebResponse = base_request.GetResponse() as HttpWebResponse;
			string text = httpWebResponse.Headers["Location"];
			if (text != null)
			{
				try
				{
					string absoluteUri = base_request.RequestUri.AbsoluteUri;
					if (!absoluteUri.Contains("blank.php") && text.Contains("blank.php"))
					{
						throw BlankErrorException.Create(text, base_request.Proxy);
					}
					if (absoluteUri.Contains("login.php"))
					{
						return httpWebResponse;
					}
					if (text.Contains("login.php"))
					{
						throw new AuthorizationFailedException();
					}
					return httpWebResponse;
				}
				catch (Exception ex)
				{
					httpWebResponse.Close();
					throw ex;
				}
			}
			return httpWebResponse;
		}
	}
}
