using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using GreenTree.Strohrmann.ERP.Core.Domain.Business;
using GreenTree.Strohrmann.ERP.Core.Helper;
using GreenTree.Strohrmann.ERP.Domain.Model;
using GreenTree.Strohrmann.ERP.Services.Geolocator;
using GreenTree.Strohrmann.ERP.Web.Extension;
using GreenTree.Strohrmann.ERP.Web.Models.Business;
using GreenTree.Strohrmann.ERP.Web.Models.Rights.User;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

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

        // The global DbContext
        private readonly ERPDbContext _eRPDbContext;

        #endregion

        #region Ctor

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

        #endregion

        #region Actions

        // GET: MaterialController
        public ActionResult Index()
        {
            var materials = _eRPDbContext.Materials
                .ToList()
                .Select(c => new MaterialModel(c));

            return View(materials);
        }

        // GET: MaterialController/Details/5
        public ActionResult Details(int id)
        {
            var material = _eRPDbContext.Materials
                .FirstOrDefault(c => c.Id == id);

            var materialModel = new MaterialModel(material);

            return View(materialModel);
        }

        // GET: MaterialController/Create
        public ActionResult Create()
        {
            ViewData.AddSelectList("AvailableUnits", 
                _eRPDbContext.Units
                    .OrderBy(u => u.Name), 
                m => m.Id, m => String.Format("{0} ({1})", m.Name, m.ShortName));

            ViewData.AddSelectList("AvailableSuppliers", _eRPDbContext.Suppliers, m => m.Id, m => m.Name);

            return View();
        }

        // POST: MaterialController/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(MaterialModel model)
        {
            if (!ModelState.IsValid)
            {
                ViewData.AddSelectList("AvailableUnits",
                    _eRPDbContext.Units
                        .OrderBy(u => u.Name),
                    m => m.Id, m => String.Format("{0} ({1})", m.Name, m.ShortName));

                ViewData.AddSelectList("AvailableSuppliers", _eRPDbContext.Suppliers, m => m.Id, m => m.Name);

                return View(model);
            }

            var material = new Material
            {
                Name = model.Name,
                Description = model.Description,
                ItemNumber = model.ItemNumber,
                NetValue = Convert.ToDecimal(model.NetValue),
                Height = model.Height,
                Width = model.Width,
                Depth = model.Depth,
                DefaultUnit = _eRPDbContext.Units.Find(model.DefaultUnit.Id),
                PackageSize = model.PackageSize,
                Supplier = _eRPDbContext.Suppliers.Find(model.Supplier.Id),
                CreatedBy = User.Identity.Name,
                CreatedOn = DateTime.Now
            };

            _eRPDbContext.Materials.Add(material);
            _eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

        // GET: MaterialController/Edit/5
        public ActionResult Edit(int id)
        {
            ViewData.AddSelectList("AvailableUnits",
                _eRPDbContext.Units
                    .OrderBy(u => u.Name),
                m => m.Id, m => String.Format("{0} ({1})", m.Name, m.ShortName));

            ViewData.AddSelectList("AvailableSuppliers", _eRPDbContext.Suppliers, m => m.Id, m => m.Name);

            var material = _eRPDbContext.Materials
                .FirstOrDefault(c => c.Id == id);

            var materialModel = new MaterialModel(material);

            return View(materialModel);
        }

        // POST: MaterialController/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, MaterialModel model)
        {
            if (!ModelState.IsValid)
            {
                ViewData.AddSelectList("AvailableUnits",
                    _eRPDbContext.Units
                        .OrderBy(u => u.Name),
                    m => m.Id, m => String.Format("{0} ({1})", m.Name, m.ShortName));

                ViewData.AddSelectList("AvailableSuppliers", _eRPDbContext.Suppliers, m => m.Id, m => m.Name);

                return View(model);
            }

            var material = _eRPDbContext.Materials
                .FirstOrDefault(u => u.Id == id);

            material.Name = model.Name;
            material.Description = model.Description;
            material.ItemNumber = model.ItemNumber;
            material.NetValue = Convert.ToDecimal(model.NetValue);
            material.Height = model.Height;
            material.Width = model.Width;
            material.Depth = model.Depth;
            material.DefaultUnit = _eRPDbContext.Units.Find(model.DefaultUnit.Id);
            material.PackageSize = model.PackageSize;
            material.Supplier = _eRPDbContext.Suppliers.Find(model.Supplier.Id);
            material.ChangedBy = User.Identity.Name;
            material.ChangedOn = DateTime.Now;

            _eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

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

            _eRPDbContext.Materials.Remove(material);
            _eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

        #endregion
    }
}
