﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Prasatec.Cu2Com.Data
{
    public sealed class PermissionCollection : Raden.ICollection<Permission>
    {
        private const int DEFAULT_LIMIT = 25;
        public PermissionCollection()
            : this(new Raden.QueryBuilder<Permission>().Build())
        {

        }
        public PermissionCollection(Raden.IQuery Query)
        {
            this.baseQuery = Query;
        }

        private int i_PageIndex, i_RecordsPerPage;
        private Raden.IQuery baseQuery;

        public event EventHandler ListRefreshed;

        public void Refresh()
        {
            this.ListRefreshed?.Invoke(this, EventArgs.Empty);
            throw new NotImplementedException();
        }

        public string[] Columns { get; private set; }
        public Permission[] Records { get; private set; }

        public int PageCount { get; private set; }
        public int PageIndex
        {
            get { return this.i_PageIndex; }

            set
            {
                if (value > this.PageCount) { value = PageCount; }
                if (value < 1) { value = 1; }
                this.Refresh();
            }
        }

        public int RecordsPerPage
        {
            get
            {
                return this.i_RecordsPerPage;
            }

            set
            {
                this.i_RecordsPerPage = value;
                this.i_PageIndex = 1;
                this.Refresh();
            }
        }

        public bool Next()
        {
            if (this.PageIndex != this.PageCount)
            {
                this.PageIndex++;
                return true;
            }
            return false;
        }

        public bool Previous()
        {
            if (this.PageIndex != 1)
            {
                this.PageIndex--;
                return true;
            }
            return false;
        }

        public void FirstPage()
        {
            if (this.PageIndex != 1)
            {
                this.PageIndex = 1;
            }
        }

        public void LastPage()
        {
            if (this.PageIndex != this.PageCount)
            {
                this.PageIndex = this.PageCount;
            }
        }

        public void Reload()
        {
            this.i_PageIndex = 1;
            this.i_RecordsPerPage = DEFAULT_LIMIT;
            this.Refresh();
        }
    }
}