using System;
using System.Linq;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using log4net;
using Rainbow.Data;
using Rainbow.Models;
using Rainbow.ViewModels;

namespace Rainbow.Controllers
{
    public class CustomerController : Controller
    {
        public CustomerController(IUnitOfWork uow)
        {
            _uow = uow;
        }

        //
        // GET: /Customer/
        public ActionResult Index(string searchTerm)
        {
            return View();
        }

        public ActionResult CustomersGridData([DataSourceRequest] DataSourceRequest request)
        {
            var customers = GetCustomerViewModels();
            var result = customers.ToDataSourceResult(request);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        private IQueryable<CustomerViewModel> GetCustomerViewModels()
        {
            var customers = _uow.Customers.List().Select(c => new CustomerViewModel
            {
                Id = c.Id,
                Number = c.Number,
                CompanyName = c.CompanyName,
                Zip = c.Zip,
                City = c.City,
                Created = c.Created,
                LastModified = c.LastModified
            });
            return customers;
        }

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Customer_Update([DataSourceRequest] DataSourceRequest request, CustomerViewModel customer)		
		{
			if(customer != null && ModelState.IsValid)
			{

			}

			return Json(new[] { customer }.ToDataSourceResult(request, ModelState));
		}		

        //
        // GET: /Customer/Details/5
        public ActionResult Details(Int32 id)
        {
            Customer customer = _uow.Customers.GetById(id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

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

        //
        // POST: /Customer/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Customer customer)
        {
            if (ModelState.IsValid)
            {
                _uow.Customers.Add(customer);
                _uow.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(customer);
        }

        //
        // GET: /Customer/Edit/5
        public ActionResult Edit(Int32 id)
        {
            Customer customer = _uow.Customers.GetById(id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        //
        // POST: /Customer/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Customer customer)
        {
            if (ModelState.IsValid)
            {
                _uow.Customers.Update(customer);
                _uow.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(customer);
        }

        //
        // GET: /Customer/Delete/5
        public ActionResult Delete(Int32 id)
        {
            Customer customer = _uow.Customers.GetById(id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        //
        // POST: /Customer/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Int32 id)
        {
            _uow.Customers.Delete(id);
            _uow.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Sync()
        {
            try
            {
                int numInserted;
                int numUpdated;
                Data.Synchronization.Handler.SyncCustomers(out numInserted, out numUpdated);
                ViewBag.Message = String.Format("Erfolgreich: {0} hinzugefügt, {1} aktualisiert", numInserted, numUpdated);
            }
            catch (Exception exc)
            {
                Logger.Error(exc);
                ViewBag.Error = "Fehler: " + exc.Message;
            }
            return View();
        }

        protected override void Dispose(bool disposing)
        {
            _uow.Dispose();
            base.Dispose(disposing);
        }

        private readonly IUnitOfWork _uow;
        private static readonly ILog Logger = LogManager.GetLogger(typeof (CustomerController));
    }
}
