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

namespace KendoUIMVC5.Controllers
{
    public class HomeController : Controller
    {
        public static ICollection<Student> students = new List<Student>();

        public HomeController()
        {
            var names = new List<string> { "John", "Jane", "Jeremy", "James" };
            var lastNames = new List<string> { "Hammont", "Clarkson", "May", "Hennethon" };
            var random = new Random();
            if (students.Count == 0)
            {
                for (int i = 1; i < 8; i++)
                {
                    students.Add(new Student
                    {
                        Age = random.Next(1, 20),
                        MyTimeSpan = new TimeSpan(i,i,i),
                        FirstName = names[random.Next(0, 4)],
                        LastName = lastNames[random.Next(0, 4)],
                        Id = i
                    });
                }
            }
        }

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

        public ActionResult GetStudents([DataSourceRequest] DataSourceRequest request)
        {

            return Json(students, JsonRequestBehavior.AllowGet);
        }

        public ActionResult EditStudent(Student student)
        {
            students.Where(x => x.Id == student.Id).Select(x => student);

            return Json(student);
        }

        public ActionResult CreateStudent(Student student)
        {
            student.Id = students.Count + 1;
            students.Add(student);

            return Json(student);
        }

        public ActionResult DestroyStudent(Student student)
        {
            students.Remove(students.First(x => x.Id == student.Id));

            return Json(student);
        }
    }
}


