public static List<RelatedOccupation> GetRelatedOccupations(int careerId)
 {
     using (var context = new FastForwardContext())
     {
         return GetRelatedOccupations(context, careerId);
     }
 }
        public ActionResult Survey()
        {
            using (var ctx = new FastForwardContext())
            {

                var survey = ctx.Surveys.FirstOrDefault();
                foreach (var question in survey.Questions)
                {
                    question.Answers = question.Answers.ToList();
                    question.Answers.Reverse();
                }

                return View("Survey", survey);

            }
        }
        public ActionResult Survey(Survey survey)
        {
            using (var ctx = new FastForwardContext())
            {
                var dbSurvey = ctx.Surveys.FirstOrDefault();
                var resultDict = new Dictionary<string, int>();
                for (var i = 1; i < 11; i++)
                {
                    resultDict.Add(i.ToString(), 0);
                }

                for (var i = 0; i < dbSurvey.Questions.Count(); i++)
                {
                    dbSurvey.Questions[i].Result = survey.Questions != null ? survey.Questions[i].Result : 0;
                    dbSurvey.Questions[i].Answers = dbSurvey.Questions[i].Answers.ToList();
                    var question = dbSurvey.Questions[i];
                    var indexA = question.GroupIdA;
                    var indexB = question.GroupIdB;
                    var answer = question.Answers.Last().AnswerId - survey.Questions[i].Result;
                    resultDict[indexA.ToString()] += answer;
                    resultDict[indexB.ToString()] += answer;
                }

                var sortedResults = (from entry in resultDict orderby entry.Value descending select entry)
                    .ToDictionary(pair => pair.Key, pair => pair.Value);

                var top1 = Convert.ToInt32(sortedResults.ElementAt(0).Key);
                var top2 = Convert.ToInt32(sortedResults.ElementAt(1).Key);
                var top3 = Convert.ToInt32(sortedResults.ElementAt(2).Key);
                var groupIds = new List<int> {top1, top2, top3};

                var jsonResults = JsonConvert.SerializeObject(groupIds);
                CookieHelper.SetCookie(AppConstants.ResultsCookieName, jsonResults, AppConstants.DefaultCookieLifetimeInMinutes);
                return RedirectToAction("Result", new { calculate = true });
            }
        }
        public async Task<ActionResult> Index(FacebookContext context)
        {
            using (var ctx = new FastForwardContext())
            {
                if (ModelState.IsValid)
                {
                    var user = await context.Client.GetCurrentUserAsync<MyAppUser>();
                    CookieHelper.SetCookie(AppConstants.UserIdCookieName, user.Id, Int32.MaxValue);
                    return View(user);
                    //var loc = user.Location.Name.Split(',');
                    //var state = loc[1].Substring(1);
                    //if (ctx.LocalColleges.Any(x => x.State == state))
                    //{
                    //    return View(user);
                    //}
                    //else
                    //{
                    //    return View("ErrorLoc");
                    //}
                }
            }

            return View("Error");
        }
        public ActionResult Learn()
        {
            var model = new LearnModel();
            List<Video> videos;

            using (var ctx = new FastForwardContext())
            {
                videos = ctx.Videos.OrderBy(v => v.DisplayOrder).ToList();
            }

            model.Videos = videos.Select(v => new VideoModel
                                                  {
                                                      VideoId = v.VideoId,
                                                      Name = v.Name,
                                                      Description = v.Description,
                                                      Video = VideoHelper.GetVideo(v.VideoId)
                                                  }).ToList();

            return View(model);
        }
        public async Task<ActionResult> Timeline(FacebookContext context, int careerId)
        {
            using (var ctx = new FastForwardContext())
            {
                var events = ctx.Events.Where(x => x.CareerId == careerId).OrderBy(x => x.Index).ToList();
                var timeline = new Timeline();
                timeline.Events = events;
                timeline.Career = ctx.Careers.FirstOrDefault(x => x.CareerId == careerId);
                var user = await context.Client.GetCurrentUserAsync<MyAppUser>();
                if (user.Name.Length > 22)
                {
                    user.Name = user.Name.Split(' ')[0];
                }

                var random = new Random(unchecked((int)(DateTime.Now.Ticks)));
                var len = user.Friends.Data.Count;
                var friends = user.Friends.Data.OrderBy(x => random.Next(len));
                if (len < events.Count)
                {
                    for (var i = 0; i < events.Count - len; i++)
                    {
                        user.Friends.Data.Add(user.Friends.Data[i % len]);
                    }
                }
                timeline.User = user;
                timeline.Friends = friends.ToList();

                var hasState = false;

                if (user.Location != null)
                {
                    var loc = user.Location.Name.Split(',');
                    var state = loc[1].Substring(1);
                    if (ctx.LocalColleges.Any(c => c.State == state))
                    {
                        var locals = ctx.LocalColleges.Where(x => x.State == state);
                        timeline.LocalColleges = locals.ToList();

                        foreach (var ev in timeline.Events.Where(ev => ev.TextContent.Contains("%localcollege%")))
                        {
                            ev.TextContent = ev.TextContent.Replace("%localcollege%", timeline.LocalColleges[0].College);
                        }

                        hasState = true;
                        ViewBag.HasLocation = true;
                    }
                }

                if(! hasState)
                {
                    timeline.Events = timeline.Events.Where(ev => !ev.TextContent.Contains("%localcollege%")).ToList();
                    ViewBag.HasLocation = false;
                }

                timeline.RelatedOccupations = CareerService.GetRelatedOccupations(ctx, careerId).ToList();

                var jsonCareerId = JsonConvert.SerializeObject(careerId);
                CookieHelper.SetCookie(AppConstants.CareerIdCookieName, jsonCareerId, AppConstants.DefaultCookieLifetimeInMinutes);

                return View("Timeline", timeline);
            }

        }
        public ActionResult Result(bool calculate = false)
        {
            var cookie = CookieHelper.GetCookie(AppConstants.ResultsCookieName);
            var model = new Result();
            model.Calculate = calculate;
            const int numberOfCareersToFetch = 9;
            using (var ctx = new FastForwardContext())
            {
                var allCareers = ctx.Careers.Where(c => c != null).ToList();

                if (cookie.Length > 0)
                {
                    model.HasTakenSurvey = true;
                    var cookieResults = cookie.TrimStart('[').TrimEnd(']');
                    var cookieList = cookieResults.Split(',');
                    var groupIds = cookieList.Select(int.Parse).ToList();
                    model.TopCareers = allCareers.Where(c => c.GroupId == groupIds[0] || c.GroupId == groupIds[1] || c.GroupId == groupIds[2]).ToList();
                }
                else
                {
                    model.TopCareers =
                        allCareers.Take(numberOfCareersToFetch).OrderBy(c => Guid.NewGuid()).ToList();
                    model.HasTakenSurvey = false;
                }

                model.RemainingCareers = allCareers.Except(model.TopCareers).ToList();
            }
            return View(model);
        }
 public static List<RelatedOccupation> GetRelatedOccupations(FastForwardContext context, int careerId)
 {
     return context.RelatedOccupations.Where(x => x.CareerId == careerId).ToList();
 }