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 Post_Create(Post post, Action<Post, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Post_Create(ref post);
                    onComplete(post, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public void Post_Create(ref Post post)
        {
            Parameters parms = new Parameters();
            parms.AddChangedProperties(post);
            int postId = int.Parse(ParseXml(MakeApi2Request("Post_Create", parms)).InnerText);
            post = Post_Get(postId);
        }

        // TODO: Post_Update2.aspx
        public EQTask Post_Update(Post post, Action<Post, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Post_Update(ref post);
                    onComplete(post, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public void Post_Update(ref Post post)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid
                );
            parms.AddChangedProperties(post);
            post.ClearPropertyChanges();
            // Post_Update2 does not need *all* of the parameters passed like Post_Update does
            int postId = int.Parse(ParseXml(MakeApi2Request("Post_Update2", parms)).InnerText);
            post = Post_Get(postId);
        }

        // TODO: need Post_Get.aspx
        public EQTask Post_Get(int postId, Action<Post, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Post post = Post_Get(postId);
                    onComplete(post, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public Post Post_Get(int postId)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "PostId", postId
                );
            return new Post(ParseXml(MakeApi2Request("Post_GetNode", parms)).FirstChild);
        }

        public EQTask Post_Delete(Post post, Action<EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Post_Delete(post);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void Post_Delete(Post post)
        {
            Post_Delete((int)post.PostId);
        }

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