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 ContentMenu : PropertyChangeTracker
    {
        // not writeable
        public int? ContentMenuId { get; private set; }
        public bool? Active { get; private set; }
        public DateTime? Created { get; private set; }
        public DateTime? Modified { get; private set; }
        public float? SortBy {get; private set; }
        public int? ContentMenuTypeId { get; private set; }
        public string Name { get; private set; }
        public string Description { get; private set; }
        public string IconFilename { get; private set; }
        public int? UserId { get; private set; }
        public int? CollectionId { get; private set; }
        public string FilterData { get; private set; }

        public ContentMenu()
        {
        }

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

        public override string ToString()
        {
            return Name;
        }

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

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

            foreach (XmlNode node in root.ChildNodes)
            {
                if (node.Name.Equals("ArrayOfItem", StringComparison.OrdinalIgnoreCase))
                {
                    maxModified = DateTime.Parse(node.Attributes["MaxModified"].Value);

                    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 ContentMenu(node.ChildNodes[i]));
                }
            }
            if (pagingParameters != null)
                pagingParameters.hasMore = hasMore;
            return list;
        }
    }
}
