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

namespace ThreeLevelHierarchy.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult HierarchyBinding_Employees([DataSourceRequest] DataSourceRequest request)
        {
            NorthwindEntities db = new NorthwindEntities();
            var employees = db.Employees.Select(employee => new EmployeeViewModel()
                                {
                                    EmployeeID = employee.EmployeeID,
                                    FirstName = employee.FirstName,
                                    LastName = employee.LastName,
                                    Country = employee.Country,
                                    City = employee.City,
                                    Notes = employee.Notes,
                                    Title = employee.Title,
                                    Address = employee.Address,
                                    HomePhone = employee.HomePhone
                                });

            return Json(employees.ToDataSourceResult(request));
        }

        public ActionResult HierarchyBinding_Orders(int employeeID, [DataSourceRequest] DataSourceRequest request)
        {
            NorthwindEntities db = new NorthwindEntities();
            var orders = db.Orders.Where(order => order.EmployeeID == employeeID)
                                  .Select(order => new OrderViewModel() 
                                  {
                                      ContactName = order.Customer.ContactName,
                                      Freight = order.Freight,
                                      OrderDate = order.OrderDate,
                                      ShippedDate = order.ShippedDate,
                                      OrderID = order.OrderID,
                                      ShipAddress = order.ShipAddress,
                                      ShipCountry = order.ShipCountry,
                                      ShipName = order.ShipName,
                                      ShipCity = order.ShipCity,
                                      EmployeeID = order.EmployeeID,
                                      CustomerID = order.CustomerID
                                  });

            return Json(orders.ToDataSourceResult(request));
        }

        public ActionResult HierarchyBinding_OrderDetails(int orderID, [DataSourceRequest] DataSourceRequest request)
        {
            NorthwindEntities db = new NorthwindEntities();
            var orderDetails = db.Order_Details.Where(o => o.OrderID == orderID)
                                               .Select(o => new OrderDetailsViewModel()
                                               {
                                                   OrderID = o.OrderID,
                                                   ProductName = o.Product.ProductName,
                                                   Quantity = o.Quantity,
                                                   UnitPrice = o.UnitPrice
                                               });
            return Json(orderDetails.ToDataSourceResult(request));
        }
    }
}
