using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;

using EQNetwork.Objects;
using EQNetwork.Utility;

namespace EQNetwork.Services
{
    public partial class EQApiService : EQNetwork.Interfaces.IEQApiService
    {
        public EQTask ContentFile_Get(int contentFileId, Action<ContentFile, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    ContentFile contentFile = ContentFile_Get(contentFileId);
                    onComplete(contentFile, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public ContentFile ContentFile_Get(int contentFileId)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "ContentFileId", contentFileId
                );
            return new ContentFile(ParseXml(MakeApi2Request("ContentFile_GetNode", parms)).FirstChild);
        }

        public EQTask ContentFile_Get(string filename, Action<ContentFile, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    ContentFile contentFile = ContentFile_Get(filename);
                    onComplete(contentFile, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public ContentFile ContentFile_Get(string filename)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "Filename", filename
                );
            return new ContentFile(ParseXml(MakeApi2Request("ContentFile_GetNode", parms)).FirstChild);
        }

        public EQTask ContentFile_Create(ContentFile contentFile, Action<ContentFile, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    ContentFile_Create(ref contentFile);
                    onComplete(contentFile, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public void ContentFile_Create(ref ContentFile contentFile)
        {
            Parameters parms = new Parameters();
            parms.AddChangedProperties(contentFile);
            int contentFileId = int.Parse(ParseXml(MakeApi2Request("ContentFile_Create", parms)).InnerText);
            contentFile = ContentFile_Get(contentFileId);
        }

        public EQTask ContentFile_CreateFromUrl(ContentFile contentFile, string url, Action<ContentFile, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    ContentFile_CreateFromUrl(ref contentFile, url);
                    onComplete(contentFile, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public void ContentFile_CreateFromUrl(ref ContentFile contentFile, string url)
        {
            Parameters parms = new Parameters(
                "Url", url
                );
            parms.AddChangedProperties(contentFile);
            int contentFileId = int.Parse(ParseXml(MakeApi2Request("ContentFile_Create", parms)).InnerText);
            contentFile = ContentFile_Get(contentFileId);
        }

        public EQTask ContentFile_Delete(ContentFile contentFile, Action<EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    ContentFile_Delete(contentFile);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void ContentFile_Delete(ContentFile contentFile)
        {
            ContentFile_Delete((int)contentFile.ContentFileId);
        }
        public EQTask ContentFile_Delete(int contentFileId, Action<EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    ContentFile_Delete(contentFileId);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void ContentFile_Delete(int contentFileId)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "ContentFileId", contentFileId
                );
            MakeApi2Request("ContentFile_Delete", parms);
        }

        // TODO: implement ContentFile_Update2.aspx
        public EQTask ContentFile_Update(ContentFile contentFile, Action<ContentFile, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    ContentFile_Update(ref contentFile);
                    onComplete(contentFile, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public void ContentFile_Update(ref ContentFile contentFile)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid
                );
            parms.AddChangedProperties(contentFile);
            contentFile.ClearPropertyChanges();
            // ContentFile_Update2 does not need *all* of the parameters passed like ContentFile_Update does
            int contentFileId = int.Parse(ParseXml(MakeApi2Request("ContentFile_Update2", parms)).InnerText);
            contentFile = ContentFile_Get(contentFileId);
        }

        public EQTask ContentFile_UpdateSortBy(int contentFileId, float? sortBy, Action<EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    ContentFile_UpdateSortBy(contentFileId, sortBy);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void ContentFile_UpdateSortBy(int contentFileId, float? sortBy)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "SortBy", sortBy == null ? "" : sortBy.ToString()
                );

            ParseXml(MakeApi2Request("ContentFile_UpdateSortBy", parms));
        }

        public EQTask ContentFile_ChangeCollection(int contentFileId, int old_CollectionId, int new_CollectionId, Action <EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    ContentFile_ChangeCollection(contentFileId, old_CollectionId, new_CollectionId);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void ContentFile_ChangeCollection(int contentFileId, int old_CollectionId, int new_CollectionId)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "ContentFileId", contentFileId,
                "Old_CollectionId", old_CollectionId,
                "New_CollectionId", new_CollectionId
                );
            ParseXml(MakeApi2Request("ContentFile_ChangeCollection", parms));
        }

        public EQTask ContentFile_FlagAsInappropriate(int contentFileId, Action<EQException> onComplete)
        { 
            return new EQTask((EQTask task) =>
            {
                try
                {
                    ContentFile_FlagAsInappropriate(contentFileId);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void ContentFile_FlagAsInappropriate(int contentFileId)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "ContentFileId", contentFileId
                );
            ParseXml(MakeApi2Request("ContentFile_FlagAsInappropriate", parms));
        }

        public EQTask ContentFile_ListPosts(string filename, PagingParameters pagingParameters, Action<List<Post>, bool, EQException> onComplete)
        { 
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<Post> posts = ContentFile_ListPosts(filename, pagingParameters);
                    onComplete(posts, pagingParameters.hasMore, null);
                }
                catch (Exception ex) { onComplete(null, false, new EQException(ex)); }
            });
        }
        public List<Post> ContentFile_ListPosts(string filename, PagingParameters pagingParameters)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "Filename", filename
                );
            parms.AddChangedProperties(pagingParameters);
            return Post.ParseList(pagingParameters, MakeApi2Request("ContentFile_ListPosts", parms));
        }

        public EQTask ContentFile_SocialAdd(int contentFileId, string action, Db.SocialService? exclude, bool? force, Action<EQException> onComplete)
        { 
            return new EQTask((EQTask task) =>
            {
                try
                {
                    ContentFile_SocialAdd(contentFileId, action, exclude, force);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void ContentFile_SocialAdd(int contentFileId, string action, Db.SocialService? exclude, bool? force)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "ContentFileId", contentFileId,
                "Action", action
                );
            if (exclude != null) parms.Add("Exclude", exclude.ToString());
            if (force != null) parms.Add("Force", force);
            ParseXml(MakeApi2Request("ContentFile_SocialAdd", parms));
        }

        public EQTask ContentFile_SocialRemove(int contentFileId, string action, bool? force, Action<EQException> onComplete)
        { 
            return new EQTask((EQTask task) =>
            {
                try
                {
                    ContentFile_SocialRemove(contentFileId, action, force);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void ContentFile_SocialRemove(int contentFileId, string action, bool? force)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "ContentFileId", contentFileId,
                "Action", action
                );
            if (force != null) parms.Add("Force", force);

            ParseXml(MakeApi2Request("ContentFile_SocialRemove", parms));
        }

        public EQTask ContentFile_Viewed(int contentFileId, Action<EQException> onComplete)
        { 
            return new EQTask((EQTask task) =>
            {
                try
                {
                    ContentFile_Viewed(contentFileId);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void ContentFile_Viewed(int contentFileId)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "ContentFileId", contentFileId
                );

            ParseXml(MakeApi2Request("ContentFile_Viewed", parms));
        }
    }
}
