using System;
using System.Collections.Generic;
using LiteDB;
using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainService.Repositories;
using skydiveLogs_api.Infrastructure.Interfaces;

namespace skydiveLogs_api.Infrastructure
{
    public class TunnelFlightRepository : ITunnelFlightRepository
    {
        #region Public Constructors

        public TunnelFlightRepository(IDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
            _col = _dataProvider.CollOfTunnelFlight;
        }

        #endregion Public Constructors

        #region Public Methods

        /// <summary>
        /// Adds a new tunnel flight to the database.
        /// </summary>
        /// <param name="newTunnelFlight">The tunnel flight instance to insert into the database.</param>
        /// <returns>The number of rows affected. Returns 0 if the insert operation failed.</returns>
        public int Add(TunnelFlight newTunnelFlight)
        {
            int result;

            try
            {
                var tmp = _col.Insert(newTunnelFlight);
                result = tmp.AsInt32;
            }
            catch
            {
                result = 0;
            }

            return result;
        }

        /// <summary>
        /// Deletes a tunnel flight by its unique identifier.
        /// </summary>
        /// <param name="id">The unique identifier of the tunnel flight to delete.</param>
        /// <returns><see langword="true"/> if the deletion was successful; otherwise, <see langword="false"/>.</returns>
        public bool DeleteById(int id)
        {
            return _col.Delete(new BsonValue(id));
        }

        /// <summary>
        /// Retrieves all tunnel flights for a specific user.
        /// </summary>
        /// <param name="user">The user whose tunnel flight records to retrieve.</param>
        /// <returns>An enumerable collection containing all tunnel flight records belonging to the specified user.</returns>
        public IEnumerable<TunnelFlight> GetAll(User user)
        {
            return _col.Include(x => x.Tunnel)
                       .Include(x => x.JumpType)
                       .Find(j => j.User.Id == user.Id);
        }

        /// <summary>
        /// Retrieves all tunnel flights from the database.
        /// </summary>
        /// <returns>An enumerable collection containing all tunnel flight records stored in the database.</returns>
        public IEnumerable<TunnelFlight> GetAll()
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Retrieves a range of tunnel flights for a specific user with pagination.
        /// </summary>
        /// <param name="user">The user whose tunnel flight records to retrieve.</param>
        /// <param name="beginIndex">The starting index for pagination (inclusive).</param>
        /// <param name="endIndex">The ending index for pagination (exclusive).</param>
        /// <returns>An enumerable collection containing the requested range of tunnel flight records.</returns>
        public IEnumerable<TunnelFlight> GetBetweenIndex(User user, int beginIndex, int endIndex)
        {
            return _col.Include(x => x.Tunnel)
                       .Include(x => x.JumpType)
                       .Query()
                       .OrderByDescending(j => j.FlightDate)
                       .Where(j => j.User.Id == user.Id)
                       .Limit(endIndex - beginIndex)
                       .Offset(beginIndex)
                       .ToList();
        }

        /// <summary>
        /// Retrieves a range of tunnel flights within a date range for a specific user.
        /// </summary>
        /// <param name="user">The user whose tunnel flight records to retrieve.</param>
        /// <param name="beginDate">The start of the date range (inclusive).</param>
        /// <param name="endDate">The end of the date range (exclusive).</param>
        /// <returns>An enumerable collection containing the requested range of tunnel flight records within the specified date range.</returns>
        public IEnumerable<TunnelFlight> GetBetweenDate(User user, DateTime beginDate, DateTime endDate)
        {
            return _col.Include(x => x.Tunnel)
                       .Include(x => x.JumpType)
                       .Query()
                       .OrderByDescending(j => j.FlightDate)
                       .Where(j => j.FlightDate >= beginDate)
                       .Where(j => j.FlightDate <= endDate)
                       .ToList();
        }

        /// <summary>
        /// Retrieves a tunnel flight by its unique identifier.
        /// </summary>
        /// <param name="id">The unique identifier of the tunnel flight to retrieve.</param>
        /// <returns>The tunnel flight record instance if found, otherwise null.</returns>
        public TunnelFlight GetById(int id)
        {
            return _col.FindById(new BsonValue(id));
        }

        /// <summary>
        /// Gets the total count of tunnel flights for a specific user.
        /// </summary>
        /// <param name="user">The user whose tunnel flight records to count.</param>
        /// <returns>The total number of tunnel flight records for the specified user.</returns>
        public int GetCount(User user)
        {
            return _col.Count(j => j.User.Id == user.Id);
        }

        /// <summary>
        /// Gets the total count of tunnel flights in the database.
        /// </summary>
        /// <returns>The total number of tunnel flight records stored in the database.</returns>
        public int GetCount()
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Updates an existing tunnel flight record in the database.
        /// </summary>
        /// <param name="updatedTunnelFlight">The tunnel flight instance containing the updated data.</param>
        /// <returns><see langword="true"/> if the update was successful; otherwise, <see langword="false"/>.</returns>
        public bool Update(TunnelFlight updatedTunnelFlight)
        {
            return _col.Update(updatedTunnelFlight);
        }

        #endregion Public Methods

        #region Private Fields

        private readonly ILiteCollection<TunnelFlight> _col;

        private readonly IDataProvider _dataProvider;

        #endregion Private Fields
    }
}
