using System.Linq;
using System.Web.Mvc;
using TelerikMvcAppDropDownList.Models;

namespace TelerikMvcAppDropDownList.Controllers
{
    public class ComboBoxController : Controller
    {
        public JsonResult GetCascadeCategories()
        {
            var result = Enumerable.Range(0, 50).Select(i => new CategoryViewModel
            {
                CategoryID = i,
                CategoryName = "Category " + i
            });

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetCascadeProducts(int? categories, string productFilter)
        {
            var products = Enumerable.Range(0, 50).Select(i => new ProductViewModel
            {
                ProductID = i,
                CategoryID = i,
                ProductName = "Product " + i
            });

            if (categories != null)
            {
                products = products.Where(p => p.CategoryID == categories);
            }

            if (!string.IsNullOrEmpty(productFilter))
            {
                products = products.Where(p => p.ProductName.Contains(productFilter));
            }

            return Json(products, JsonRequestBehavior.AllowGet);
        }
    }
}