using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Xml;
using VKontakte;

namespace Gsome
{
	internal static class Configurations
	{
		public static void Load()
		{
			Configurations.xr = XmlReader.Create("configurations.xml");
			Configurations.General.Load();
			Configurations.Wall.Load();
			Configurations.Topics.Load();
			Configurations.Albums.Load();
			Configurations.Audio.Load();
			Configurations.Video.Load();
			Configurations.xr.Close();
		}

		private static XmlReader xr;

		public static class General
		{
			public static string Library { get; private set; }

			public static IWebProxy Proxy { get; private set; }

			public static bool RandomCase { get; private set; }

			public static bool AddAsciiImages { get; private set; }

			public static bool AddExtraLines { get; private set; }

			public static bool FullLog { get; private set; }

			public static bool AutoDelays { get; private set; }

			public static int CaptchaTimeout { get; private set; }

			public static void Load()
			{
				XmlReader xr = Configurations.xr;
				xr.ReadToFollowing("general");
				xr.MoveToAttribute("library");
				string text = xr.ReadContentAsString();
				if (File.Exists(text))
				{
					Configurations.General.Library = text;
					xr.MoveToAttribute("proxy");
					string text2 = xr.ReadContentAsString().Trim();
					if (text2.Length > 0)
					{
						Configurations.General.Proxy = new WebProxy(text2);
					}
					VKClient.Proxy = Configurations.General.Proxy;
					xr.MoveToAttribute("random_case");
					Configurations.General.RandomCase = xr.ReadContentAsBoolean();
					xr.MoveToAttribute("ascii_img");
					Configurations.General.AddAsciiImages = xr.ReadContentAsBoolean();
					xr.MoveToAttribute("extra_lines");
					Configurations.General.AddExtraLines = xr.ReadContentAsBoolean();
					xr.MoveToAttribute("full_log");
					Configurations.General.FullLog = xr.ReadContentAsBoolean();
					xr.MoveToAttribute("auto_delays");
					Configurations.General.AutoDelays = xr.ReadContentAsBoolean();
					xr.MoveToAttribute("captcha_timeout");
					Configurations.General.CaptchaTimeout = xr.ReadContentAsInt();
					CaptchaDialog.Timeout = Configurations.General.CaptchaTimeout;
					return;
				}
				throw new FileNotFoundException(string.Format("Словарь лучепоноса '{0}' не найден.", text));
			}
		}

		public static class Wall
		{
			public static bool Enabled { get; private set; }

			public static int TimeDelay { get; private set; }

			public static int MessageLength { get; private set; }

			public static bool Lucheponos { get; private set; }

			public static bool PostGraffiti { get; private set; }

			public static bool PostAudio { get; private set; }

			public static bool PostVideo { get; private set; }

			public static string[] Graffities { get; private set; }

			public static string[] Audio { get; private set; }

			public static string[] Videos { get; private set; }

			public static void Load()
			{
				XmlReader xr = Configurations.xr;
				xr.ReadToFollowing("wall");
				xr.MoveToAttribute("enabled");
				Configurations.Wall.Enabled = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("msg_length");
				Configurations.Wall.MessageLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("time_delay");
				Configurations.Wall.TimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("lucheponos");
				Configurations.Wall.Lucheponos = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("post_audio");
				Configurations.Wall.PostAudio = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("post_video");
				Configurations.Wall.PostVideo = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("post_graff");
				Configurations.Wall.PostGraffiti = xr.ReadContentAsBoolean();
				List<string> list = new List<string>();
				List<string> list2 = new List<string>();
				List<string> list3 = new List<string>();
				xr.MoveToElement();
				XmlReader xmlReader = xr.ReadSubtree();
				while (xmlReader.ReadToFollowing("add"))
				{
					if (xr.MoveToAttribute("type"))
					{
						string a = xr.ReadContentAsString().Trim().ToLower();
						if (a == "graff")
						{
							if (xr.MoveToAttribute("src"))
							{
								string text = xr.ReadContentAsString();
								if (File.Exists(text))
								{
									list.Add(text);
								}
							}
						}
						else if (a == "audio")
						{
							if (xr.MoveToAttribute("oid"))
							{
								string arg = xr.ReadContentAsString();
								if (xr.MoveToAttribute("id"))
								{
									string arg2 = xr.ReadContentAsString();
									list2.Add(string.Format("[[audio{0}_{1}]]", arg, arg2));
								}
							}
						}
						else if (a == "video" && xr.MoveToAttribute("oid"))
						{
							string arg3 = xr.ReadContentAsString();
							if (xr.MoveToAttribute("id"))
							{
								string arg4 = xr.ReadContentAsString();
								list3.Add(string.Format("[[video{0}_{1}]]", arg3, arg4));
							}
						}
					}
				}
				xmlReader.Close();
				Configurations.Wall.Graffities = list.ToArray();
				Configurations.Wall.Audio = list2.ToArray();
				Configurations.Wall.Videos = list3.ToArray();
			}
		}

