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 Search_CollectionAndContentFiles(SearchFilters searchFilters, PagingParameters pagingParameters, Action<List<Collection>, bool, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<Collection> collections = Search_CollectionAndContentFiles(searchFilters, pagingParameters);
                    onComplete(collections, pagingParameters.hasMore, null);
                }
                catch (Exception ex) { onComplete(null, false, new EQException(ex)); }
            });
        }
        public List<Collection> Search_CollectionAndContentFiles(SearchFilters searchFilters, PagingParameters pagingParameters)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid
                );
            parms.AddChangedProperties(searchFilters);
            parms.AddChangedProperties(pagingParameters);
            return Collection.ParseList(pagingParameters, MakeApi2Request("Search_CollectionAndContentFiles", parms));
        }

        public EQTask Search_ContentFiles(SearchFilters searchFilters, PagingParameters pagingParameters, Action<List<ContentFile>, bool, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    List<ContentFile> contentFiles = Search_ContentFiles(searchFilters, pagingParameters);
                    onComplete(contentFiles, pagingParameters.hasMore, null);
                }
                catch (Exception ex) { onComplete(null, false, new EQException(ex)); }
            });
        }
        public List<ContentFile> Search_ContentFiles(SearchFilters searchFilters, PagingParameters pagingParameters)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid
                );
            parms.AddChangedProperties(searchFilters);
            parms.AddChangedProperties(pagingParameters);
            return ContentFile.ParseList(pagingParameters, MakeApi2Request("Search_ContentFiles", parms));
        }

        public EQTask Search_ContentFiles(string filename, Action<ContentFile, EQException> onComplete)
        {
            return new EQTask((EQTask task) =>
            {
                try
                {
                    ContentFile contentFile = Search_ContentFiles(filename);
                    onComplete(contentFile, null);
                }
                catch (Exception ex) { onComplete(null, new EQException(ex)); }
            });
        }
        public ContentFile Search_ContentFiles(string filename)
        {
            Parameters parms = new Parameters(
                "SessionGuid", _eq.SessionGuid,
                "Filename", filename
                );
            List<ContentFile> contentFiles = ContentFile.ParseList(null, MakeApi2Request("Search_CollectionAndContentFiles", parms));
            return contentFiles[0];
        }
    }
}