public async Task<ActionResult> Index(FacebookContext context)
        {
            if (ModelState.IsValid)
            {
                var user = await context.Client.GetCurrentUserAsync<MyAppUser>();
                var friendsWithUpcomingBirthdays = user.Friends.Data.OrderBy(friend =>
                {
                    try
                    {
                        string friendBirthDayString = friend.Birthday;
                        if (String.IsNullOrEmpty(friendBirthDayString))
                        {
                            return int.MaxValue;
                        }

                        var birthDate = DateTime.Parse(friendBirthDayString);
                        friend.Birthday = birthDate.ToString("MMMM d"); // normalize birthday formats
                        return BirthdayCalculator.GetDaysBeforeBirthday(birthDate);
                    }
                    catch
                    {
                        return int.MaxValue;
                    }
                }).Take(100);
                user.Friends.Data = friendsWithUpcomingBirthdays.ToList();
                return View(user);
            }

            return View("Error");
        }
 public async Task<ActionResult> Search(string friendName, FacebookContext context)
 {
     var userFriends = await context.Client.GetCurrentUserFriendsAsync<MyAppUserFriendSimple>();
     var friendsFound = String.IsNullOrEmpty(friendName) ?
         userFriends.ToList() :
         userFriends.Where(f => f.Name.ToLowerInvariant().Contains(friendName.ToLowerInvariant())).ToList();
     friendsFound.ForEach(f => f.Birthday = !String.IsNullOrEmpty(f.Birthday) ? DateTime.Parse(f.Birthday).ToString("MMMM d") : "");
     return View(friendsFound);
 }
        public async Task<ActionResult> Index(FacebookContext context)
        {
            if (ModelState.IsValid)
            {
                var user = await context.Client.GetCurrentUserAsync<MyAppUser>();
                return View(user);
            }

            return View("Error");
        }
        public async Task<ActionResult> RecommendGifts(string friendId, FacebookContext context)
        {
            if (!String.IsNullOrEmpty(friendId))
            {
                var friend = await context.Client.GetFacebookObjectAsync<MyAppUserFriend>(friendId);
                if (friend != null)
                {
                    var products = await RecommendationEngine.RecommendProductAsync(friend);
                    ViewBag.FriendName = friend.Name;
                    return View(products);
                }
            }

            return View("Error");
        }
        public async Task<ActionResult> Index(FacebookContext context)
        {
            if (ModelState.IsValid)
            {
                var user = await context.Client.GetCurrentUserAsync<MyAppUser>();
                var model = new HomeModel();

                var blUser = GetUser(user);

                model.Name = user.Name;
                model.Bucket = new BucketModel();
                model.Bucket.Id = blUser.Bucket.Id;
                model.Bucket.Name = string.Empty;
                model.Bucket.Items = blUser.Bucket.Items.Select(GetItemModel).ToList();
                
                return View(model);
            }
            return View("Error");
        }
        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 Network(FacebookContext context)
 {
     ViewBag.AccessToken = context.AccessToken;
     return View();
 }
        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);
            }

        }