/// <summary>
 /// Inserts a new Clan User record
 /// </summary>
 public static void Insert(ClanUser clanUser)
 {
     using (var dbContext = new HouseOfClansEntities())
     {
         clanUser.addedOn = DateTime.Now;
         dbContext.ClanUsers.Add(clanUser);
         dbContext.SaveChanges();
     }
 }
        /// <summary>
        /// Gets the clan user information based on the aspnet user id
        /// </summary>
        /// <param name="aspnetUserId">AspNetUser Id</param>
        public static ClanUser SelectByAspNetUserId(string aspnetUserId)
        {
            ClanUser clanUser = new ClanUser();
            using (var dbContext = new HouseOfClansEntities())
            {
                clanUser = dbContext.ClanUsers.Where(p => p.aspnetUserId == aspnetUserId).Select(user => user).FirstOrDefault();
            }

            return clanUser;
        }
        public ActionResult Create(CreateMemberViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    ClanUser newMember = new ClanUser();

                    newMember.name = model.Name;
                    newMember.userRoleId = (short)ClanRole.Member;
                    newMember.clanId = ClanManager.GetClanId(User.Identity.GetUserId());

                    ClanUserManager.Insert(newMember);
                }
            }
            catch
            {
                return View(new CreateMemberViewModel());
            }

            return RedirectToAction("EditMode");
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };
                IdentityResult result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
					//TODO //vv I think here is where we add the system Role, and all new should be 'normal' user, we need to find a place to change this role in the near future
					//ref: http://stackoverflow.com/questions/19689183/add-user-to-role-asp-net-identity
					var currentUser = UserManager.FindByName(user.UserName);

					CheckAspRoles();

					var userRoleResult = UserManager.AddToRole(currentUser.Id, "Normal");
					
                    await SignInAsync(user, isPersistent: false);

                    ClanUser newClanUser = new ClanUser();

                    newClanUser.name = model.VillageName;
                    newClanUser.userRoleId = (short)ClanRole.Member;
                    newClanUser.aspnetUserId = currentUser.Id;

                    ClanUserManager.Insert(newClanUser);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        /// <summary>
        /// Updates the current Clan User
        /// </summary>
        public static void Update(ClanUser updatedClanUser)
        {
            ClanUser clanUser = ClanUserManager.SelectByClanUserId(updatedClanUser.id);
            using (var dbContext = new HouseOfClansEntities())
            {

                updatedClanUser.updatedOn = DateTime.Now;
                dbContext.ClanUsers.Attach(clanUser);
                dbContext.Entry(clanUser).CurrentValues.SetValues(updatedClanUser);
                dbContext.SaveChanges();
            }
        }
        /// <summary>
        /// Gets the Clan Leader based on the clan id
        /// </summary>
        /// <param name="clanId">Clan Id</param>
        public static ClanUser SelectClanLeaderByClanId(int clanId)
        {
            ClanUser clanLeader = new ClanUser();
            using (var dbContext = new HouseOfClansEntities())
            {
                clanLeader = dbContext.ClanUsers.Where(p => p.clanId == clanId && (ClanRole)p.userRoleId == ClanRole.Leader).Select(user => user).FirstOrDefault();
            }

            return clanLeader;
        }