using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using SmartCDC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SmartCDC.Controllers
{
    public class UserController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();
        private ApplicationUserManager _userManager;
        public ApplicationUserManager UserManager
        {
            get
            {
                return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                _userManager = value;
            }
        }
        public UserController()
        {

        }
        public UserController(ApplicationUserManager userManager)
        {
            UserManager = userManager;
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
        // GET: User
        public ActionResult Index()
        {
            var userList = db.Users.ToList();
            var roleList = db.Roles.ToDictionary(r => r.Id, r => r.Name);

            ViewBag.Roles = roleList;
            return View(userList);
        }
        public ActionResult Edit(string id)
        {
            if (id == null)
                return HttpNotFound();

            var user = UserManager.FindById(id);

            if (user == null)
                return HttpNotFound();

            return View(user);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ApplicationUser model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var user = UserManager.FindById(model.Id);

            if (user == null)
                return HttpNotFound();

            // Update fields
            user.UserName = model.UserName;
            user.FullName = model.FullName;
            user.Mobile = model.Mobile;
            user.Address = model.Address;
            user.Email = model.Email;
            //user.PhoneNumber = model.PhoneNumber;

            // save changes
            var result = UserManager.Update(user);

            if (result.Succeeded)
            {
                TempData["Success"] = "User updated successfully.";
                return RedirectToAction("Index");
            }

            // show identity errors
            foreach (var error in result.Errors)
            {
                ModelState.AddModelError("", error);
            }

            return View(model);
        }
        public ActionResult ChangePassword(string id)
        {
            if (id == null)
                return HttpNotFound();

            var user = UserManager.FindById(id);

            if (user == null)
                return HttpNotFound();

            ViewBag.UserId = id;
            ViewBag.UserName = user.UserName;

            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ChangePassword(string id, string newPassword)
        {
            if (string.IsNullOrEmpty(newPassword))
            {
                ModelState.AddModelError("", "Password cannot be empty");
                return View();
            }

            var user = UserManager.FindById(id);
            if (user == null)
                return HttpNotFound();

            // Remove old password hash
            var removePassword = UserManager.RemovePassword(id);
            if (!removePassword.Succeeded)
            {
                ModelState.AddModelError("", removePassword.Errors.First());
                return View();
            }

            // Add new password
            var addPassword = UserManager.AddPassword(id, newPassword);
            if (!addPassword.Succeeded)
            {
                ModelState.AddModelError("", addPassword.Errors.First());
                return View();
            }

            TempData["Success"] = "Password updated successfully.";
            return RedirectToAction("Index");
        }


    }
}