//
        // GET: /Account/GoogleAuthorization?code={code}
        public ActionResult GoogleAuthorization(string code)
        {
            // Retrieve the authenticator and save it in session for future use
            var authenticator = GoogleAuthorizationHelper.GetAuthenticator(code);
            Session["authenticator"] = authenticator;

            // Save the refresh token locally
            using (var dbContext = new UsersContext())
            {
                var userName = User.Identity.Name;
                var userRegistry = dbContext.GoogleRefreshTokens.FirstOrDefault(c => c.UserName == userName);

                if (userRegistry == null)
                {
                    dbContext.GoogleRefreshTokens.Add(
                        new GoogleRefreshToken()
                        {
                            UserName = userName,
                            RefreshToken = authenticator.RefreshToken
                        });
                }
                else
                {
                    userRegistry.RefreshToken = authenticator.RefreshToken;
                }

                dbContext.SaveChanges();
            }

            return RedirectToAction("Index", "Home");
        }
            public SimpleMembershipInitializer()
            {
                try
                {
                    using (var context = new UsersContext())
                    {
                        context.Database.CreateIfNotExists();
                        context.Database.Initialize(true);
                    }

                    WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
        private GoogleAuthenticator GetAuthenticator()
        {
            var authenticator = (GoogleAuthenticator)Session["authenticator"];

            if (authenticator == null || !authenticator.IsValid)
            {
                // Get a new Authenticator using the Refresh Token
                var refreshToken = new UsersContext()
                                        .GoogleRefreshTokens
                                        .FirstOrDefault(c => c.UserName == User.Identity.Name)
                                        .RefreshToken;
                authenticator = GoogleAuthorizationHelper.RefreshAuthenticator(refreshToken);
                Session["authenticator"] = authenticator;
            }

            return authenticator;
        }
        public ActionResult ListEvents(DateTime startDate, DateTime endDate)
        {
            UserProfile userProfile = null;
            using(var context = new UsersContext())
            {
                userProfile = context.UserProfiles.FirstOrDefault(c => c.UserName == User.Identity.Name);
            }

            if (userProfile == null) return RedirectToAction("Register", "Account");

            var authenticator = GetAuthenticator();

            var service = new GoogleCalendarServiceProxy(authenticator);
            var model = service.GetEvents(userProfile.Email, startDate, endDate);

            ViewBag.StartDate = startDate.ToShortDateString();
            ViewBag.EndDate = endDate.ToShortDateString();
            return View("Index", model);
        }
        public ActionResult Create()
        {
            string calendarId = string.Empty;
            using (var context = new UsersContext())
            {
                calendarId = context.UserProfiles.FirstOrDefault(c => c.UserName == User.Identity.Name).Email;
            }

            var model = new CalendarEvent()
            {
                CalendarId = calendarId,
                Title = "Stand up meeting",
                Location = "Starbucks",
                StartDate = DateTime.Today,
                EndDate = DateTime.Today.AddMinutes(60),
                Description = "Let's start this day with a great cup of coffee"
            };
            var colorList = Enum.GetValues(typeof(GoogleEventColors)).Cast<GoogleEventColors>()
                                .Select(v => new SelectListItem { Text = v.ToString(), Value = ((int)v).ToString() });

            ViewBag.Colors = new SelectList(colorList, "Value", "Text");
            return View(model);
        }