using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EQNetwork;
using EQNetwork.Objects;
using EQNetwork.Services;
using EQNetwork.Utility;

namespace EQNetwork.Controllers
{
    public class User_ListCollections_Controller
    {
        private EQProcess _eq;

        public int? UserId;
        public ListCollectionsContext ListCollectionsContext = new ListCollectionsContext();
        public PagingParameters PagingParameters;

        public List<Collection> Collections = new List<Collection>();
        public bool? HasMore = null;
        
        public User_ListCollections_Controller(EQProcess eq)
        {
            _eq = eq;
        }

        public void Start(Action<EQException> onComplete)
        {
            HasMore = null;

            if (UserId == null)
            {
                _eq.UserService.ListCollections(ListCollectionsContext, PagingParameters,
                    (List<Collection> collections, bool hasMore, EQException ex) => // onComplete
                    {
                        Collections = collections;
                        HasMore = hasMore;
                        onComplete(ex);
                    }
                );
            }
            else
            {
                _eq.UserService.ListCollections((int)UserId, ListCollectionsContext, PagingParameters,
                    (List<Collection> collections, bool hasMore, EQException ex) => // onComplete
                    {
                        Collections = collections;
                        HasMore = hasMore;
                        onComplete(ex);
                    }
                );
            }
        }

        public void More(Action<EQException> onComplete)
        {
            if (HasMore == false)
                return;

            HasMore = null;

            PagingParameters.NextPage();

            if (UserId == null)
            {
                _eq.UserService.ListCollections(ListCollectionsContext, PagingParameters,
                    (List<Collection> collections, bool hasMore, EQException ex) => // onComplete
                    {
                        Collections.AddRange(collections);
                        HasMore = hasMore;
                        onComplete(ex);
                    }
                );
            }
            else
            {
                _eq.UserService.ListCollections((int)UserId, ListCollectionsContext, PagingParameters,
                    (List<Collection> collections, bool hasMore, EQException ex) => // onComplete
                    {
                        Collections.AddRange(collections);
                        HasMore = hasMore;
                        onComplete(ex);
                    }
                );
            }
        }

    }
}
