﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.XPath;

namespace PlanetNotify
{
	public abstract class Census
	{
		public static string[] GetFriendsList(string character_id)
		{
			string url = "http://census.soe.com/xml/get/ps2:v2/characters_friend?character_id=" + character_id;
			XPathNavigator nav = GetXmlDocument(url).CreateNavigator();
			XPathNodeIterator it = nav.Select("/characters_friend_list/characters_friend/friend_list/friend");

			List<string> ids = new List<string>();

			foreach(XPathNavigator friend in it)
			{
				ids.Add(friend.GetAttribute("character_id", ""));
			}

			return ids.ToArray();
		}

		public static string[] GetFriendsListAsNames(string character_id)
		{
			string[] ids = GetFriendsList(character_id);
			List<string> names = new List<string>();

			for (int i = 0; i < ids.Length; i++)
			{
				names.Add(CharacterIdToName(ids[i]));
			}

			return names.ToArray();
		}

		public static string CharacterNameToId(string name)
		{
			string url = "http://census.soe.com/xml/get/ps2:v2/character/?name.first_lower=" + name.ToLower() + "&c:show=character_id";

			XPathNavigator nav = GetXmlDocument(url).CreateNavigator();
			XPathNodeIterator it = nav.Select("//@character_id");

			foreach (XPathNavigator id in it)
			{
				return id.Value;
			}

			return null;
		}

		public static string CharacterIdToName(string character_id)
		{
			string url = "http://census.soe.com/xml/get/ps2:v2/character?character_id=" + character_id + "&c:show=name";
			XPathNavigator nav = GetXmlDocument(url).CreateNavigator();
			XPathNodeIterator it = nav.Select("//@first");

			foreach (XPathNavigator name in it)
			{
				return name.Value;
			}

			return null;
		}

		public static bool GetCharacterStatus(string character_id)
		{
			string url = "http://census.soe.com/xml/get/ps2:v2/character?character_id=" + character_id + "&c:resolve=online_status";
			XPathNavigator nav = GetXmlDocument(url).CreateNavigator();
			XPathNodeIterator it = nav.Select("//@online_status");

			foreach (XPathNavigator status in it)
			{
				if (status.Value.Equals("0"))
					return false;
				else
					return true;
			}

			return false;
		}

		public static bool[] GetCharactersStatuses(string[] characters_ids)
		{
			bool[] statuses = new bool[characters_ids.Length];

			for (int i = 0; i < characters_ids.Length; i++)
			{
				string url = "http://census.soe.com/xml/get/ps2:v2/character?character_id=" + characters_ids[i] + "&c:resolve=online_status";

				XPathNavigator nav = GetXmlDocument(url).CreateNavigator();
				XPathNodeIterator it = nav.Select("//@online_status");

				foreach (XPathNavigator status in it)
				{
					if (status.Value.Equals("0"))
						statuses[i] = false;
					else
						statuses[i] = true;
				}
			}

			return statuses;
		}

		public static void GetPlayerObject()
		{
			throw new NotImplementedException();
		}

		public static void GetPlayersObjects()
		{
			throw new NotImplementedException();
		}

		public static XPathDocument GetXmlDocument(string url)
		{
			try
			{
				HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
				HttpWebResponse response = request.GetResponse() as HttpWebResponse;

				return new XPathDocument(response.GetResponseStream());
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				return null;
			}
		}
	}
}
