using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GreenTree.Strohrmann.ERP.Core.Domain.Business;
using GreenTree.Strohrmann.ERP.Domain.Model;
using GreenTree.Strohrmann.ERP.Web.Extension;
using GreenTree.Strohrmann.ERP.Web.Models.Business;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace GreenTree.Strohrmann.ERP.Web.Controllers
{
    public class EmployeeController : Controller
    {
        #region DI fields

        // The global DbContext
        private readonly ERPDbContext _eRPDbContext;

        #endregion

        #region Ctor

        /// <summary>
        /// Initializes a new instance of the EmployeeController class
        /// </summary>
        /// <param name="eRPDbContext">Global DbContext.</param>
        public EmployeeController(ERPDbContext eRPDbContext)
        {
            _eRPDbContext = eRPDbContext;
        }

        #endregion

        #region Actions

        // GET: EmployeeController
        public ActionResult Index()
        {
            var employees = _eRPDbContext.Employees
                .ToList()
                .Select(e => new EmployeeModel(e));

            return View(employees);
        }

        // GET: EmployeeController/Details/5
        public ActionResult Details(int id)
        {
            var employee = _eRPDbContext.Employees
                .FirstOrDefault(e => e.Id == id);

            var employeeModel = new EmployeeModel(employee);

            return View(employeeModel);
        }

        // GET: EmployeeController/Create
        public ActionResult Create()
        {
            ViewData.AddSelectList("AvailableDegrees", _eRPDbContext.EmployeeDegrees, m => m.Id, m => m.Name);

            return View();
        }

        // POST: EmployeeController/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(EmployeeModel model)
        {
            if (!ModelState.IsValid)
            {
                ViewData.AddSelectList("AvailableDegrees", _eRPDbContext.EmployeeDegrees, m => m.Id, m => m.Name);

                return View(model);
            }

            var employee = new Employee
            {
                Firstname = model.Firstname,
                Lastname = model.Lastname,
                Birthdate = model.Birthdate,
                MailAddress = model.MailAddress,
                EmployeeDegree = _eRPDbContext.EmployeeDegrees.Find(model.EmployeeDegree.Id),
                CreatedBy = User.Identity.Name,
                CreatedOn = DateTime.Now
            };

            _eRPDbContext.Employees.Add(employee);
            _eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

        // GET: EmployeeController/Edit/5
        public ActionResult Edit(int id)
        {
            ViewData.AddSelectList("AvailableDegrees", _eRPDbContext.EmployeeDegrees, m => m.Id, m => m.Name);

            var employee = _eRPDbContext.Employees
                .FirstOrDefault(c => c.Id == id);

            var employeeModel = new EmployeeModel(employee);

            return View(employeeModel);
        }

        // POST: EmployeeController/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, EmployeeModel model)
        {
            if (!ModelState.IsValid)
            {
                ViewData.AddSelectList("AvailableDegrees", _eRPDbContext.EmployeeDegrees, m => m.Id, m => m.Name);

                return View(model);
            }

            var employee = _eRPDbContext.Employees
                .FirstOrDefault(u => u.Id == id);

            employee.Firstname = model.Firstname;
            employee.Lastname = model.Lastname;
            employee.Birthdate = model.Birthdate;
            employee.MailAddress = model.MailAddress;
            employee.EmployeeDegree = _eRPDbContext.EmployeeDegrees.Find(model.EmployeeDegree.Id);
            employee.ChangedBy = User.Identity.Name;
            employee.ChangedOn = DateTime.Now;

            _eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

        // POST: EmployeeController/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            var employee = _eRPDbContext.Employees
                .FirstOrDefault(c => c.Id == id);

            _eRPDbContext.Employees.Remove(employee);
            _eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

        #endregion

        #region Helper



        #endregion
    }
}