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

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

        public ActionResult Virtualization_Categories_Read([DataSourceRequest] DataSourceRequest request)
        {
            return Json(GetCategories().ToDataSourceResult(request));
        }

        public ActionResult Categories_ValueMapper(int[] values)
        {
            var indices = new List<int>();

            if (values != null && values.Any())
            {
                var index = 0;

                foreach (var category in GetCategories())
                {
                    if (values.Contains(category.CategoryID))
                    {
                        indices.Add(index);
                    }

                    index += 1;
                }
            }

            return Json(indices, JsonRequestBehavior.AllowGet);
        }

        public IEnumerable<CategoryViewModel> GetCategories()
        {
            var categories = Enumerable.Range(1, 5000).Select(i => new CategoryViewModel
            {
                CategoryID = i,
                CategoryName = "Category" + i,
            });

            return categories;
        }       
    }
}
