using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Kendo.Mvc.Examples.Models;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using WebApplication1.Models;
using System;

namespace Kendo.Mvc.Examples.Controllers
{
    public partial class GridController : Controller
    {
        private ProductService productService = new ProductService(new SampleEntities());
        public ActionResult Editing()
        {
            return View();
        }
        public ActionResult EditInLineConfirmation()
        {
            return View();
        }
        public ActionResult ComboInHeader()
        {
            ViewBag.GridColumnNames = new string[] { "ProductName", "UnitPrice", "Discontinued", "UnitsInStock" };
            return View();
        }
        public ActionResult RefreshFilterDataSource()
        {
            return View();
        }

        public JsonResult DropDownSource()
        {
            return Json(productService.Read(), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetColumnFilterForecast(string text)
        {
            return Json(DateTime.Now.Millisecond.ToString()
                .Select(x => new { ProductName = x.ToString() }), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Editing_Read([DataSourceRequest] DataSourceRequest request)
        {
            return Json(productService.Read().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Editing_Create([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<ProductViewModel> products)
        {
            var results = new List<ProductViewModel>();

            if (products != null && ModelState.IsValid)
            {
                foreach (var product in products)
                {
                    productService.Create(product);
                    results.Add(product);
                }
            }

            return Json(results.ToDataSourceResult(request, ModelState));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Editing_Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<ProductViewModel> products)
        {
            if (products != null && ModelState.IsValid)
            {
                foreach (var product in products)
                {
                    productService.Update(product);
                }
            }

            return Json(products.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Editing_Destroy([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<ProductViewModel> products)
        {
            if (products.Any())
            {
                foreach (var product in products)
                {
                    productService.Destroy(product);
                }
            }

            return Json(products.ToDataSourceResult(request, ModelState));
        }
    }
}