		public static class Topics
		{
			public static bool Enabled { get; private set; }

			public static bool CreateNewTopics { get; private set; }

			public static bool PostAnswersToExistTopics { get; private set; }

			public static int CreationTimeDelay { get; private set; }

			public static int AnswerTimeDelay { get; private set; }

			public static int TitleLength { get; private set; }

			public static int PostLength { get; private set; }

			public static int AnswerLength { get; private set; }

			public static int GrabMaxCount { get; private set; }

			public static bool NoAttackTodayCreated { get; private set; }

			public static bool RecognizeByKey { get; private set; }

			public static bool RandomAnswerStart { get; private set; }

			public static void Load()
			{
				XmlReader xr = Configurations.xr;
				xr.ReadToFollowing("topics");
				xr.MoveToAttribute("enabled");
				Configurations.Topics.Enabled = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("create_new");
				Configurations.Topics.CreateNewTopics = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("answer_exist");
				Configurations.Topics.PostAnswersToExistTopics = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("create_time_delay");
				Configurations.Topics.CreationTimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("answer_time_delay");
				Configurations.Topics.AnswerTimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("title_length");
				Configurations.Topics.TitleLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("post_length");
				Configurations.Topics.PostLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("answer_length");
				Configurations.Topics.AnswerLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("grab_max_count");
				Configurations.Topics.GrabMaxCount = xr.ReadContentAsInt();
				xr.MoveToAttribute("no_attack_today_created");
				Configurations.Topics.NoAttackTodayCreated = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("recognize_by_key");
				Configurations.Topics.RecognizeByKey = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("random_answer_start");
				Configurations.Topics.RandomAnswerStart = xr.ReadContentAsBoolean();
			}
		}

		public static class Albums
		{
			public static bool Enabled { get; private set; }

			public static bool CreateNew { get; private set; }

			public static bool PhotosUploadToExist { get; private set; }

			public static int CreateTimeDelay { get; private set; }

			public static int PhotosUploadTimeDelay { get; private set; }

			public static int DescriptionLength { get; private set; }

			public static int GrabMaxCount { get; private set; }

			public static bool PhotosAlwaysUpload { get; private set; }

			public static bool NoAttackTodayCreated { get; private set; }

			public static bool RecognizeByKey { get; private set; }

			public static string[] Photos { get; private set; }

			public static void Load()
			{
				XmlReader xr = Configurations.xr;
				xr.ReadToFollowing("albums");
				xr.MoveToAttribute("enabled");
				Configurations.Albums.Enabled = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("create_new");
				Configurations.Albums.CreateNew = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("create_time_delay");
				Configurations.Albums.CreateTimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("photos_upload_to_exist");
				Configurations.Albums.PhotosUploadToExist = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("photos_upload_time_delay");
				Configurations.Albums.PhotosUploadTimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("desc_length");
				Configurations.Albums.DescriptionLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("grab_max_count");
				Configurations.Albums.GrabMaxCount = xr.ReadContentAsInt();
				xr.MoveToAttribute("photos_always_upload");
				Configurations.Albums.PhotosAlwaysUpload = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("no_attack_today_created");
				Configurations.Albums.NoAttackTodayCreated = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("recognize_by_key");
				Configurations.Albums.RecognizeByKey = xr.ReadContentAsBoolean();
				xr.MoveToElement();
				XmlReader xmlReader = xr.ReadSubtree();
				List<string> list = new List<string>();
				while (xmlReader.ReadToFollowing("add"))
				{
					if (xr.MoveToAttribute("src"))
					{
						string text = xr.ReadContentAsString();
						if (File.Exists(text))
						{
							list.Add(text);
						}
					}
				}
				xmlReader.Close();
				Configurations.Albums.Photos = list.ToArray();
			}
		}

