using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using UploadGridIntegration_MVC2.Models;

namespace UploadGridIntegration_MVC2.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private const string SessionKey = "employees";

        private IEnumerable<Employee> Model
        {
            get
            {
                var employees = (IEnumerable<Employee>)Session[SessionKey];
                if (employees == null)
                {
                    employees = new Employee[]
                    {
                        new Employee
                        {
                            ID = 1,
                            Name = "John Doe",
                            Picture = "johndoe.jpg"
                        }
                    };

                    Model = employees;
                }

                return employees;
            }

            set
            {
                Session[SessionKey] = value;
            }
        }

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

        [HttpPost]
        public ActionResult Save(int id, HttpPostedFileBase attachment)
        {
            var attachmentName = Path.GetFileName(attachment.FileName);
            var imagesPath = Server.MapPath("~/Content/UserFiles");
            attachment.SaveAs(Path.Combine(imagesPath, attachmentName));

            return Json(new { fileName = attachmentName }, "text/plain");
        }

        [GridAction]
        public ActionResult Select()
        {
            return View(new GridModel(Model));
        }

        [HttpPost]
        [GridAction]
        public ActionResult Update(Employee model)
        {
            Model = new[] { model };

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