using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GreenTree.Maschinenbestellungen.Core.Domain.Rights;
using GreenTree.Maschinenbestellungen.Core.Helper;
using GreenTree.Maschinenbestellungen.Domain.Model;
using GreenTree.Maschinenbestellungen.Web.Models.Rights.User;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace GreenTree.Maschinenbestellungen.Web.Controllers
{
    public class RightsController : Controller
    {
        #region DI fields

        // The global DbContext
        private readonly OrderDbContext _eRPDbContext;

        // The global user helper
        private readonly IUserHelper _userHelper;

        #endregion

        #region Ctor

        /// <summary>
        /// Initializes a new instance of the RightsController class
        /// </summary>
        /// <param name="eRPDbContext">Global DbContext.</param>
        /// <param name="userHelper">Global user helper.</param>
        public RightsController(
            OrderDbContext eRPDbContext,
            IUserHelper userHelper)
        {
            _eRPDbContext = eRPDbContext;
            _userHelper = userHelper;
        }

        #endregion

        #region Actions

        // GET: Rights
        public ActionResult Index()
        {
            var users = _eRPDbContext.Users
                .ToList()
                .Select(u => new UserModel(u));

            return View("~/Views/Rights/User/Index.cshtml", users);
        }

        // GET: Rights/Details/5
        public ActionResult Details(int id)
        {
            var user = _eRPDbContext.Users
                .FirstOrDefault(u => u.Id == id);

            var userModel = new UserModel(user);

            return View("~/Views/Rights/User/Details.cshtml", userModel);
        }

        // GET: Rights/Create
        public ActionResult Create()
        {
            ViewData.Add("AvailablePolicies", Startup._availablePolicies);

            return View("~/Views/Rights/User/Create.cshtml");
        }

        // POST: Rights/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(UserModel userModel)
        {
            if (!ModelState.IsValid)
                return View("~/Views/Rights/User/Create.cshtml", userModel);

            var user = new User
            {
                Accountname = userModel.Accountname,
                Password = _userHelper.HashString(userModel.Password, true),
                Forename = userModel.Forename,
                Lastname = userModel.Lastname,
                MailAddress = userModel.MailAddress,
                Birthdate = userModel.Birthdate,
                Activated = true
            };

            if (userModel.Policies != null)
            {
                user.Policies = new List<UserPolicy>(
                    userModel.Policies
                        .Select(p => new UserPolicy
                        {
                            PolicyName = p,
                            User = user
                        }));
            }

            _eRPDbContext.Users.Add(user);
            _eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

        // GET: Rights/Edit/5
        public ActionResult Edit(int id)
        {
            ViewData.Add("AvailablePolicies", Startup._availablePolicies);

            var user = _eRPDbContext.Users
                .FirstOrDefault(u => u.Id == id);

            var userModel = new UserModel(user);

            return View("~/Views/Rights/User/Edit.cshtml", userModel);
        }

        // POST: Rights/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, UserModel userModel)
        {
            if (!ModelState.IsValid)
                return View("~/Views/Rights/User/Edit.cshtml", userModel);

            var user = _eRPDbContext.Users
                .FirstOrDefault(u => u.Id == id);

            user.Accountname = userModel.Accountname;
            user.Forename = userModel.Forename;
            user.Lastname = userModel.Lastname;
            user.MailAddress = userModel.MailAddress;
            user.Birthdate = userModel.Birthdate;
            user.Activated = userModel.Activated;

            user.Policies.Clear();

            _eRPDbContext.SaveChanges();

            if (userModel.Policies != null)
            {
                user.Policies = new List<UserPolicy>(
                    userModel.Policies
                        .Select(p => new UserPolicy
                        {
                            PolicyName = p,
                            User = user
                        }));
            }

            _eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

        // POST: Rights/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            var user = _eRPDbContext.Users
                .FirstOrDefault(u => u.Id == id);

            _eRPDbContext.Users.Remove(user);
            _eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

        #endregion
    }
}