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 Post : PropertyChangeTracker
    {
        // NotifyPropertyChanged is handled by PropertyChangeTracker to remember changes

        // fields that can be set on the backend 
        [TrackPropertyChanged()]
        public string Title { get; set; }
        [TrackPropertyChanged()]
        public string Message { get; set; }

        [TrackPropertyChanged()]
        public int? CommentOn_PostId { get; set; }
        [TrackPropertyChanged()]
        public int? CommentOn_UserId { get; set; }
        [TrackPropertyChanged()]
        public int? CommentOn_ContentFileId { get; set; }
        [TrackPropertyChanged()]
        public int? PostTypeId { get; set; }

        // helper enum conversion
        public Db.PostType PostType { get { return (PostTypeId == null) ? Db.PostType.Unknown : (Db.PostType)PostTypeId; } set { PostTypeId = (int)value; } }

        // not writeable
        public int? PostId { get; private set; }
        public bool? Active { get; private set; }
        public DateTime? Created { get; private set; }
        public DateTime? Modified { get; private set; }

        public int UserId { get; set; }
        public int Child_PostId { get; set; }

        public string UserName { get; set; }
        public string UserTitle { get; set; }

        public int Level { get; set; }

        public Post()
        {
        }

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

        public override string ToString()
        {
            return "(" + PostId + ") " + Message;
        }

        public static Post Parse(System.Net.WebResponse response)
        {
            return new Post(EQNetwork.Services.EQApiService.ParseXml(response).FirstChild);
        }

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

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

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