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

using skydiveLogs_api.Business.Interfaces;
using skydiveLogs_api.Domain;


namespace skydiveLogs_api.Business
{
    public class StatsService : IStatsService
    {
        public StatsService(IJumpService jumpService)
        {
            _jumpService = jumpService;
        }

        public IEnumerable<Statistic> GetStatsByAircraft(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);
            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(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);
            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> GetStatsByJumpType(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);
            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> GetStatsByGear(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);
            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> GetStatsByYear(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);
            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> GetStatsForLastYearByDz(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);
            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(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);
            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;
        }

        public IEnumerable<Statistic> GetStatsForLastMonthByDz(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);
            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(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);
            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 SimpleSummary GetSimpleSummary(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);
            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;
        }

        private readonly IJumpService _jumpService;
    }
}
