public void Execute(IJobExecutionContext context)
        {
            //throw new NotImplementedException();

            //using (var message = new MailMessage("*****@*****.**", "*****@*****.**"))
            //{
            //    message.Subject = "Test";
            //    message.Body = "Test at " + DateTime.Now;
            //    using (SmtpClient client = new SmtpClient
            //    {
            //        EnableSsl = true,
            //        Host = "smtp.gmail.com",
            //        Port = 587,
            //        Credentials = new NetworkCredential("*****@*****.**", "password")
            //    })
            //    {
            //        client.Send(message);
            //    }
            //}

            var emailNotification = new EmailNotification();

            emailNotification.SendEmail("*****@*****.**", "Test of Scheduled Task","Test message sent at " + DateTime.Now );
        }
        // id = user id
        public JsonResult ActivateMerchantAccount(string id)
        {
            //Check if supplier account exists, if not, create an account
            //
            var profileId = UserManager.FindById(id).UserProfile.Id;
            var email = UserManager.FindById(id).UserName; //find user login name (in email) by userid

            var supplier = _merchantServie.FindSupplierById(profileId);

            if (supplier != null)
            {
                supplier.IsActive = true;
                supplier.UserProfileId = profileId;
                supplier.CompanyIconImgUrl = email; //user this field to pass the username(email) to supplier information table

                _merchantServie.UpdateSupplier(supplier.Id);
            }
            else
            {
                var newSupplier = new Supplier();

                if (ModelState.IsValid)
                {
                    newSupplier.ContactFirstName = UserManager.FindById(id).UserProfile.FirstName;
                    newSupplier.ContactLastName = UserManager.FindById(id).UserProfile.LastName;
                    newSupplier.IsActive = true;
                    newSupplier.CompanyIconImgUrl = UserManager.FindById(id).UserProfile.Email;
                    newSupplier.UserProfileId = profileId;

                    _merchantServie.AddSupplier(newSupplier);
                }

            }

            //Send notification
            //
            var emailNotification = new EmailNotification();

            string message = "Your merchante account has been activated and ready for user. If you need help, please contact us! Thanks for choosing CommunityMarket for your ecommerce needs!";

            emailNotification.SendEmail(email, "New User Registration", message);

            //Bill merchant for setup fee: $50.00
            //

            return Json(id + " Activated!");
        }
        public void BillMerchant(MerchantFeePayment payment, string id)
        {
            var userInfo = UserManager.FindById(id);
            var profileId = userInfo.UserProfile.Id;

            var supplier = _merchantServie.FindSupplierBy(profileId);
            var supplierId =supplier.Id;
            var supplierEmail = supplier.CompanyIconImgUrl;

            var newPayment = new MerchantFeePayment();

            if (ModelState.IsValid)
            {
                try
                {
                    newPayment.BillingDate = DateTime.Now;
                    newPayment.IsPaid = false;
                    newPayment.MerchantFeeTypeId = 2;
                    newPayment.FeeAmount = payment.FeeAmount;
                    newPayment.SupplierId = supplierId;
                    newPayment.BillingYear = payment.BillingYear;
                    newPayment.BillingMonth = payment.BillingMonth;
                    newPayment.Notes = payment.Notes;

                    _merchantServie.AddBillPayment(newPayment);

                    //Send invoice and email notification
                    //

                    var month = "";

                    switch ( newPayment.BillingMonth)
                    {
                        case "January":
                            month = "1";
                            break;
                        case "February":
                            month = "2";
                            break;
                        case "March":
                            month = "3";
                            break;
                        case "April":
                            month = "4";
                            break;
                        case "May":
                            month = "5";
                            break;
                        case "June":
                            month = "6";
                            break;
                        case "July":
                            month = "7";
                            break;
                        case "August":
                            month = "8";
                            break;
                        case "September":
                            month = "9";
                            break;
                        case "October":
                            month = "10";
                            break;
                        case "November":
                            month = "11";
                            break;
                        case "December":
                            month = "12";
                            break;
                    }

                    var allTransactions = GetTransactionsByVendor(newPayment.SupplierId, month);

                    var emailNotification = new EmailNotification();

                    string message = this.RenderView("~/Views/Admin/_TransactionSummary.cshtml", allTransactions);

                    emailNotification.SendEmail(supplierEmail, "Merchant Management Fee Bill: " + newPayment.BillingMonth + " " + newPayment.BillingYear, message);
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
        private void SendNotificaiton(string emailAddress, string subject, string message)
        {
            var emailNotification = new EmailNotification();

            emailNotification.SendEmail(emailAddress, subject, message);
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            //var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
            //var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };

                // Add the additonal profile properties:

                //user.Address = model.Address;
                //user.City = model.City;
                //user.State = model.State;
                //user.PostalCode = model.PostalCode;

                //user.UserProfile.FirstName = model.FirstName;
                //user.UserProfile.LastName = model.LastName;

                //Add additional user profile information
                //
                //var user = userManager.FindByName(name);
                //if (user == null)
                //{

                

                if (model.Supplier)
                {
                    user = new ApplicationUser
                    {
                        UserName = model.Email,
                        Email = model.Email,


                        UserProfile = new AppUserProfile
                        {
                            FirstName = model.FirstName,
                            LastName = model.LastName,
                            Address1 = "",
                            Address2 = "2", //this attribute is used to tell if the user is registered as a supplier, then Admin will assing the right role manaully
                            City = "",
                            ProvState = "",
                            Country = "",
                            PostZipCode = "",
                            Email = model.Email,
                            AvatarImgUrl = "/Content/Assets/Images/user_default.png",
                            CreateDate = DateTime.Now,
                            UpdateDate = DateTime.Now
                        }
                    };
                }
                else
                {
                    user = new ApplicationUser
                    {
                        UserName = model.Email,
                        Email = model.Email,
                        

                        UserProfile = new AppUserProfile
                        {
                            FirstName = model.FirstName,
                            LastName = model.LastName,
                            Address1 = "",
                            Address2 = "1",
                            City = "",
                            ProvState = "",
                            Country = "",
                            PostZipCode = "",
                            Email = model.Email,
                            AvatarImgUrl = "/Content/Assets/Images/user_default.png",
                            CreateDate = DateTime.Now,
                            UpdateDate = DateTime.Now
                        }
                    };
                }
                    
                //}

                var result = await UserManager.CreateAsync(user, model.Password); //user account is created here.
                if (result.Succeeded)
                {
                    //Assign default role
                    //
                    var currentUser = UserManager.FindByName(user.UserName);
                    var userRole = UserManager.AddToRole(user.Id, "Customer");

                    //if (model.Supplier) //This can be activated if the merchant role can be assigned automatically
                    //{
                    //    userRole = UserManager.AddToRole(user.Id, "Merchant");
                    //}

                    //  Comment the following line to prevent log in until the user is confirmed.
                    //
                    //  await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); //If this one is restore, then email confirmation is NOT required.
                                                                                                          // that means the user will be logged in automatically after registering


                    //Create and send email confirmation to the email provided -- note: here is only the demo that the link will appear on the redirected page
                    //
                    var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    //var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));

                    await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");

                    //Send email notificaton to admin
                    //
                    var emailNotification = new EmailNotification();

                    string message;

                    if (model.Supplier)
                    {
                        message = "New user: "******" just registered as supplier. Setup may be required.";
                    }
                    else
                    {
                        message = "New user: "******" just registered.";
                    }
                    

                    emailNotification.SendEmail("*****@*****.**", "New User Registration", message);

                    //Create an entry in customer entity
                    //
                    
                    //CreateCustomer();
                    

                    return View("AccountConfirmation");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }