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 Upload
    {
        // the following fields are returned my many of the upload api calls

        public int? UploadId { get; set; }
        public bool? Active { get; set; }
        public DateTime? Created { get; set; }
        public DateTime? Modified { get; set; }
        public int? UserId { get; set; }
        public long? Checksum { get; set; }
        public long? FileSize { get; set; }
        public long? UploadedSoFar { get; set; }
        public int? UploadStateId { get; set; }
        public string OriginalFilepath { get; set; }
        public string Name { get; set; }
        public string Filename { get; set; }
        public int? ContentFileId { get; set; }
        public string DeliveryRequestId { get; set; }
        public string DownloadFilepath { get; set; }
        public DateTime? LastUserNotification { get; set; }
        public long? LastUserNotificationSpot { get; set; }

        public List<UploadChunk> UploadChunks = new List<UploadChunk>();

        public Upload()
        {
        }

        public Upload(XmlNode node)
        {
            if (node.Name == "Upload") // Upload can be at two different levels because of Begin call
                EQParse.SetPropertiesWithXmlAttributes(this, node);

            foreach (XmlNode child in node.ChildNodes)
            {
                if (child.Name == "Upload")
                {
                    EQParse.SetPropertiesWithXmlAttributes(this, child);
                }
                else if (child.Name == "UploadChunks")
                {
                    foreach (XmlNode child2 in child.ChildNodes)
                        UploadChunks.Add(new UploadChunk(child2));
                }
            }
        }

        public override string ToString()
        {
            return Name;
        }

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

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

            foreach (XmlNode node in root.ChildNodes)
            {
                if (node.Name.Equals("Uploads", StringComparison.OrdinalIgnoreCase))
                {
                    foreach (XmlNode child in node.ChildNodes) 
                        list.Add(new Upload(child));
                }
            }
            return list;
        }

    }
}
