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;
using EQNetwork.Interfaces;

namespace EQNetwork.Services
{
    public partial class EQApiService : IEQApiService
    {
        public EQTask Invitation_Accept(Guid invitationGuid, Action<EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Invitation_Accept(invitationGuid);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void Invitation_Accept(Guid invitationGuid)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "InvitationGuid", invitationGuid
                );
            ParseXml(MakeApi2Request("Invitation_Accept", parms));
        }

        public EQTask Invitation_Confirm(Guid invitationGuid, Action<EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Invitation_Confirm(invitationGuid);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void Invitation_Confirm(Guid invitationGuid)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "InvitationGuid", invitationGuid
                );
            ParseXml(MakeApi2Request("Invitation_Confirm", parms));
        }

        public EQTask Invitation_Create(InvitationContext invitationContext, Action<List<InvitationMessage>, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<InvitationMessage> invitationMessage = Invitation_Create(invitationContext);
                    onComplete(invitationMessage, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public List<InvitationMessage> Invitation_Create(InvitationContext invitationContext)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid
                );
            parms.AddChangedProperties(invitationContext);
            return InvitationMessage.ParseList(null, MakeApi2Request("Invitation_Create", parms));
        }

        public EQTask Invitation_Decline(Guid invitationGuid, Action<EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Invitation_Decline(invitationGuid);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void Invitation_Decline(Guid invitationGuid)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "InvitationGuid", invitationGuid
                );
            ParseXml(MakeApi2Request("Invitation_Decline", parms));
        }

        public EQTask Invitation_Delete(Guid invitationGuid, Action<EQException> onComplete) 
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Invitation_Delete(invitationGuid);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void Invitation_Delete(Guid invitationGuid)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "InvitationGuid", invitationGuid
                );
            ParseXml(MakeApi2Request("Invitation_Delete", parms));
        }

        public EQTask Invitation_ListInvitationsForCollection(int collectionId, Action<List<Invitation>, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<Invitation> invitations = Invitation_ListInvitationsForCollection(collectionId);
                    onComplete(invitations, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public List<Invitation> Invitation_ListInvitationsForCollection(int collectionId)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "CollectionId", collectionId
                );
            return Invitation.ParseList(null, MakeApi2Request("Invitation_ListInvitations", parms));
        }

        public EQTask Invitation_ListInvitationsForCollection(Guid collectionGuid, Action<List<Invitation>, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<Invitation> invitations = Invitation_ListInvitationsForCollection(collectionGuid);
                    onComplete(invitations, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public List<Invitation> Invitation_ListInvitationsForCollection(Guid collectionGuid)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "CollectionGuid", collectionGuid
                );
            return Invitation.ParseList(null, MakeApi2Request("Invitation_ListInvitations", parms));
        }

        public EQTask Invitation_ListInvitationsForUser(int userId, Action<List<Invitation>, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<Invitation> invitations = Invitation_ListInvitationsForUser(userId);
                    onComplete(invitations, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public List<Invitation> Invitation_ListInvitationsForUser(int userId)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "UserId", userId
                );
            return Invitation.ParseList(null, MakeApi2Request("Invitation_ListInvitations", parms));
        }

        public EQTask Invitation_ListInvitationsToMe(List<Db.InvitationType> invitationTypes, bool? all, Action<List<Invitation>, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<Invitation> invitations = Invitation_ListInvitationsToMe(invitationTypes, all);
                    onComplete(invitations, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public List<Invitation> Invitation_ListInvitationsToMe(List<Db.InvitationType> invitationTypes, bool? all)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid
                );
            StringBuilder sb = new StringBuilder();
            if (invitationTypes != null)
            {
                foreach (var invitationType in invitationTypes)
                {
                    if (sb.Length > 0) sb.Append(",");
                    sb.Append((int)invitationType);
                }
                if (sb.Length > 0)
                    parms.Add("InvitationTypeIds", sb.ToString());
            }
            if (all != null)
                parms.Add("All", (bool)all);

            return Invitation.ParseList(null, MakeApi2Request("Invitation_ListInvitationsToMe", parms));
        }

        public EQTask Invitation_Resend(Guid invitationGuid, Action<EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Invitation_Resend(invitationGuid);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void Invitation_Resend(Guid invitationGuid)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "InvitationGuid", invitationGuid
                );
            ParseXml(MakeApi2Request("Invitation_Resend", parms));
        }

        public EQTask Invitation_Update(Guid invitationGuid, Action<EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Invitation_Update(invitationGuid);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void Invitation_Update(Guid invitationGuid)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "InvitationGuid", invitationGuid
                );
            ParseXml(MakeApi2Request("Invitation_Update", parms));
        }
    }
}
