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

namespace Gsome
{
	internal static class Configurations
	{
		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))
				{
					Library = text;
					xr.MoveToAttribute("proxy");
					string text2 = xr.ReadContentAsString().Trim();
					if (text2.Length > 0)
					{
						Proxy = new WebProxy(text2);
					}
					VKClient.Proxy = Proxy;
					xr.MoveToAttribute("random_case");
					RandomCase = xr.ReadContentAsBoolean();
					xr.MoveToAttribute("ascii_img");
					AddAsciiImages = xr.ReadContentAsBoolean();
					xr.MoveToAttribute("extra_lines");
					AddExtraLines = xr.ReadContentAsBoolean();
					xr.MoveToAttribute("full_log");
					FullLog = xr.ReadContentAsBoolean();
					xr.MoveToAttribute("auto_delays");
					AutoDelays = xr.ReadContentAsBoolean();
					xr.MoveToAttribute("captcha_timeout");
					CaptchaTimeout = xr.ReadContentAsInt();
					CaptchaDialog.Timeout = CaptchaTimeout;
					return;
				}
				throw new FileNotFoundException($"Словарь лучепоноса '{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");
				Enabled = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("msg_length");
				MessageLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("time_delay");
				TimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("lucheponos");
				Lucheponos = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("post_audio");
				PostAudio = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("post_video");
				PostVideo = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("post_graff");
				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"))
					{
						continue;
					}
					switch (xr.ReadContentAsString().Trim().ToLower())
					{
					case "graff":
						if (xr.MoveToAttribute("src"))
						{
							string text = xr.ReadContentAsString();
							if (File.Exists(text))
							{
								list.Add(text);
							}
						}
						break;
					case "audio":
						if (xr.MoveToAttribute("oid"))
						{
							string arg3 = xr.ReadContentAsString();
							if (xr.MoveToAttribute("id"))
							{
								string arg4 = xr.ReadContentAsString();
								list2.Add($"[[audio{arg3}_{arg4}]]");
							}
						}
						break;
					case "video":
						if (xr.MoveToAttribute("oid"))
						{
							string arg = xr.ReadContentAsString();
							if (xr.MoveToAttribute("id"))
							{
								string arg2 = xr.ReadContentAsString();
								list3.Add($"[[video{arg}_{arg2}]]");
							}
						}
						break;
					}
				}
				xmlReader.Close();
				Graffities = list.ToArray();
				Audio = list2.ToArray();
				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");
				Enabled = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("create_new");
				CreateNewTopics = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("answer_exist");
				PostAnswersToExistTopics = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("create_time_delay");
				CreationTimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("answer_time_delay");
				AnswerTimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("title_length");
				TitleLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("post_length");
				PostLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("answer_length");
				AnswerLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("grab_max_count");
				GrabMaxCount = xr.ReadContentAsInt();
				xr.MoveToAttribute("no_attack_today_created");
				NoAttackTodayCreated = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("recognize_by_key");
				RecognizeByKey = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("random_answer_start");
				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");
				Enabled = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("create_new");
				CreateNew = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("create_time_delay");
				CreateTimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("photos_upload_to_exist");
				PhotosUploadToExist = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("photos_upload_time_delay");
				PhotosUploadTimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("desc_length");
				DescriptionLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("grab_max_count");
				GrabMaxCount = xr.ReadContentAsInt();
				xr.MoveToAttribute("photos_always_upload");
				PhotosAlwaysUpload = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("no_attack_today_created");
				NoAttackTodayCreated = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("recognize_by_key");
				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();
				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");
				Enabled = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("time_delay");
				TimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("rename");
				Rename = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("performer_length");
				PerformerLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("title_length");
				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();
				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");
				Enabled = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("add_new");
				AddNew = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("comment_exist");
				CommentExist = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("add_time_delay");
				AddTimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("comment_time_delay");
				CommentTimeDelay = xr.ReadContentAsInt();
				xr.MoveToAttribute("desc_length");
				DescriptionLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("comment_length");
				CommentLength = xr.ReadContentAsInt();
				xr.MoveToAttribute("use_ajax");
				UseAjax = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("grab_max_count");
				GrabMaxCount = xr.ReadContentAsInt();
				xr.MoveToAttribute("no_attack_today_created");
				NoAttackTodayCreated = xr.ReadContentAsBoolean();
				xr.MoveToAttribute("random_comment_start");
				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();
				Videos = list.ToArray();
			}
		}

		private static XmlReader xr;

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