		public static class Audio
		{
			public static bool Enabled { get; private set; }

			public static int TimeDelay { get; private set; }

			public static bool Rename { get; private set; }

			public static int PerformerLength { get; private set; }

			public static int TitleLength { get; private set; }

			public static string[] AudioFiles { get; private set; }

			public static void Load()
			{
				XmlReader xr = Configurations.xr;
				xr.ReadToFollowing("audio");
				xr.MoveToAttribute("enabled");
				Configurations.Audio.Enabled = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("time_delay");
				Configurations.Audio.TimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("rename");
				Configurations.Audio.Rename = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("performer_length");
				Configurations.Audio.PerformerLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("title_length");
				Configurations.Audio.TitleLength = xr.ReadContentAsInt();
				xr.MoveToElement();
				XmlReader xmlReader = xr.ReadSubtree();
				List<string> list = new List<string>();
				while (xmlReader.ReadToFollowing("add"))
				{
					xr.MoveToAttribute("src");
					string text = xr.ReadContentAsString();
					if (File.Exists(text))
					{
						list.Add(text);
					}
				}
				xmlReader.Close();
				Configurations.Audio.AudioFiles = list.ToArray();
			}
		}

		public static class Video
		{
			public static bool Enabled { get; private set; }

			public static bool AddNew { get; private set; }

			public static bool CommentExist { get; private set; }

			public static int AddTimeDelay { get; private set; }

			public static int CommentTimeDelay { get; private set; }

			public static int DescriptionLength { get; private set; }

			public static int CommentLength { get; private set; }

			public static bool UseAjax { get; private set; }

			public static int GrabMaxCount { get; private set; }

			public static bool NoAttackTodayCreated { get; private set; }

			public static bool RandomCommentStart { get; private set; }

			public static VideoInfo[] Videos { get; private set; }

			public static void Load()
			{
				XmlReader xr = Configurations.xr;
				xr.ReadToFollowing("video");
				xr.MoveToAttribute("enabled");
				Configurations.Video.Enabled = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("add_new");
				Configurations.Video.AddNew = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("comment_exist");
				Configurations.Video.CommentExist = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("add_time_delay");
				Configurations.Video.AddTimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("comment_time_delay");
				Configurations.Video.CommentTimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("desc_length");
				Configurations.Video.DescriptionLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("comment_length");
				Configurations.Video.CommentLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("use_ajax");
				Configurations.Video.UseAjax = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("grab_max_count");
				Configurations.Video.GrabMaxCount = xr.ReadContentAsInt();
				xr.MoveToAttribute("no_attack_today_created");
				Configurations.Video.NoAttackTodayCreated = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("random_comment_start");
				Configurations.Video.RandomCommentStart = xr.ReadContentAsBoolean();
				List<VideoInfo> list = new List<VideoInfo>();
				xr.MoveToElement();
				XmlReader xmlReader = xr.ReadSubtree();
				while (xmlReader.ReadToFollowing("add"))
				{
					if (xr.MoveToAttribute("oid"))
					{
						int ownerID = xr.ReadContentAsInt();
						if (xr.MoveToAttribute("id"))
						{
							int id = xr.ReadContentAsInt();
							list.Add(new VideoInfo
							{
								ID = id,
								OwnerID = ownerID
							});
						}
					}
				}
				xmlReader.Close();
				Configurations.Video.Videos = list.ToArray();
			}
		}
	}
}
