using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;

using Kendo.Mvc.Grid.CRUD.Models;

using Filter = Kendo.Mvc.Grid.CRUD.Models.Filter;
using System.Web;
using System.IO;

namespace Kendo.Mvc.Grid.CRUD.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(ProductViewModel productViewModel)
        {
            using (var northwind = new Northwind())
            {
                var product = new Product
                                  {
                                      ProductName = productViewModel.ProductName,
                                      UnitPrice = productViewModel.UnitPrice,
                                      UnitsInStock = productViewModel.UnitsInStock,
                                      Discontinued = productViewModel.Discontinued
                                  };

                northwind.Products.AddObject(product);
                northwind.SaveChanges();

                productViewModel.ProductID = product.ProductID;

                return Json(productViewModel);
            }
        }

        [HttpPost]
        public ActionResult Read(int take, int skip, IEnumerable<Sort> sort, Filter filter)
        {
            using (var northwind = new Northwind())
            {
                var result = northwind.Products
                                      .OrderBy(p => p.ProductID) // EF requires ordered IQueryable in order to do paging
                                      .Select(p => new ProductViewModel // Use a view model to avoid serializing internal Entity Framework properties as JSON
                                                       {
                                                           ProductID = p.ProductID,
                                                           ProductName = p.ProductName,
                                                           UnitPrice = p.UnitPrice,
                                                           UnitsInStock = p.UnitsInStock,
                                                           Discontinued = p.Discontinued
                                                       })
                                      .ToDataSourceResult(take, skip, sort, filter);

                return Json(result);
            }
        }

        [HttpPost]
        public ActionResult Update(ProductViewModel productViewModel)
        {
            using (var northwind = new Northwind())
            {
                if (productViewModel.ProductID == null)
                    throw new ArgumentException("The product ID should not be null");

                var product = new Product
                                  {
                                      ProductID = productViewModel.ProductID.Value,
                                      ProductName = productViewModel.ProductName,
                                      UnitPrice = productViewModel.UnitPrice,
                                      UnitsInStock = productViewModel.UnitsInStock,
                                      Discontinued = productViewModel.Discontinued
                                  };

                northwind.Products.Attach(product);
                northwind.ObjectStateManager.ChangeObjectState(product, EntityState.Modified);
                northwind.SaveChanges();

                return Json(null);
            }
        }

        [HttpPost]
        public ActionResult Destroy(ProductViewModel productViewModel)
        {
            using (var northwind = new Northwind())
            {
                if (productViewModel.ProductID == null)
                    throw new ArgumentException("The product ID should not be null");

                var product = new Product
                                  {
                                      ProductID = productViewModel.ProductID.Value,
                                  };

                northwind.Products.Attach(product);
                northwind.Products.DeleteObject(product);
                northwind.SaveChanges();

                return Json(null);
            }
        }

        public ActionResult Save(HttpPostedFileBase file, int productID)
        {
            var fileName = Path.GetFileName(file.FileName);
            var directory = Server.MapPath("~/Content/ProductImages/" + productID);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            var physicalPath = Path.Combine(directory, fileName);

            file.SaveAs(physicalPath);

            return Json(new { fileName = fileName });
        }
    }
}
