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

namespace Gsome
{
	internal static class Accounts
	{
		public static List<VKAccount> Items { get; private set; }

		public static void Load()
		{
			Accounts.xmlDoc = new XmlDocument();
			Accounts.xmlDoc.Load("accounts.xml");
			Accounts.Items = new List<VKAccount>();
			new ArrayList();
			XmlElement documentElement = Accounts.xmlDoc.DocumentElement;
			foreach (object obj in documentElement)
			{
				XmlNode xmlNode = (XmlNode)obj;
				XmlElement xmlElement = xmlNode as XmlElement;
				if (xmlElement != null && xmlElement.Name == "add")
				{
					VKAccount item = new VKAccount
					{
						EMail = xmlElement.GetAttribute("email"),
						Pass = xmlElement.GetAttribute("pass")
					};
					if (item.EMail.Length > 0 && item.Pass.Length > 0)
					{
						Accounts.Items.Add(item);
					}
				}
			}
		}

		public static void Save()
		{
			XmlElement documentElement = Accounts.xmlDoc.DocumentElement;
			foreach (VKAccount vkaccount in Accounts.Items)
			{
				XmlElement xmlElement = null;
				foreach (object obj in documentElement)
				{
					XmlNode xmlNode = (XmlNode)obj;
					XmlElement xmlElement2 = xmlNode as XmlElement;
					if (xmlElement2 != null && xmlElement2.Name == "add")
					{
						VKAccount vkaccount2 = new VKAccount
						{
							EMail = xmlElement2.GetAttribute("email"),
							Pass = xmlElement2.GetAttribute("pass")
						};
						if (vkaccount.EMail == vkaccount2.EMail && vkaccount.Pass == vkaccount2.Pass)
						{
							xmlElement = xmlElement2;
							break;
						}
					}
				}
				if (xmlElement == null)
				{
					XmlAttribute xmlAttribute = Accounts.xmlDoc.CreateAttribute("email");
					xmlAttribute.Value = vkaccount.EMail;
					XmlAttribute xmlAttribute2 = Accounts.xmlDoc.CreateAttribute("pass");
					xmlAttribute2.Value = vkaccount.Pass;
					xmlElement = Accounts.xmlDoc.CreateElement("add");
					xmlElement.Attributes.Append(xmlAttribute);
					xmlElement.Attributes.Append(xmlAttribute2);
					documentElement.AppendChild(xmlElement);
				}
			}
			List<XmlElement> list = new List<XmlElement>();
			foreach (object obj2 in documentElement)
			{
				XmlNode xmlNode2 = (XmlNode)obj2;
				XmlElement xmlElement3 = xmlNode2 as XmlElement;
				if (xmlElement3 != null && xmlElement3.Name == "add")
				{
					VKAccount acc = new VKAccount
					{
						EMail = xmlElement3.GetAttribute("email"),
						Pass = xmlElement3.GetAttribute("pass")
					};
					List<VKAccount> list2 = Accounts.Items.FindAll((VKAccount a) => a.EMail == acc.EMail && a.Pass == acc.Pass);
					if (list2.Count == 0)
					{
						list.Add(xmlElement3);
					}
				}
			}
			foreach (XmlElement xmlElement4 in list)
			{
				documentElement.ReplaceChild(Accounts.xmlDoc.CreateComment(xmlElement4.OuterXml), xmlElement4);
			}
			Accounts.xmlDoc.Save("accounts.xml");
		}

		private const string FileName = "accounts.xml";

		private const string XmlName_AccountElement = "add";

		private const string XmlName_EmailAttribute = "email";

		private const string XmlName_PassAttribute = "pass";

		private const string XmlName_Name = "name";

		private const string XmlName_Cookie = "cookie";

		private static XmlDocument xmlDoc;
	}
}
