using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using EQNetwork.Utility;
using EQNetwork.Aspects;

namespace EQNetwork.Objects
{
    public class Friend : PropertyChangeTracker
    {
        public string Name { get; set; }
        public string Name2 { get; set; } // twitter = screenname

        public string FacebookId { get; set; }
        public string TwitterId { get; set; }
        
        public string Picture { get; set; }
        
        public int? UserInvitationStateId { get; set; }
        public Guid? UserInvitationGuid { get; set; }


        public Friend()
        {
        }

        public Friend(XmlNode node)
        {
            EQParse.SetPropertiesWithXmlAttributes(this, node);
            ClearPropertyChanges();
        }

        public override string ToString()
        {
            return Name;
        }
        public static List<Friend> ParseList(PagingParameters pagingParameters, System.Net.WebResponse response)
        {
            XmlNode root = EQNetwork.Services.EQApiService.ParseXml(response);

            List<Friend> list = new List<Friend>();
            bool hasMore = false;

            foreach (XmlNode node in root.ChildNodes)
            {
                if (node.Name.Equals("Friends", StringComparison.OrdinalIgnoreCase))
                {
                    int n = node.ChildNodes.Count;
                    if (pagingParameters != null)
                    {
                        // we always ask for one extra when paging
                        hasMore = (n > pagingParameters.PageSize);
                        if (hasMore) n--;
                    }
                    else
                        hasMore = false;
                    for (int i = 0; i < n; i++)
                        list.Add(new Friend(node.ChildNodes[i]));
                }
            }
            if (pagingParameters != null)
                pagingParameters.hasMore = hasMore;
            return list;
        }
    }
}
