using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainBusiness.Interfaces;
using System.Collections.Generic;
using System.Linq;

namespace skydiveLogs_api.DomainBusiness
{
    public class StatsService : IStatsService
    {
        #region Public Constructors

        public StatsService(IJumpService jumpService)
        {
            _jumpService = jumpService;
        }

        #endregion Public Constructors

        #region Public Methods

        public SimpleSummary GetSimpleSummary()
        {
            var allJumps = _jumpService.GetAllJumps();
            var results = new SimpleSummary();

            if (allJumps.Any())
            {
                var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();

                results = new SimpleSummary
                {
                    LastJump = lastJump,
                    TotalJumps = allJumps.Count(),
                    TotalCutaways = allJumps.Where(j => j.WithCutaway).Count()
                };
            }

            return results;
        }

        public IEnumerable<Statistic> GetStatsByAircraft()
        {
            var allJumps = _jumpService.GetAllJumps();
            var results = new List<Statistic>();

            if (allJumps.Any())
            {
                results = allJumps.GroupBy(j => j.Aircraft.Name,
                                           j => j,
                                           (groupby, jumps) => new Statistic
                                           {
                                               Label = groupby.ToString(),
                                               Nb = jumps.Count()
                                           })
                                  .ToList();
            }

            return results;
        }

        public IEnumerable<Statistic> GetStatsByDz()
        {
            var allJumps = _jumpService.GetAllJumps();
            var results = new List<Statistic>();

            if (allJumps.Any())
            {
                results = allJumps.GroupBy(j => j.DropZone.Name,
                                           j => j,
                                           (groupby, jumps) => new Statistic
                                           {
                                               Label = groupby.ToString(),
                                               Nb = jumps.Count()
                                           })
                                  .ToList();
            }

            return results;
        }

        public IEnumerable<Statistic> GetStatsByGear()
        {
            var allJumps = _jumpService.GetAllJumps();
            var results = new List<Statistic>();

            if (allJumps.Any())
            {
                results = allJumps.GroupBy(j => j.Gear.Name,
                                           j => j,
                                           (groupby, jumps) => new Statistic
                                           {
                                               Label = groupby.ToString(),
                                               Nb = jumps.Count()
                                           })
                                  .ToList();
            }

            return results;
        }

        public IEnumerable<Statistic> GetStatsByJumpType()
        {
            var allJumps = _jumpService.GetAllJumps();
            var results = new List<Statistic>();

            if (allJumps.Any())
            {
                results = allJumps.GroupBy(j => j.JumpType.Name,
                                           j => j,
                                           (groupby, jumps) => new Statistic
                                           {
                                               Label = groupby.ToString(),
                                               Nb = jumps.Count()
                                           })
                                  .ToList();
            }

            return results;
        }

        public IEnumerable<Statistic> GetStatsByYear()
        {
            var allJumps = _jumpService.GetAllJumps();
            var results = new List<Statistic>();

            if (allJumps.Any())
            {
                results = allJumps.GroupBy(j => j.JumpDate.Year,
                                           j => j,
                                           (groupby, jumps) => new Statistic
                                           {
                                               Label = groupby.ToString(),
                                               Nb = jumps.Count()
                                           })
                                  .ToList();
            }

            return results;
        }

        public IEnumerable<Statistic> GetStatsForLastMonthByDz()
        {
            var allJumps = _jumpService.GetAllJumps();
            var results = new List<Statistic>();

            if (allJumps.Any())
            {
                var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
                var yearOfLastJump = lastJump.JumpDate.Year;
                var monthOfLastJump = lastJump.JumpDate.Month;

                results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump && j.JumpDate.Month == monthOfLastJump)
                                  .GroupBy(j => j.DropZone.Name,
                                           j => j,
                                           (groupby, jumps) => new Statistic
                                           {
                                               Label = groupby.ToString(),
                                               Nb = jumps.Count()
                                           })
                                  .ToList();
            }

            return results;
        }

        public IEnumerable<Statistic> GetStatsForLastMonthByJumpType()
        {
            var allJumps = _jumpService.GetAllJumps();
            var results = new List<Statistic>();

            if (allJumps.Any())
            {
                var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
                var yearOfLastJump = lastJump.JumpDate.Year;
                var monthOfLastJump = lastJump.JumpDate.Month;

                results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump && j.JumpDate.Month == monthOfLastJump)
                                  .GroupBy(j => j.JumpType.Name,
                                           j => j,
                                           (groupby, jumps) => new Statistic
                                           {
                                               Label = groupby.ToString(),
                                               Nb = jumps.Count()
                                           })
                                  .ToList();
            }

            return results;
        }

        public IEnumerable<Statistic> GetStatsForLastYearByDz()
        {
            var allJumps = _jumpService.GetAllJumps();
            var results = new List<Statistic>();

            if (allJumps.Any())
            {
                var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
                var yearOfLastJump = lastJump.JumpDate.Year;

                results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump)
                                  .GroupBy(j => j.DropZone.Name,
                                           j => j,
                                           (groupby, jumps) => new Statistic
                                           {
                                               Label = groupby.ToString(),
                                               Nb = jumps.Count()
                                           })
                                  .ToList();
            }

            return results;
        }

        public IEnumerable<Statistic> GetStatsForLastYearByJumpType()
        {
            var allJumps = _jumpService.GetAllJumps();
            var results = new List<Statistic>();

            if (allJumps.Any())
            {
                var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
                var yearOfLastJump = lastJump.JumpDate.Year;

                results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump)
                                  .GroupBy(j => j.JumpType.Name,
                                           j => j,
                                           (groupby, jumps) => new Statistic
                                           {
                                               Label = groupby.ToString(),
                                               Nb = jumps.Count()
                                           })
                                  .ToList();
            }

            return results;
        }

        #endregion Public Methods

        #region Private Fields

        private readonly IJumpService _jumpService;

        #endregion Private Fields
    }
}