using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using GridCommandFilterDescriptors.Models;

namespace GridCommandFilterDescriptors.Controllers
{
    public class HomeController : Controller
    {
        public static List<UserModel> users = new List<UserModel>()
        {
            new UserModel{UserID=1,UserName="John"},
            new UserModel{UserID=2,UserName="Sara"},
            new UserModel{UserID=3,UserName="Jacky"}
        };

        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View(users);
        }

        public ActionResult About()
        {
            return View();
        }
        
        [HttpPost, GridAction(EnableCustomBinding = true)]
        public ActionResult Refresh(GridCommand command)
        {
            var data = users.AsQueryable();
            var filtered = data.Where(ExpressionBuilder.Expression<UserModel>(command.FilterDescriptors));
            var gridModel = new GridModel<UserModel>
            {
                Data = filtered,
                Total = filtered.Count()
            };
            return new JsonResult
            {
                Data = gridModel
            };
        }
    }
}
