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

namespace EditingWithServerValidation.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult _Read([DataSourceRequest] DataSourceRequest request)
        {
            return Json(GetProducts().ToDataSourceResult(request));
        }

        public ActionResult _Create([DataSourceRequest] DataSourceRequest request, Product product)
        {
            //Manually add an model error in order to simulate validation error
            ModelState.AddModelError("Name", "My server error");
            


            //TODO: implement create functionality

            return Json(ModelState.ToDataSourceResult());
        }

        public ActionResult _Update([DataSourceRequest] DataSourceRequest request, Product product)
        {
            //Manually add an model error in order to simulate validation error
            ModelState.AddModelError("Name", "My server error");


            //TODO: implement update functionality

            return Json(ModelState.ToDataSourceResult());
        }

        private IEnumerable<Product> GetProducts()
        {
            return Enumerable.Range(1, 10).Select(i => new Product
            {
                Id = i,
                Name = "Product" + i
            });
        }
    }
}
