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 CraftController : Controller
    {
        #region DI fields

        // The global DbContext
        private readonly ERPDbContext _eRPDbContext;

        #endregion

        #region Ctor

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

        #endregion

        #region Actions

        // GET: CraftController
        public ActionResult Index()
        {
            var crafts = _eRPDbContext.Crafts
                .ToList()
                .Select(c => new CraftModel(c));

            return View(crafts);
        }

        // GET: CraftController/Details/5
        public ActionResult Details(int id)
        {
            var craft = _eRPDbContext.Crafts
                .FirstOrDefault(c => c.Id == id);

            var craftModel = new CraftModel(craft);

            return View(craftModel);
        }

        // GET: CraftController/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: CraftController/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CraftModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            //var craft = new Craft
            //{
            //    CreatedBy = User.Identity.Name,
            //    CreatedOn = DateTime.Now
            //};

            //_eRPDbContext.Crafts.Add(craft);
            //_eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

        // GET: CraftController/AddCraftEmployeePartial
        public ActionResult AddCraftEmployeePartial(CraftEmployeeModel craftEmployeeModel)
        {
            return PartialView("~/Views/Craft/_CraftEmployeePartial.cshtml", craftEmployeeModel);
        }

        // GET: CraftController/Edit/5
        public ActionResult Edit(int id)
        {
            var craft = _eRPDbContext.Crafts
                .FirstOrDefault(c => c.Id == id);

            var craftModel = new CraftModel(craft);

            return View(craftModel);
        }

        // POST: CraftController/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, CraftModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var craft = _eRPDbContext.Crafts
                .FirstOrDefault(u => u.Id == id);

            craft.ChangedBy = User.Identity.Name;
            craft.ChangedOn = DateTime.Now;

            _eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

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

            _eRPDbContext.Crafts.Remove(craft);
            _eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

        #endregion
    }
}
