public async Task <ActionResult> Dashboard(int?OwnerId, int?HomeId, int?ServiceId)
        {
            if (!User.IsInRole("Admin"))
            {
                return(View("NotFound"));
            }
            List <FuneralHome>         Homes     = db.FuneralHomes.Where(f => f.DevHome == false).ToList();
            List <Analytic>            Analytics = new List <Analytic>();
            AnalyticViewModel          AVM       = new AnalyticViewModel();
            AnalyticDashboardViewModel ADVM      = new AnalyticDashboardViewModel();
            List <Service>             Services  = new List <Service>();

            ADVM.TotalVideoViews = db.Analytics.Count();
            DateTime OneMonth = DateTime.Now.AddDays(-30);

            ADVM.MonthViews = db.Analytics.Where(a => a.CreateDate > OneMonth).Count();
            if (OwnerId == null && HomeId == null && ServiceId == null)
            {
                //   Analytics = db.Analytics.Where(a=>a.Video.Service.FuneralHome.DevHome==false).ToList();
                Services = db.Services.Where(s => s.FuneralHome.DevHome == false).ToList();
            }
            else
            {
                if (OwnerId != null)
                {
                    //  Analytics = db.Analytics.Where(a=>a.Video.Service.FuneralHome.OwnerId==OwnerId).ToList();
                    Services = db.Services.Where(a => a.FuneralHome.OwnerId == OwnerId).ToList();
                }
                else
                {
                    if (HomeId != null)
                    {
                        // Analytics = db.Analytics.Where(a => a.Video.Service.FuneralHome.Id == HomeId).ToList();
                        Services = db.Services.Where(a => a.FuneralHome.Id == HomeId).ToList();
                    }
                    else
                    {
                        // Analytics = db.Analytics.Where(a => a.Video.ServiceId == ServiceId).ToList();
                    }
                }
            }

            //List<IpAddressObject> addresses = new List<IpAddressObject>();
            //foreach (var an in Analytics)
            //{
            //    if (an.Latitude != 0 && an.Longitude != 0)
            //    {
            //        //Database got mucked up with a bunch of hard coded values
            //        //So lets exclude those IP addresses from the heat map
            //        if (an.IPAddress != "174.17.56.90")
            //        {
            //            IpAddressObject add = new IpAddressObject();
            //            add.IpAdd = an.IPAddress;
            //            add.latitude = an.Latitude;
            //            add.longitude = an.Longitude;
            //            addresses.Add(add);
            //        }
            //    }
            //}

            DateTime        LastWeek          = DateTime.Now.AddDays(-7);
            List <Analytic> thisWeekAnalytics = Analytics.Where(a => a.CreateDate > LastWeek).ToList();
            string          mostViewedHome    = "";
            int             HighestVIews      = 0;
            int             popularhomeId     = 0;

            foreach (var home in Homes)
            {
                int homeViews = thisWeekAnalytics.Where(a => a.Video.Service.FuneralHome.Id == home.Id).Count();
                if (homeViews > HighestVIews)
                {
                    mostViewedHome = home.Name;
                    HighestVIews   = homeViews;
                    popularhomeId  = home.Id;
                }
            }
            //CRMFuneralHome popFH = db.CRMFuneralHome.Where(f => f.FuneralHomeId == popularhomeId).FirstOrDefault();
            //CRMContact PopularHomePicCont = null;
            //if(popFH !=null)
            //{
            //     PopularHomePicCont = popFH.PrimaryCRMContact;
            //}

            // if(PopularHomePicCont!=null)
            // {
            //     ADVM.PopularHomeOfTheWeekImage = PopularHomePicCont.PictureFileName;
            // }
            // ADVM.PopularHomeOfTheWeek = mostViewedHome;
            //ADVM.PopularHomeNumOfViews = HighestVIews;
            //ADVM.ServicesBarChartArray = Admin.GetServicesBarChart(Services, DateTime.Now.AddMonths(-3), DateTime.Now);
            //AVM.Details = addresses;
            //CalculateAnalytics.PageViews(ref ADVM, Analytics, Homes);
            // CalculateAnalytics.AverageTime(ref AVM, Analytics);
            //ADVM.NeedsToBeContacted = Admin.GetContactsThatNeedToBeContacted(db);
            //Admin.dashboardlinechart(ref ADVM, Analytics);
            //CalculateAnalytics.linechart(ref AVM, Analytics);
            ADVM.TotalFuneralsWithVideos   = Admin.GetTotalServicesWithVideo(Services);
            ADVM.MonthlyFuneralsWithVideos = Admin.GetServicesThisMonthWithVideo(Services);
            ADVM.LowUsageHomes             = CalculateAnalytics.LowUsageHomes(Homes);

            return(View(ADVM));
        }
        public ActionResult Dashboard()
        {
            if (!User.IsInRole("Admin"))
            {
                return(View("NotFound"));
            }
            //TODO: write logic to derive the owner id based on user that is logged in
            int ownerId = 2;
            List <FuneralHome>         Homes     = db.FuneralHomes.Where(f => f.DevHome == false && f.OwnerId == ownerId).ToList();
            List <Analytic>            Analytics = new List <Analytic>();
            AnalyticViewModel          AVM       = new AnalyticViewModel();
            AnalyticDashboardViewModel ADVM      = new AnalyticDashboardViewModel();
            List <Service>             Services  = new List <Service>();

            Analytics = db.Analytics.Where(a => a.Video.Service.FuneralHome.DevHome == false && a.Video.Service.FuneralHome.OwnerId == ownerId).ToList();
            Services  = db.Services.Where(s => s.FuneralHome.DevHome == false && s.FuneralHome.OwnerId == ownerId).ToList();

            List <IpAddressObject> addresses = new List <IpAddressObject>();

            foreach (var an in Analytics)
            {
                if (an.Latitude != 0 && an.Longitude != 0)
                {
                    //Database got mucked up with a bunch of hard coded values
                    //So lets exclude those IP addresses from the heat map
                    if (an.IPAddress != "174.17.56.90")
                    {
                        IpAddressObject add = new IpAddressObject();
                        add.IpAdd     = an.IPAddress;
                        add.latitude  = an.Latitude;
                        add.longitude = an.Longitude;
                        addresses.Add(add);
                    }
                }
            }

            DateTime        LastWeek          = DateTime.Now.AddDays(-7);
            List <Analytic> thisWeekAnalytics = Analytics.Where(a => a.CreateDate > LastWeek).ToList();
            string          mostViewedHome    = "";
            int             HighestVIews      = 0;
            int             popularhomeId     = 0;

            foreach (var home in Homes)
            {
                int homeViews = thisWeekAnalytics.Where(a => a.Video.Service.FuneralHome.Id == home.Id).Count();
                if (homeViews > HighestVIews)
                {
                    mostViewedHome = home.Name;
                    HighestVIews   = homeViews;
                    popularhomeId  = home.Id;
                }
            }
            CRMFuneralHome popFH = db.CRMFuneralHome.Where(f => f.FuneralHomeId == popularhomeId).FirstOrDefault();
            CRMContact     PopularHomePicCont = null;

            if (popFH != null)
            {
                PopularHomePicCont = popFH.PrimaryCRMContact;
            }

            if (PopularHomePicCont != null)
            {
                ADVM.PopularHomeOfTheWeekImage = PopularHomePicCont.PictureFileName;
            }
            ADVM.PopularHomeOfTheWeek  = mostViewedHome;
            ADVM.PopularHomeNumOfViews = HighestVIews;
            ADVM.ServicesBarChartArray = Admin.GetServicesBarChart(Services, DateTime.Now.AddMonths(-3), DateTime.Now);
            AVM.Details = addresses;
            CalculateAnalytics.PageViews(ref ADVM, Analytics, Homes);
            CalculateAnalytics.AverageTime(ref AVM, Analytics);
            Admin.dashboardlinechart(ref ADVM, Analytics);
            CalculateAnalytics.linechart(ref AVM, Analytics);
            ADVM.TotalFuneralsWithVideos   = Admin.GetTotalServicesWithVideo(Services);
            ADVM.MonthlyFuneralsWithVideos = Admin.GetServicesThisMonthWithVideo(Services);
            ADVM.LowUsageHomes             = CalculateAnalytics.LowUsageHomes(Homes);

            return(View(ADVM));
        }