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 Request_Accept(Guid requestGuid, Action<EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Request_Accept(requestGuid);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void Request_Accept(Guid requestGuid)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "RequestGuid", requestGuid
                );
            ParseXml(MakeApi2Request("Request_Accept", parms));
        }

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

        public EQTask Request_Create(RequestContext requestContext, Action<RequestMessage, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    RequestMessage requestMessage = Request_Create(requestContext);
                    onComplete(requestMessage, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public RequestMessage Request_Create(RequestContext requestContext)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid
                );
            parms.AddChangedProperties(requestContext);
            return new RequestMessage(ParseXml(MakeApi2Request("Request_Create", parms)).FirstChild);
        }

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

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

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

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

        public EQTask Request_ListRequestsForUser(int userId, Action<List<Request>, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<Request> requests = Request_ListRequestsForCollection(userId);
                    onComplete(requests, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public List<Request> Request_ListRequestsForUser(int userId)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "UserId", userId
                );
            return Request.ParseList(null, MakeApi2Request("Request_ListRequests", parms));
        }

        public EQTask Request_ListRequestsToMe(List<Db.RequestType> requestTypes, bool? all, Action<List<Request>, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<Request> requests = Request_ListRequestsToMe(requestTypes, all);
                    onComplete(requests, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public List<Request> Request_ListRequestsToMe(List<Db.RequestType> requestTypes, bool? all)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid
                );
            StringBuilder sb = new StringBuilder();
            if (requestTypes != null)
            {
                foreach (var requestType in requestTypes)
                {
                    if (sb.Length > 0) sb.Append(",");
                    sb.Append((int)requestType);
                }
                if (sb.Length > 0)
                    parms.Add("RequestTypeIds", sb.ToString());
            }
            if (all != null)
                parms.Add("All", (bool)all);

            return Request.ParseList(null, MakeApi2Request("Request_ListRequestsToMe", parms));
        }

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

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