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

using EQNetwork.Objects;
using EQNetwork.Utility;
using EQNetwork.Aspects;

namespace EQNetwork.Objects
{
    public class PagingParameters : PropertyChangeTracker
    {
        /// <summary>
        /// Sort order 
        /// </summary>
        [TrackPropertyChanged()]
        public string OrderBy { get; set; }

        /// <summary>
        /// Limits results by ordinal position - for paging support (starts at 1 not 0)
        /// </summary>
        int? _page;
        public int? Page { get { return _page; } set { _page = value; CalcStartEnd(); } }

        /// <summary>
        /// Limits results by ordinal position - for paging support
        /// </summary>
        int? _pageSize;
        public int? PageSize { get { return _pageSize; } set { _pageSize = value; CalcStartEnd(); } }

        // Note: Page and PageSize will be replaced with Start and End in the actual API call

        /// <summary>
        /// Limits results by ordinal position - for paging support (starts at 1 not 0)
        /// </summary>
        [TrackPropertyChanged()]
        public int? Start { get; private set; }

        /// <summary>
        /// Limits results by ordinal position - for paging support (starts at 1 not 0)
        /// </summary>
        [TrackPropertyChanged()]
        public int? End { get; private set; }

        // returned by API functions
        public bool hasMore { get; set; }

        public PagingParameters()
        {
        }

        public PagingParameters(int pageSize, string orderBy)
        {
            Page = 1;
            PageSize = pageSize;
            OrderBy = orderBy;
        }

        public void NextPage()
        {
            if (Page == null)
                Page = 1;
            Page++;
        }

        public void CalcStartEnd()
        {
            // convert page and pagesize to start and end
            int? page = _page;
            int? pageSize = _pageSize;
            if (page == null)
                page = 1;
            if (pageSize == null)
                pageSize = 10;
            Start = (page - 1) * pageSize + 1; // get one extra so we can tell if there are "more" 
            End = Start + pageSize;
        }
    }
}
