using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TelerikMvcApplication1.Models;
using Telerik.Web.Mvc;

namespace TelerikMvcApplication1.Controllers
{

    public class EmployeeController : Controller
    {
        //
        // GET: /Employee/t
		private DataClasses1DataContext dbContext = new DataClasses1DataContext();

        public ActionResult Index()
        {
            return View(GetEmployees());
        }

        //
        // GET: /Employee/Details/5



        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Employee/Edit/5

		public ActionResult Edit(int id)
		{
			return View();
		}

		[GridAction]
		public ActionResult _SelectPostCommentEditing()
		{

			return View(new GridModel(GetEmployees()));
		}

        //
        // POST: /Employee/Edit/5
		[HttpPost]
		[GridAction(GridName = "EmployeeGrid")]
        public ActionResult _Edit(int employeeID, EmployeeViewModel model)
        {
			model.timestamp = model.sTimestamp.StringToTimestampArray();
			model.startTime = model.startDateTime - new DateTime(DateTime.Today.Year,
				DateTime.Today.Month, DateTime.Today.Day, 0, 0, 0);
			model.endTime = model.endDateTime - new DateTime(DateTime.Today.Year,
				DateTime.Today.Month, DateTime.Today.Day, 0, 0, 0);

			var emp = dbContext.Employees.FirstOrDefault(e => e.EmployeeID == employeeID);
			emp.EndTime = model.endTime;
			emp.StartTime = model.startTime;
			emp.Timestamp = model.timestamp;
			emp.FirstName = model.FirstName;
			emp.LastName = model.LastName;

			TryUpdateModel(emp);
			dbContext.SubmitChanges();
			return View(new GridModel(GetEmployees()));

		}

        //
        // GET: /Employee/Delete/5
 
		[HttpPost]
		[GridAction(GridName = "EmployeeGrid")]
        public ActionResult Delete(int id)
        {
			return View(new GridModel(GetEmployees()));
        }


		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction(GridName = "EmployeeGrid")]
		public ActionResult _Insert()
		{
			//Create a new instance of the EditableProduct class.
			Employee emp	 = new Employee();

			//Perform model binding (fill the product properties and validate it).
			if(TryUpdateModel(emp))
			{

			}

			//Rebind the grid
			return View(new GridModel(GetEmployees()));
		}

		private IEnumerable<EmployeeViewModel> GetEmployees()
		{
			var employees 
				= dbContext.Employees
				.Select(e => new EmployeeViewModel
				{
					EmployeeID = e.EmployeeID,
					City = e.City,
					Country = e.Country,
					FirstName = e.FirstName,
					LastName = e.LastName,
					Title = e.Title,
					timestamp = e.Timestamp.ToArray(),
					startTime = e.StartTime ?? TimeSpan.Zero,
					endTime = e.EndTime ?? TimeSpan.Zero,
					startDateTime = DateTime.Today + e.StartTime,
					endDateTime = DateTime.Today + e.EndTime,
					HomePhone = e.HomePhone,
					sTimestamp = e.Timestamp.TimestampToString()
				})
				.OrderBy(e => e.EmployeeID);

			return employees;
		}
		[GridAction]
		public ActionResult _EmployeeSelect()
		{
			return View(new GridModel(GetEmployees()));
		}
    }
}
