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 Category : 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 CategoryTypeId { get; set; }

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

        


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

        public Category()
        {
        }

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

        public override string ToString()
        {
            return "(" + CategoryId + ") " + Name;
        }

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

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

            List<Category> list = new List<Category>();

            foreach (XmlNode node in root.ChildNodes)
            {
                if (node.Name.Equals("Categories", StringComparison.OrdinalIgnoreCase))
                {
                    foreach (XmlNode child in root.ChildNodes)
                    {
                        if (child.Name.Equals("Category", StringComparison.OrdinalIgnoreCase))
                        {
                            int n = child.ChildNodes.Count;
                            for (int i = 0; i < n; i++)
                                list.Add(new Category(child.ChildNodes[i]));
                        }
                    }
                }
            }
            return list;
        }
    }
}
