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

        // fields that can be set on the backend 
        [TrackPropertyChanged()]
        public int CollectionId { get; set; }

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

        [TrackPropertyChanged()]
        public string VideoFilename { get; set; }
        [TrackPropertyChanged()]
        public string ThumbnailFilename { 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 Video { private get; set; } // only one way serialization

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

        // not writeable
        public int? ContentFileId { get; private 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 string MuxKey { get; set; }

        public int? UserId { get; set; }
        public string Keywords { get; set; }
        public int? ContentFileTypeId { get; set; }
        public int? DeliveryRequestId { get; set; }
        public string JdiResult { get; set; }
        public bool? IsAdult { get; set; }
        public int? ViewCount { get; set; }
        public string OriginalUrl { get; set; }
        public long? OriginalFilesize { get; set; }
        public string DownloadUrl { get; set; }
        public int? Width { get; set; }
        public int? Height { get; set; }
        public float? Duration { get; set; }
        public string Thumbnail { get; set; }
        public bool? IsFlashVideo { get; set; }
        public string Filename { get; set; }
        public string CurrentMuxKey { get; set; }

        public ContentFile()
        {
        }

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

        public override string ToString()
        {
            return Name;
        }

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

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

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

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