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 GreenTree.Strohrmann.ERP.Web.Models.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

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

        // The global DbContext
        private readonly ERPDbContext _eRPDbContext;

        #endregion

        #region Ctor

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

        #endregion

        #region Actions

        // GET: CustomerController
        public ActionResult Index()
        {
            var customers = _eRPDbContext.Customers
                .ToList()
                .Select(c => new CustomerModel(c));

            return View(customers);
        }

        // GET: CustomerController/Details/5
        public ActionResult Details(int id)
        {
            var customer = _eRPDbContext.Customers
                .FirstOrDefault(c => c.Id == id);

            var customerModel = new CustomerModel(customer);

            return View(customerModel);
        }

        // GET: CustomerController/Create
        public ActionResult Create()
        {
            ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);

            return View();
        }

        // POST: CustomerController/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CustomerModel model)
        {
            if (!ModelState.IsValid)
            {
                ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);

                return View(model);
            }

            var customer = new Customer
            {
                Title = _eRPDbContext.Titles.Find(model.Title.Id),
                Firstname = model.Firstname,
                Lastname = model.Lastname,
                CompanyName = model.CompanyName,
                Address = model.Address,
                Town = model.Town,
                ZipCode = model.ZipCode,
                Country = model.Country,
                IsBusiness = model.IsBusiness,
                CreatedBy = User.Identity.Name,
                CreatedOn = DateTime.Now
            };

            _eRPDbContext.Customers.Add(customer);
            _eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

        // GET: CustomerController/Edit/5
        public ActionResult Edit(int id)
        {
            ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);

            var customer = _eRPDbContext.Customers
                .FirstOrDefault(c => c.Id == id);

            var customerModel = new CustomerModel(customer);

            return View(customerModel);
        }

        // POST: CustomerController/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, CustomerModel model)
        {
            if (!ModelState.IsValid)
            {
                ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);

                return View(model);
            }

            var customer = _eRPDbContext.Customers
                .FirstOrDefault(u => u.Id == id);

            customer.Title = _eRPDbContext.Titles.Find(model.Title.Id);
            customer.Firstname = model.Firstname;
            customer.Lastname = model.Lastname;
            customer.CompanyName = model.CompanyName;
            customer.Address = model.Address;
            customer.Town = model.Town;
            customer.ZipCode = model.ZipCode;
            customer.Country = model.Country;
            customer.IsBusiness = model.IsBusiness;
            customer.ChangedBy = User.Identity.Name;
            customer.ChangedOn = DateTime.Now;

            _eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

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

            _eRPDbContext.Customers.Remove(customer);
            _eRPDbContext.SaveChanges();

            return RedirectToAction(nameof(Index));
        }

        #endregion

        #region Partials


        // POST: CustomerController/Search/*term*
        [HttpPost]
        public ActionResult Search(SearchModel search)
        {
            if (search == null)
                return new JsonResult(null);

            var words = search.SearchTerm.Split(' ');

            var result = _eRPDbContext.Customers
                .Where(c => words.Contains(c.Firstname) ||
                            words.Contains(c.Lastname) ||
                            words.Contains(c.CompanyName))
                .ToArray()
                .Select(c => new CustomerModel(c));

            return new JsonResult(result);
        }

        #endregion
    }
}
