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

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

        [TrackPropertyChanged()]
        public int CollectionTypeId { get; set; }

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

        [TrackPropertyChanged()]
        public string IconFilename { get; set; }

        [TrackPropertyChanged()]
        [SerializeWith("Categories")]
        public List<string> CategoryList { get; set; }
        public string Categories
        {
            get
            {
                return string.Join(",", CategoryList.ToArray());
            }
            set
            {
                CategoryList = new List<string>();
                foreach (var str in value.Split(','))
                    CategoryList.Add(str.Trim());
            }
        }

        [TrackPropertyChanged()]
        [ReplaceNullWithInstanceOf(typeof(FileUploadSpec))] // this will generate a zero length stream using the default constructor
        public FileUploadSpec Icon { private get; set; } // only one way serialization

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

        public int User_CollectionTypeId { get; set; }

        public int FollowersCollection { get; set; }
        public int FollowersUser { get; set; }

        public int OwnerUserId { get; set; }
        public string OwnerUserIconFilename { get; set; }
        public string OwnerUserTitle { get; set; }

        public int ContentFileCount { get; set; }
        public int UnviewedCount { get; set; }
        public DateTime MostRecent { get; set; }
        public int NotificationCount { get; set; }
        public int ViewCount { get; set; }
        public int SortTypeId { get; set; }
        public int NumberOfComments { get; set; }

        public Collection()
        {
        }

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

        public override string ToString()
        {
            return Name;
        }

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

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

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

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