using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TelerikMvcApp63.Models;

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

            return View();
        }

        public List<ProductViewModel> data = new List<ProductViewModel>()
        {
            new ProductViewModel() { NumericID = 111222, ProductID = "1", ProductName = "Tea" },
            new ProductViewModel() { NumericID = 222333, ProductID = "2", ProductName = "Vodka" },
            new ProductViewModel() { NumericID = 344634, ProductID = "3", ProductName = "Milk" },
            new ProductViewModel() { NumericID = 434656, ProductID = "4", ProductName = "Apple" },
            new ProductViewModel() { NumericID = 564433, ProductID = "5", ProductName = "Orange" },
            new ProductViewModel() { NumericID = 666454, ProductID = "6", ProductName = "Bread" },
            new ProductViewModel() { NumericID = 784556, ProductID = "7", ProductName = "Blueberry" },
            new ProductViewModel() { NumericID = 901222, ProductID = "8", ProductName = "Carrot" },
            new ProductViewModel() { NumericID = 124567, ProductID = "9", ProductName = "Coca-cola" },
            new ProductViewModel() { NumericID = 242424, ProductID = "10", ProductName = "Juice" },
            new ProductViewModel() { NumericID = 334566, ProductID = "11", ProductName = "Whiskey" },
            new ProductViewModel() { NumericID = 444444, ProductID = "12", ProductName = "Burger" },
            new ProductViewModel() { NumericID = 678543, ProductID = "13", ProductName = "Ham" },
            new ProductViewModel() { NumericID = 745436, ProductID = "14", ProductName = "Pepper" }
        };
    
        public JsonResult GetProductsAjax(string text)
        {
            var products = data.Select(product => new ProductViewModel
            {
                NumericID = product.NumericID,
                ProductID = product.ProductID,
                ProductName = product.ProductName
            });

            if (!string.IsNullOrEmpty(text))
            {
               products = data.Where(p => p.NumericID.ToString().Contains(text));
            }
            
            return Json(products, JsonRequestBehavior.AllowGet);
        }
    }
}
