public async Task <IActionResult> Index()
        {
            //var gmailAuthService = scope.ServiceProvider.GetRequiredService<IGoogleAuthService>();

            //var credentials = await gmailService.AcquireCredentialsAsync();

            //if (credentials == null)
            //{
            //    //return;
            //}
            //else
            //{
            //var accessToken = await gmailService.GetAccessToken(credentials);

            //var gmailApiService = scope.ServiceProvider.GetRequiredService<IGmailApiService>();

            //await apiService.SaveEmailsToDbAsync(accessToken);

            //this.logger.LogInformation("Scoped service id: " + service.Id);
            //}

            var credentials = await gmailService.CheckForTokensAsync();

            var accessToken = await gmailService.GetAccessToken(credentials);

            // this.emailService.SetInitialStatusAsync();
            // var emails = await this.apiService.SyncEmails(accessToken);
            //var latest = this.emailService.GetAllEmails(5);

            var homeViewModel = new HomeViewModel
            {
                LatestEmails = this.emailService
                               .GetAllEmails(5)
                               .Select(e => emailMapper.PartialMapFrom(e))
                               .ToList(),

                LastModified = this.emailService
                               .GetLastModifiedEmails(5)
                               .Select(e => emailMapper.PartialMapFrom(e))
                               .ToList(),

                ApprovedAndNew = this.emailService
                                 .GetApprovedAndNewEmails(5)
                                 .Select(e => emailMapper.PartialMapFrom(e))
                                 .ToList(),
            };

            return(View(homeViewModel));
        }
        public async Task <IActionResult> StartProgram()
        {
            var credentials = await gmailAuthService.CheckForTokensAsync();

            if (credentials == null)
            {
                return(RedirectToAction("GoogleLogin", "Gmail"));
            }
            else
            {
                var accessToken = await gmailAuthService.GetAccessToken(credentials);

                await gmailService.SyncEmails(accessToken);

                return(RedirectToAction("Index", "Home"));
            }
        }
        public async Task <Email> HandleSuccessStatusesAsync(int emailId, string userId)
        {
            var email = await this.context.Emails.FindAsync(emailId);

            var user = await this.context.AppUsers.FindAsync(userId);

            var status = await this.context.EmailStatuses.FindAsync(email.StatusID);

            var currentStatus = status.StatusName.ToLower();

            if (currentStatus == "not reviewed")
            {
                var credentials = await googleService.CheckForTokensAsync();

                var token = await this.googleService.GetAccessToken(credentials);

                email = await this.apiService.GetMessageBodyAsync(email.ID, token);

                email.PrevStatusName = email.Status.StatusName;
                var newStatus = this.context.EmailStatuses.FirstOrDefault(es => es.StatusName.ToLower() == "new");
                email.PrevStatusName = currentStatus;
                email.Status         = newStatus;
                email.StatusID       = newStatus.ID;
                email.LastModifiedOn = DateTime.Now;
                email.ProcessedBy    = user;
                email.ProcessedById  = user.Id;
            }
            else if (currentStatus == "open")
            {
                var newStatus = this.context.EmailStatuses.FirstOrDefault(es => es.StatusName.ToLower() == "approved");
                email.Status         = newStatus;
                email.StatusID       = newStatus.ID;
                email.LastModifiedOn = DateTime.Now;
                email.ProcessedBy    = user;
                email.ProcessedById  = user.Id;
            }
            await context.SaveChangesAsync();

            return(email);
        }