public ActionResult UsersToXml() { 
            string xml = "<?xml version = \"1.0\" encoding= \"utf-8\"?>";
            MyIdentityManager myIdentityManager = new MyIdentityManager();
            var users = myIdentityManager.GetAllUsers();
            xml = xml + "<Users>";
            foreach (var user in users) {
                xml = xml + "<Name>";
                xml = xml + user.Id;
                xml = xml + "</Name>";
            }
            xml = xml + "</Users>";

            myIdentityManager.Dispose();
            return Content(xml , "text/xml");
        }
        //called by API() 
        public ActionResult UsersToJson() { 
            MyIdentityManager myIdentityManager = new MyIdentityManager();
            var allUsers = myIdentityManager.GetAllUsers();
            List<string> usermanes = new List<string>();
            foreach (var applicationUser in allUsers) {
                usermanes.Add(applicationUser.UserName);
            }
 
            var serialized = JsonConvert.SerializeObject(usermanes);

            myIdentityManager.Dispose();
            return Content(serialized , typeof(Json).Name);
        }
        public async Task<ActionResult> AddNewUser(
            [Bind(
                Include =
                    "Email,Password,UserName,EnrollmentDate,PhoneNumber,PhoneNumberConfirmed" +
                    ",TwoFactorEnabled,Address,LockoutEnabled,LockoutEndDateUtc,AccessFailedCount,EmailConfirmed,ComboItemsStrTable"
                )] AddNewUserViewModel addNewUserViewModel) {
            MyIdentityManager myIdentityManager = new MyIdentityManager();

            if (!ModelState.IsValid) {//if the modelstate is not valid, pass the errors to a string, and display via httpnotfount. Not the best way, but works
                #region error reporting
                var modelStateerrors = ModelState.Where(x => x.Value.Errors.Count > 0)
                    .Select(x => new { x.Key , x.Value.Errors }).ToArray();
                string errorList = "";


                if (modelStateerrors != null) {
                    foreach (var modelerr in modelStateerrors) {
                        foreach (var modelerro in modelerr.Errors) {
                            errorList = errorList + " | " + modelerro.ErrorMessage;
                        }
                    }
                }
                //return PartialView("CustomError", errorList);
                #endregion
            }
            //our user, to ASPNET user. Relying to ASPNET's input error checks etc
            DateTime temp = DateTime.Now;
            var user = new ApplicationUser {
                Email = addNewUserViewModel.Email ,
                UserName = addNewUserViewModel.Email ,
                Address = addNewUserViewModel.Address ,
                EnrollmentDate = addNewUserViewModel.EnrollmentDate ?? temp ,  //if the user leaves that blank, fill it with datetime.now
                PhoneNumber = addNewUserViewModel.PhoneNumber ,
                PhoneNumberConfirmed = addNewUserViewModel.PhoneNumberConfirmed ,
                TwoFactorEnabled = addNewUserViewModel.TwoFactorEnabled ,
                LockoutEnabled = addNewUserViewModel.LockoutEnabled ,
                LockoutEndDateUtc = addNewUserViewModel.LockoutEndDateUtc ,
                AccessFailedCount = addNewUserViewModel.AccessFailedCount ?? 0 , //if the user leaves that blank, make it 0
                EmailConfirmed = addNewUserViewModel.EmailConfirmed
            };
            var createUserResult = myIdentityManager.CreateNewUser(user , addNewUserViewModel.Password);
            bool addComboItemToUserResult = false;
            if (createUserResult.Succeeded) {
                //foreach (var selectListItem in addNewUserViewModel.ComboItems)
                //{
                //    MyComboItemManager myComboItemManager = new MyComboItemManager();
                //    if (selectListItem.Selected)
                //    {
                //        addComboItemToUserResult = myComboItemManager.AddComboItemToUser(user.Id, selectListItem.Value);
                //    }
                //}

                //if (addComboItemToUserResult)
                // the above is the proper way. DropDownList returns a string[] instead a fking IEnumerable<SelectListItem>. FK that

                MyComboItemManager myComboItemManager = new MyComboItemManager();
                addComboItemToUserResult = myComboItemManager.UpdateComboItemsforUser(user.Id , addNewUserViewModel.ComboItemsStrTable);
                myComboItemManager.DisposeAll();
                myIdentityManager.Dispose();
                return Json(new { success = true });
                //else
                //    return HttpNotFound("Could not add Comboitems to User");
            }
            else {
                return HttpNotFound("User data not valid, please try again");
            }
        }
        public async Task<ActionResult> ManageComboItemsforUser(string id , string[] ComboItems , FormCollection collection) {
            MyIdentityManager myIdentityManager = new MyIdentityManager();
            MyComboItemManager myComboItemManager = new MyComboItemManager();
            if (ComboItems != null) {
                foreach (var comboItem in ComboItems) {
                    if (comboItem.IsNullOrWhiteSpace()) {
                        return HttpNotFound("AddNewRoleToUser: a role in the roles table is null or whitespace!");
                    }
                    //if role exists and we are not trying to add the user to an non existing role in the db
                }
            }// if ComboItems==null is checked by UpdateComboItemsforUser, and there is an action for that

            if (id.IsNullOrWhiteSpace()) return HttpNotFound("User Id Is null! 4956");

            //find user
            var user = myIdentityManager.SearchUserById(id);
            if (user == null) { return HttpNotFound("User with id " + id + "not found!"); }
            //if (ComboItems == null) { return HttpNotFound("ManageRolesForUser: ComboItems table zero"); }

            //Logic to update with new selection of ComboItems
            bool result = myComboItemManager.UpdateComboItemsforUser(id , ComboItems);

            myComboItemManager.DisposeAll();
            myIdentityManager.Dispose();
            if (result == true) {
                return Json(new { success = true });
            }
            return HttpNotFound("AddNewRoleToUser: Error adding role: " + " to user with id " + id);
        }
        public async Task<ActionResult> ManageRolesForUser(string id , string[] role , FormCollection collection) {
            MyIdentityManager myIdentityManager = new MyIdentityManager();

            var user = myIdentityManager.SearchUserById(id);
            #region safety checks
            if (user == null) { return HttpNotFound("User with id " + id + "not found!"); }
            if (role == null) //list emptied out
            { // all roles must be removed
                myIdentityManager.RemoveUserFromRoles(id);
                return Json(new { success = true });
            }
            //delete all roles form user, and add the new roles in the table 
            if (!myIdentityManager.RemoveUserFromRoles(id).Succeeded) { return HttpNotFound("removeRolesError"); }
            var result = false;
            foreach (var r in role) {
                if (r.IsNullOrWhiteSpace()) { return HttpNotFound("AddNewRoleToUser: a role in the roles table is null or whitespace!"); }
                //if role exists and we are not trying to add the user to an non existing role in the db
                if (!myIdentityManager.RoleExist(r)) { return HttpNotFound("AddNewRoleToUser: system roles not representing roles table"); }
                //if user is already in this role
                result = myIdentityManager.AddUserToRole(id , r);
            }
            #endregion
            //add user to every role
            //var result = myIdentityManager.AddUserToRoles(id, role);
            myIdentityManager.Dispose();
            if (result == true) {
                return Json(new { success = true });
            }
            return HttpNotFound("AddNewRoleToUser: Error adding role: " + role + " to user with id " + id);

        }
        //Get: AdminMainPage/ManageRolesForUser
        //it will manage the roles. 
        //presenting the previous roles, user selects new roles, ALL previous roles dropped, and all user selected roles added
        public ActionResult ManageRolesForUser(string id) {
            if (id.IsNullOrWhiteSpace()) { return HttpNotFound("id null or whitespace"); }
            MyIdentityManager myIdentityManager = new MyIdentityManager();

            List<SelectListItem> passingRolesList = new List<SelectListItem>();
            var allRoles = myIdentityManager.GetAllRoles();
            if (allRoles.Count == 0) { return HttpNotFound("No roles available in the system. Create a New Role First!"); }
            foreach (var role in allRoles) {
                SelectListItem listItem = new SelectListItem() { Text = role.Name , Value = role.Name };
                passingRolesList.Add(listItem);
            }
            var dictionary = new Dictionary<string , object>();
            dictionary.Add("selectlist" , passingRolesList);
            dictionary.Add("username" , myIdentityManager.GetUserByIdentityUserId(id).UserName);

            IEnumerable<SelectListItem> rolesienum = myIdentityManager.AllRolesToIenumSelectListItemsForuser(id);
            dictionary.Add("ienum" , rolesienum);
            myIdentityManager.Dispose();
            return PartialView(dictionary);
        }
        public async Task<ActionResult> UserEdit(string id) {
            if (id.IsNullOrWhiteSpace()) {
                return HttpNotFound("id IsNullOrWhiteSpace!");
            }
            MyIdentityManager myIdentityManager = new MyIdentityManager();
            ApplicationUser user;
            try {
                user = myIdentityManager.GetUserByIdentityUserId(id);
            }
            catch (Exception e) {
                return HttpNotFound("user not found!!" + e);
            }

            MyComboItemManager myComboItemManager = new MyComboItemManager();
            Dictionary<object , object> dictionary = new Dictionary<object , object>();

            var I2 = myComboItemManager.GetAllRolesForUserIdToIenumSelectListItem(id);

            dictionary.Add("ienum" , I2);
            dictionary.Add("applicationUser" , user);

            myIdentityManager.Dispose();
            myComboItemManager.DisposeAll();
            return PartialView("UserEdit" , dictionary);
        }
        public ActionResult DeleteUser(string id , FormCollection collection) {
            if (id.IsNullOrWhiteSpace())
                return HttpNotFound("id IsNullOrWhiteSpace!");

            MyIdentityManager myIdentityManager = new MyIdentityManager();
            var user = myIdentityManager.GetUserByIdentityUserId(id);
            var result = myIdentityManager.DeleteMemberShipUser(user);
            myIdentityManager.Dispose();
            if (result.Succeeded)
                return Json(new { success = true });
            else
                return HttpNotFound("Error deleting user");
        }
        public ActionResult DeleteUser(string id) {
            if (id.IsNullOrWhiteSpace())
                return HttpNotFound("id IsNullOrWhiteSpace!");

            MyIdentityManager myIdentityManager = new MyIdentityManager();
            var user = myIdentityManager.GetUserByIdentityUserId(id);
            myIdentityManager.Dispose();
            return PartialView(user);
        }