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 Activity_Create(string activity, string secondary, Action<EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    Activity_Create(activity, secondary);
                    onComplete(null);
                }
                catch (Exception ex) { onComplete(new EQException(ex)); }
            });
        }
        public void Activity_Create(string activity, string secondary)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "Activity", activity
                );
            if (secondary != null)
                parms.Add("Secondary", secondary);
            ParseXml(MakeApi2Request("Activity_Create", parms));
        }

        public EQTask ContentMenu_List(DateTime? modifiedSince, Action<List<ContentMenu>, DateTime?, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<ContentMenu> contentMenus = ContentMenu_List(ref modifiedSince);
                    onComplete(contentMenus, modifiedSince, null);
                }
                catch (Exception ex) { onComplete(null, null, new EQException(ex)); }
            });
        }
        public List<ContentMenu> ContentMenu_List(ref DateTime? modifiedSince)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid
                );
            if (modifiedSince != null)
                parms.Add("ModifiedSince", modifiedSince);

            return ContentMenu.ParseList(ref modifiedSince, null, MakeApi2Request("ContentMenu_List", parms));
        }

        public EQTask Hashtag_Search(string search, PagingParameters pagingParameters, Action<List<Hashtag>, bool, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<Hashtag> hashtags = Hashtag_Search(search, pagingParameters);
                    onComplete(hashtags, pagingParameters.hasMore, null);
                }
                catch (Exception ex) { onComplete(null, false, new EQException(ex)); }
            });
        }
        public List<Hashtag> Hashtag_Search(string search, PagingParameters pagingParameters)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid
                );
            if (search != null) parms.Add("Search", search);
            parms.AddChangedProperties(pagingParameters);

            return Hashtag.ParseList(pagingParameters, MakeApi2Request("Hashtag_Search", parms));
        }

        public EQTask Hashtag_Trending(PagingParameters pagingParameters, Action<List<Hashtag>, bool, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<Hashtag> hashtags = Hashtag_Trending(pagingParameters);
                    onComplete(hashtags, pagingParameters.hasMore, null);
                }
                catch (Exception ex) { onComplete(null, false, new EQException(ex)); }
            });
        }
        public List<Hashtag> Hashtag_Trending(PagingParameters pagingParameters)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid
                );
            parms.AddChangedProperties(pagingParameters);
            return Hashtag.ParseList(pagingParameters, MakeApi2Request("Hashtag_Trending", parms));
        }
        public EQTask Category_ListAll(Action<List<Category>, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<Category> categories = Category_ListAll();
                    onComplete(categories, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public List<Category> Category_ListAll()
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid
                );
            return Category.ParseList(MakeApi2Request("Category_ListAll", parms));
        }
    }
}
