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

using skydiveLogs_api.Business.Interface;
using skydiveLogs_api.Data.Interface;
using skydiveLogs_api.Model;


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);

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

        public IEnumerable<Statistic> GetStatsByDz(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);

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

        public IEnumerable<Statistic> GetStatsByJumpType(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);

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

        public IEnumerable<Statistic> GetStatsByGear(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);

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

        public IEnumerable<Statistic> GetStatsByYear(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);

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

        public IEnumerable<Statistic> GetStatsForLastYearByDz(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);

            var lastJump = allJumps.OrderByDescending(j => j.JumpDate).FirstOrDefault();
            var yearOfLastJump = lastJump.JumpDate.Year;

            return 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();
        }

        public IEnumerable<Statistic> GetStatsForLastYearByJumpType(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);

            var lastJump = allJumps.OrderByDescending(j => j.JumpDate).FirstOrDefault();
            var yearOfLastJump = lastJump.JumpDate.Year;

            return 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();
        }

        public IEnumerable<Statistic> GetStatsForLastMonthByDz(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);

            var lastJump = allJumps.OrderByDescending(j => j.JumpDate).FirstOrDefault();
            var yearOfLastJump = lastJump.JumpDate.Year;
            var monthOfLastJump = lastJump.JumpDate.Month;

            return 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();
        }

        public IEnumerable<Statistic> GetStatsForLastMonthByJumpType(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);

            var lastJump = allJumps.OrderByDescending(j => j.JumpDate).FirstOrDefault();
            var yearOfLastJump = lastJump.JumpDate.Year;
            var monthOfLastJump = lastJump.JumpDate.Month;

            return 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();
        }

        public SimpleSummary GetSimpleSummary(User connectedUser)
        {
            var allJumps = _jumpService.GetAllJumps(connectedUser);

            var lastJump = allJumps.OrderByDescending(j => j.JumpDate).FirstOrDefault();

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

        private readonly IJumpService _jumpService;
    }
}
