using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Linq.Dynamic;
using MvcApplication3.Models;

namespace MvcApplication3.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private IQueryable Employees
        {
            get
            {
                if (Session["Employees"] == null)
                {
                    Session["Employees"] = new DataClasses1DataContext().LargeEmployees.AsQueryable();
                }

                IQueryable employees = (IQueryable)Session["Employees"];

                if (Session["ColumnToSort"] != null)
                {
                    employees = employees.OrderBy((string)Session["ColumnToSort"]);
                }

                Session["PageSize"] = 10;

                return employees.Skip((int)Session["CurrentPageIndex"] * (int)Session["PageSize"]).Take((int)Session["PageSize"]);
            }
            set
            {
                Session["Employees"] = value;
            }
        }

        public ActionResult Index()
        {
            ViewData["Title"] = "Home Page";
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            Session["CurrentPageIndex"] = 0;

            return View("Index", Employees);
        }

        public ActionResult Sort(string columnName)
        {
            Session["ColumnToSort"] = columnName;

            Session["ItemToEdit"] = null;

            return View("Index", Employees);
        }

        public ActionResult Page(int index)
        {
            Session["CurrentPageIndex"] = index;

            Session["ItemToEdit"] = null;

            return View("Index", Employees);
        }

        public ActionResult Cancel()
        {
            Session["ItemToEdit"] = null;

            return View("Index", Employees);
        }

        public ActionResult CancelInsert()
        {
            Session["InitInsert"] = null;

            return View("Index", Employees);
        }

        public ActionResult Select(string itemIndex)
        {
            Session["ItemToSelect"] = itemIndex;

            return View("Index", Employees);
        }

        public ActionResult InitInsert()
        {
            Session["InitInsert"] = true;
            Session["ItemToEdit"] = null;

            return View("Index", Employees);
        }

        public ActionResult Edit(string itemIndex, int id)
        {
            Session["EmployeeID"] = id;
            Session["ItemToEdit"] = itemIndex;

            Session["InitInsert"] = null;

            return View("Index", Employees);
        }

        public ActionResult Update()
        {
            //simulate error  on update
            try
            {
                DataClasses1DataContext context = new DataClasses1DataContext();

                ViewData.ModelState.AddModelError("FirstName", "FirstName is wrong");
                  
                LargeEmployee employee = context.LargeEmployees.Where(n => n.EmployeeID == (int)Session["EmployeeID"]).FirstOrDefault();
                UpdateModel(employee, new string[] { "asdas" });// Request.Form.AllKeys.Where(e => e.Equals("EmployeeID") || e.Equals("FirstName") || e.Equals("LastName")).ToArray());           
                context.SubmitChanges();
                Session["Employees"] = Session["EmployeeID"] = Session["ItemToEdit"] = null;

            }
            catch (Exception e)
            {
                ViewData.ModelState.AddModelError("Exception",e);
            }            
            return View("Index", Employees);
            
        }

        public ActionResult Insert()
        {
            DataClasses1DataContext context = new DataClasses1DataContext();
            LargeEmployee employee = new LargeEmployee();
            UpdateModel(employee, Request.Form.AllKeys.Where(e => e.Equals("EmployeeID") || e.Equals("FirstName") || e.Equals("LastName")).ToArray());
            context.LargeEmployees.InsertOnSubmit(employee);
            context.SubmitChanges();
            Session["Employees"] = Session["InitInsert"] = null;

            return View("Index", Employees);
        }

        public ActionResult Delete(int? id)
        {
            DataClasses1DataContext context = new DataClasses1DataContext();
            LargeEmployee employee = context.LargeEmployees.Where(n => n.EmployeeID == id).FirstOrDefault();
            context.LargeEmployees.DeleteOnSubmit(employee);
            context.SubmitChanges();
            Session["Employees"] = Session["ItemToEdit"] = null;

            return View("Index", Employees);
        }

        public ActionResult About()
        {
            ViewData["Title"] = "About Page";

            return View();
        }
    }
}
