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 Request : PropertyChangeTracker
    {
        public int? RequestId { get; private set; }
        public bool? Active { get; private set; }
        public DateTime? Created { get; private set; }
        public DateTime? Modified { get; private set; }
        public int? RequestTypeId { get; private set; }
        public int? Requestor_UserId { get; private set; }
        public int? UserId { get; private set; }
        public int? CollectionId { get; private set; }
        public int? User_CollectionTypeId { get; private set; }
        public bool? User_UserFollowing { get; private set; }
        public string Message { get; private set; }
        public Guid? RequestGuid { get; private set; }
        public bool? Accepted { get; private set; }
        public int? RequestStateId { get; private set; }
        public int? AcceptedBy_UserId { get; private set; }
        public DateTime? AcceptedDate { get; private set; }
        public int? NotificationPriorityId { get; private set; }

        public Request()
        {
        }

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

        public override string ToString()
        {
            return RequestGuid.ToString();
        }

        public static List<Request> ParseList(PagingParameters pagingParameters, System.Net.WebResponse response)
        {
            XmlNode root = EQNetwork.Services.EQApiService.ParseXml(response);

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

            foreach (XmlNode node in root.ChildNodes)
            {
                if (node.Name.Equals("ArrayOfRequest", 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 Request(node.ChildNodes[i]));
                }
            }
            if (pagingParameters != null)
                pagingParameters.hasMore = hasMore;
            return list;
        }
    }
}
