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 Collection_Get(int collectionId, Action<Collection, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try 
                {
                    Collection collection = Collection_Get(collectionId);
                    onComplete(collection, null);
                } catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public Collection Collection_Get(int collectionId)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "CollectionId", collectionId
                );
            return Collection.Parse(MakeApi2Request("Collection_GetNode", parms));
        }

        public EQTask Collection_Get(Guid collectionGuid, Action<Collection, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try 
                {
                    Collection collection = Collection_Get(collectionGuid);
                    onComplete(collection, null);
                } catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public Collection Collection_Get(Guid collectionGuid)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "CollectionGuid", collectionGuid
                );
            return Collection.Parse(MakeApi2Request("Collection_GetNode", parms));
        }

        public EQTask Collection_Create(Collection collection, Action<Collection, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Collection_Create(ref collection);
                    onComplete(collection, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public void Collection_Create(ref Collection collection)
        {
            Parameters parms = new Parameters();
            parms.AddChangedProperties(collection);
            int collectionId = int.Parse(ParseXml(MakeApi2Request("Collection_Create", parms)).InnerText);
            collection = Collection_Get(collectionId);
        }

        public EQTask Collection_Delete(Collection collection, Action<EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Collection_Delete(collection);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void Collection_Delete(Collection collection)
        {
            Collection_Delete((int)collection.CollectionId);
        }

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

        // TODO: implement Collection_Update2.aspx
        public EQTask Collection_Update(Collection collection, Action<Collection, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Collection_Update(ref collection);
                    onComplete(collection, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public void Collection_Update(ref Collection collection)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid
                );
            parms.AddChangedProperties(collection);
            collection.ClearPropertyChanges();
            collection = Collection.Parse(MakeApi2Request("Collection_Update2", parms)); // Collection_Update2 does not need *all* of the parameters passed like Collection_Update does
        }

        public EQTask Collection_ListContentFiles(int collectionId,
            ListContentFilesContext listContentFilesContext,
            PagingParameters pagingParameters,
            Action<List<ContentFile>, bool, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<ContentFile> contentFiles = Collection_ListContentFiles(collectionId, listContentFilesContext, pagingParameters);
                    onComplete(contentFiles, pagingParameters.hasMore, null);
                }
                catch (Exception ex) { onComplete(null, false, new EQException(ex)); }
            });
        }
        public List<ContentFile> Collection_ListContentFiles(int collectionId,
            ListContentFilesContext listContentFilesContext,
            PagingParameters pagingParameters)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid
                );
            parms.Add("CollectionId", collectionId);
            parms.AddChangedProperties(listContentFilesContext);
            parms.AddChangedProperties(pagingParameters);
            return ContentFile.ParseList(pagingParameters, MakeApi2Request("Collection_ListContentFiles", parms));
        }

        public EQTask Collection_AddRelationship(int collectionId, Db.User_CollectionType user_CollectionType, Action<EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Collection_AddRelationship(collectionId, user_CollectionType);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void Collection_AddRelationship(int collectionId, Db.User_CollectionType user_CollectionType)
        {
            Collection_AddRelationship(collectionId, user_CollectionType, null, null);
        }

        public EQTask Collection_AddRelationship(int collectionId, Db.User_CollectionType user_CollectionType,
            int? collectionTriggerId, bool? silent, Action<EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Collection_AddRelationship(collectionId, user_CollectionType, collectionTriggerId, silent);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void Collection_AddRelationship(int collectionId, Db.User_CollectionType user_CollectionType,
            int? collectionTriggerId, bool? silent)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "CollectionId", collectionId,
                "User_CollectionTypeId", (int)user_CollectionType
                );
            if (collectionTriggerId != null) parms.Add("CollectionTriggerId", collectionTriggerId);
            if (silent != null) parms.Add("Silent", silent);
            ParseXml(MakeApi2Request("Collection_AddRelationship", parms));
        }

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

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

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