// Calculate most viewed videos
        public List <ViewsPerVideoViewModel> GetMostViewedVideos()
        {
            var videoIds     = HistoryVideos.Select(v => v.GetVideoID()).Distinct().ToList();
            var timesWatched = videoIds.Select(id => HistoryVideos.Count(v => v.GetVideoID() == id)).ToList();

            List <ViewsPerVideoViewModel> viewsPerVideoViewModels = new List <ViewsPerVideoViewModel>();

            for (int i = 0; i < videoIds.Count; i++)
            {
                viewsPerVideoViewModels.Add(new ViewsPerVideoViewModel()
                {
                    VideoViewModel = VideoViewModelsDict[videoIds[i]],
                    NumViews       = timesWatched[i]
                });
            }

            viewsPerVideoViewModels = viewsPerVideoViewModels.OrderByDescending(vm => vm.NumViews).ToList();
            //using (StreamWriter sw = new StreamWriter(UriHelper.CALCULATIONS_OUTPUT_DIR + "/Views Per Video - " + DateTime.Now.ToFileTime() + ".csv"))
            //{
            //    foreach (var viewModel in viewsPerVideoViewModels)
            //    {
            //        sw.WriteLine(viewModel.VideoViewModel.VideoTitle + ";" + viewModel.NumViews);
            //    }
            //}

            return(viewsPerVideoViewModels.Take(10).ToList());
        }
        // Get average length of video in history in miniutes
        public double GetAverageLengthOfVideo()
        {
            var    lengths             = HistoryVideos.Select(v => VideoViewModelsDict[v.GetVideoID()].GetDuration());
            double averageTotalSeconds = lengths.Average(l => l.TotalSeconds);
            double averageMinutes      = Math.Round(averageTotalSeconds / 60);

            return(averageMinutes);
        }
        // Get average time watched per day in hours
        public double GetAverageDailyWatchTime()
        {
            var    dates              = HistoryVideos.Select(v => v.GetTime().Date).Distinct().ToList();
            var    dailyTimesMinutes  = dates.Select(d => HistoryVideos.Where(h => h.GetTime().Date.Equals(d.Date)).Sum(v => VideoViewModelsDict[v.GetVideoID()].GetDuration().TotalMinutes));
            double averageTimeMinutes = dailyTimesMinutes.Average();
            double averageTimeHours   = Math.Round(averageTimeMinutes / 60);

            return(averageTimeHours);
        }
        // Get month with the most time watched
        public List <TimeWatchedPerMonthViewModel> GetTimeWatchedPerMonth()
        {
            List <MonthYearViewModel> monthViewModels = HistoryVideos.Select(v => new MonthYearViewModel(v.GetTime().Month, v.GetTime().Year)).GroupBy(vm => vm.Month + " " + vm.Year).Select(x => x.FirstOrDefault()).ToList();
            List <double>             monthlyHours    = monthViewModels.Select(vm => HistoryVideos.Where(h => new MonthYearViewModel(h.GetTime().Month, h.GetTime().Year) == vm).Sum(v => VideoViewModelsDict[v.GetVideoID()].GetDuration().TotalHours)).ToList();

            List <TimeWatchedPerMonthViewModel> viewModels = new List <TimeWatchedPerMonthViewModel>();

            for (int i = 0; i < monthViewModels.Count; i++)
            {
                viewModels.Add(new TimeWatchedPerMonthViewModel(monthViewModels[i], Math.Round(monthlyHours[i])));
            }

            return(viewModels.OrderByDescending(vm => vm.HoursWatched).ToList());
        }
        // Calculate how many hours the user watched per day of their history
        public List <HoursPerDayViewModel> GetHoursPerDay()
        {
            var             dates       = HistoryVideos.Select(v => v.GetTime().Date).Distinct().ToList();
            List <TimeSpan> timeWatched = new List <TimeSpan>();

            foreach (DateTime date in dates)
            {
                var      videosOnDay = HistoryVideos.Where(h => h.GetTime().Date == date.Date).ToList();
                TimeSpan timeForDay  = new TimeSpan(0, 0, 0);
                foreach (HistoryVideo video in videosOnDay)
                {
                    VideoViewModel viewModel = VideoViewModelsDict[video.GetVideoID()];
                    timeForDay = timeForDay.Add(viewModel.GetDuration());
                }
                timeWatched.Add(timeForDay);
            }

            List <HoursPerDayViewModel> hoursPerDayViewModels = new List <HoursPerDayViewModel>();

            for (int i = 0; i < timeWatched.Count; i++)
            {
                hoursPerDayViewModels.Add(new HoursPerDayViewModel()
                {
                    Date         = dates[i],
                    HoursWatched = timeWatched[i]
                });
            }


            //using (StreamWriter sw = new StreamWriter(UriHelper.CALCULATIONS_OUTPUT_DIR + "/Hours Per Day - " + DateTime.Now.ToFileTime() + ".csv"))
            //{
            //    foreach (var viewModel in hoursPerDayViewModels)
            //    {
            //        sw.WriteLine(viewModel.Date.ToShortDateString() + ";" + viewModel.HoursWatched.Hours + " Hours, " + viewModel.HoursWatched.Minutes + " Minutes and " + viewModel.HoursWatched.Seconds + " Seconds");
            //    }
            //}

            return(hoursPerDayViewModels);
        }
 // Get total number of channels watched
 public int GetTotalUniqueChannelsWatched()
 {
     return(HistoryVideos.Select(v => VideoViewModelsDict[v.GetVideoID()].ChannelId).Distinct().Count());
 }
 // Get total number of unqiue videos watched
 public int GetTotalUniqueVideosWatched()
 {
     return(HistoryVideos.Select(hv => hv.GetVideoID()).Distinct().Count());
 }