public ActionResult Detail(int? id)
 {
     UserModel model = new UserModel();
     if (id.HasValue && id != 0)
     {
         User userEntity = userService.GetUser(id.Value);
        // model.ID = userEntity.ID;
         model.FirstName = userEntity.UserProfile.FirstName;
         model.LastName = userEntity.UserProfile.LastName;
         model.Email = userEntity.Email;
         model.AddedDate = userEntity.AddedDate;
     }
     return View(model);
 }
        public ActionResult CreateOrEditUser(int? id)
        {
            UserModel model = new UserModel();
            if(id.HasValue && id!=0)
            {
                Core.Data.User userEntity = userService.GetUser(id.Value);
                model.ID =(long)id;

                model.FirstName = userEntity.UserProfile.FirstName;
                model.LastName = userEntity.UserProfile.LastName;
                model.Email = userEntity.Email;
            }

            model.Roles = userService.GetRoles().Select(t => new RoleCheckbox
            {
                Id = (int)t.ID,
                Name = t.Name,
                IsChecked = false
            }).ToList();

            model.Themes = userService.GetCategories().Select(t => new CategoryCheckbox
                {
                    Id = (int)t.ID,
                    Name = t.Title,
                    IsChecked = false
                }).ToList();

            //var roles = userService.GetUsers().Select(t => t.Roles);

            //foreach (var item in roles)
            //{
            //    foreach (var item2 in item)
            //    {
            //        model.Roles.Add(new RoleCheckbox { Id = (int)item2.ID,
            //                                           Name= item2.Name,
            //                                            IsChecked=false});
            //    }
            //}

            //model.Roles.GroupBy(t => t.Id);

            return View(model);
        }
        public ActionResult Registration(string returnUrl)
        {
            UserModel model = new UserModel();

            return View(model);
        }
        public ActionResult Registration(UserModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                Core.Data.User userEntity = new Core.Data.User
                {

                    Email = model.Email,
                    AddedDate = DateTime.UtcNow,

                    UserProfile = new UserProfile
                    {
                        FirstName = model.FirstName,
                        LastName = model.LastName,
                        AddedDate = DateTime.UtcNow
                    },

                    Roles = userService.GetRoles().Where(t => t.Name == "user").ToList()
                };

                userEntity.SetPassword(model.PasswordHash);
                userService.InsertUser(userEntity);

                FormsAuthentication.SetAuthCookie(userEntity.Email, true);

                if ((userEntity.ID > 0) && (returnUrl != string.Empty))
                {
                        return Redirect(returnUrl);
                }

                return RedirectToRoute("home");
            }

            return View(model);
        }
        public ActionResult CreateOrEditUser(UserModel model)
        {
            var user= new Core.Data.User();
            SyncRoles(model.Roles, user.Roles);

            if (!ModelState.IsValid)
                return View(model);

            if (model.ID == 0)
            {
                if (userService.GetUsers().Any(t => t.Email == model.Email))
                    ModelState.AddModelError("Email", "Email დაკავებულია");

                if (!ModelState.IsValid)
                    return View(model);

                    user.Email = model.Email;
                    user.AddedDate = DateTime.UtcNow;
                    //Roles = model.Roles.ToList();
                    user.UserProfile = new UserProfile
                    {
                        FirstName = model.FirstName,
                        LastName = model.LastName,
                        AddedDate = DateTime.UtcNow,
                        FbLink = model.FbLink,
                        Avattar = model.Avattar,
                        NickName = model.Nickname

                    };

                user.SetPassword(model.PasswordHash);
                userService.InsertUser(user);
                if (user.ID > 0)
                {
                    return RedirectToAction("index");
                }
            }

            else
            {
                Core.Data.User userEntity = userService.GetUser(model.ID);
                userEntity.Email = model.Email;
                userEntity.SetPassword(model.PasswordHash);
                SyncRoles(model.Roles,userEntity.Roles);
                userEntity.FullName = model.FirstName + model.LastName;

                userEntity.UserProfile.FirstName = model.FirstName;
                userEntity.UserProfile.LastName = model.LastName;
                userEntity.UserName = model.Nickname;
                userEntity.Avattar = model.Avattar;
                userEntity.Address = model.Address;
                userEntity.FbLink = model.FbLink;
                userEntity.PhoneNumbers = model.PhoneNumbers;
                userEntity.UserProfile.userId = model.ID;
                userService.UpdateUser(userEntity);
                if (userEntity.ID > 0)
                {
                    return RedirectToAction("index");
                }

            }
            return View(model);
        }
        public ActionResult Delete(int id)
        {
            UserModel model = new UserModel();
            if (id != 0)
            {
                Core.Data.User userEntity = userService.GetUser(id);
                model.FirstName = userEntity.UserProfile.FirstName;
                model.LastName = userEntity.UserProfile.LastName;
                model.Email = userEntity.Email;
                model.AddedDate = userEntity.AddedDate;

            }
            return View(model);
        }