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 Session_Create(string username, string password, Action<Session, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Session session = Session_Create(username, password);
                    onComplete(session, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public Session Session_Create(string username, string password)
        {
            Parameters parms = new Parameters(
                "Username", username,
                "Password", password
                );
            return new Session(ParseXml(MakeApi2Request("Session_Create", parms)));
        }

        public EQTask Session_Check(Guid sessionGuid, Action<Session, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Session session = Session_Check(sessionGuid);
                    onComplete(session, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public Session Session_Check(Guid sessionGuid)
        {
            Parameters parms = new Parameters(
                "SessionGuid", sessionGuid
                );
            return new Session(ParseXml(MakeApi2Request("Session_Check", parms)));
        }

        public EQTask Session_CreateFromFacebook(string fbAccessToken, DateTime fbExpirationDate, string email, Action<Session, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Session session = Session_CreateFromFacebook(fbAccessToken, fbExpirationDate, email);
                    onComplete(session, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public Session Session_CreateFromFacebook(string fbAccessToken, DateTime fbExpirationDate, string email)
        {
            Parameters parms = new Parameters(
                "FbAccessToken", fbAccessToken,
                "fbExpirationDate", fbExpirationDate,
                "Email", email
                );
            return new Session(ParseXml(MakeApi2Request("Session_CreateFromFacebook", parms)));
        }

        public EQTask Session_CreateFromFacebook(string code, string redirect_Url, string email, Action<Session, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Session session = Session_CreateFromFacebook(code, redirect_Url, email);
                    onComplete(session, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public Session Session_CreateFromFacebook(string code, string redirect_Url, string email)
        {
            Parameters parms = new Parameters(
                "Code", code,
                "Redirect_Url", redirect_Url,
                "Email", email
                );
            return new Session(ParseXml(MakeApi2Request("Session_CreateFromFacebook", parms)));
        }

        public EQTask Session_CreateFromTwitter(string twUserId, string twScreenName, string twOAuthToken, string twOAuthTokenSecret, string email, Action<Session, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Session session = Session_CreateFromTwitter(twUserId, twScreenName, twOAuthToken, twOAuthTokenSecret, email);
                    onComplete(session, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public Session Session_CreateFromTwitter(string twUserId, string twScreenName, string twOAuthToken, string twOAuthTokenSecret, string email)
        {
            Parameters parms = new Parameters(
                "TwUserId", twUserId,
                "TwScreenName", twScreenName,
                "TwOAuthToken", twOAuthToken,
                "TwOAuthTokenSecret", twOAuthTokenSecret,
                "Email", email
                );
            return new Session(ParseXml(MakeApi2Request("Session_CreateFromTwitter", parms)));
        }
    }
}
