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

namespace KendoMVCWrappers.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to kick-start your ASP.NET MVC application.";

            return View();
        }

        public ActionResult Read([DataSourceRequest] DataSourceRequest request, string searchbox)
        {
            return Json(GetData(searchbox).ToDataSourceResult(request));
        }

        private IEnumerable<Product> GetData(string searchbox = "")
        {
            var number = 0;

            int.TryParse(searchbox, out number);
             
            var db = new NwEntities();
            var result = from product in db.Products
                         where product.ProductName.Contains(searchbox) ||
                                product.ProductID == number ||                                
                                product.UnitPrice == number ||
                                product.QuantityPerUnit.Contains(searchbox) ||
                                searchbox == ""
                         select product;

            return result;
        }

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

            return View();
        }

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

            return View();
        }
    }
}
