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

namespace CascadingDDL.Controllers
{
    public class DropDownListController : Controller
    {
        // GET: DropDownList
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetCategories()
        {            
            var categories = AllCategories();
            return Json(categories, JsonRequestBehavior.AllowGet); 
        }

        public JsonResult GetCascadeProducts()
        {
            var products = AllProducts();
            return Json(products, JsonRequestBehavior.AllowGet);
        }

        private IEnumerable<ProductViewModel> AllProducts() {
            var categories = new List<ProductViewModel>() {
                new ProductViewModel(){ ProductId = 1, CategoryId = 1, ProductName = "Milk"},
                new ProductViewModel(){ ProductId = 2, CategoryId = 1, ProductName = "Juice"},
                new ProductViewModel(){ ProductId = 3, CategoryId = 1, ProductName = "Water"},
                new ProductViewModel(){ ProductId = 4, CategoryId = 2, ProductName = "Fruits"},
                new ProductViewModel(){ ProductId = 5, CategoryId = 2, ProductName = "Vegetables"},
                new ProductViewModel(){ ProductId = 6, CategoryId = 2, ProductName = "Meat"}
            };
            return categories;

        }

        private IEnumerable<CategoryViewModel> AllCategories()
        {
            var category = new CategoryViewModel()
            {
                CategoryId = 1,
                CategoryName = "Drinks"
            };

            var category2 = new CategoryViewModel()
            {
                CategoryId = 2,
                CategoryName = "Food"
            };

            var categories = new List<CategoryViewModel>();
            categories.Add(category);
            categories.Add(category2);

            return categories;

        }
    }
}