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 PurchaseTransaction : PropertyChangeTracker
    {
        // not writeable
        public int? PurchaseTransactionId { get; private set; }

        public List<Product> Products { get; private set; }

        public PurchaseTransaction()
        {
        }

        public PurchaseTransaction(XmlNode root)
        {
            XmlNode node = root.FirstChild;
            EQParse.SetPropertiesWithXmlAttributes(this, node);
            Products = Product.ParseList(node);
            ClearPropertyChanges();
        }

        public override string ToString()
        {
            return "Purchase Transaction";
        }

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

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

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