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

namespace GetGridRecordPage.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Read([DataSourceRequest] DataSourceRequest request)
        {
            return Json(new NorthwindEntities().Products.ToDataSourceResult(request));
        }

        public ActionResult GetRecordPage([DataSourceRequest] DataSourceRequest request, int id)
        {
            var pageSize = request.PageSize;
            request.PageSize = 0;
            var products = new NorthwindEntities().Products.ToDataSourceResult(request).Data as List<Product>;
            var page = (products.IndexOf(products.First(p => p.ProductID == id)) / pageSize) + 1;
            return Json(page);
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
