#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MvcApplication3.Entities;
using MvcApplication3.Models;
using MvcApplication3.Linq;
using MvcApplication3.ViewModels;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data;
using System.Linq.Expressions;

#endregion

namespace MvcApplication3.Controllers
{
    public class DepartmentsController : Controller
    {
        [HttpPost]
        public ActionResult GetAll(int skip, int take, int page, int pageSize, 
            List<GridSort> sort = null, GridFilter filter = null)
        {
            try
            {
                var myDatabaseContext = new MyDatabaseContext();

                var departments = myDatabaseContext.Departments.Include(o => o.Division).AsQueryable();
                var totalCount = myDatabaseContext.Departments.Include(o => o.Division).AsQueryable();

                if (filter != null && (filter.Filters != null && filter.Filters.Count > 0))
                {
                    GridHelper.ProcessFilters<Department>(filter, ref departments);
                    totalCount = departments;
                }

                if (sort != null && sort.Count > 0)
                    foreach (var s in sort)
                    {
                        if (s.Field == "DivisionName") s.Field = "Division.Name";
                        departments = departments.OrderBy(s.Field + " " + s.Dir);
                    }

                departments = departments.Skip(skip).Take(take);

                List<Department> departmentList = departments.ToList();

                return Json(
                    new KendoGridModelWrapper<Department>
                        {
                            Items = departmentList,
                            TotalItemCount = totalCount.Count()
                        });
            }
            catch (Exception e)
            {
                throw;
            }
        }       

        [HttpPost]
        public ActionResult Update(IEnumerable<Department> models)
        {            
            using (var db = new MyDatabaseContext())
            {
                //Iterate all updated products which are posted by the Kendo Grid
                foreach (var dp in models)
                {
                    var department = db.Departments.SingleOrDefault(d => d.Id == dp.Id);
                    // Create a new Product entity and set its properties from productViewModel
                    department.Name = dp.Name;
                    department.Code = dp.Code;
                    department.Active = dp.Active;
                    department.DivisionId = dp.DivisionId;
                }

                // Save all updated products to the database
                db.SaveChanges();

                //Return emtpy result
                return Json(null);
            }
        }

        [HttpPost]
        public ActionResult Create(IEnumerable<Department> models)
        {
            using (var db = new MyDatabaseContext())
            {
                //Iterate all updated products which are posted by the Kendo Grid
                foreach (var dp in models)
                {
                    if (dp.Id <= 0)
                    {
                        var department = new Department
                        {
                            // Create a new Product entity and set its properties from productViewModel
                            Name = dp.Name,
                            Code = dp.Code,
                            Active = dp.Active,
                            DivisionId = dp.DivisionId
                        };
                        db.Departments.Add(department);
                    }
                }

                // Save all updated products to the database
                db.SaveChanges();

                //Return emtpy result
                return Json(null);
            }
        } 
    }
}