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

namespace TelerikMvcApp20.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        private List<SchedulerEventModel> appointments = new List<SchedulerEventModel> {
            new SchedulerEventModel {
                Description = "Task 1 Description",
                Title = "Task 1",
                Start = new DateTime(2020,6,15,12,00,00),
                End= new DateTime(2020,6,15,13,30,00),
            },
            new SchedulerEventModel {
                Description = "Task 2 Description",
                Title = "Task 2",
                Start = new DateTime(2020,6,15,12,00,00),
                End= new DateTime(2020,6,15,14,30,00),
            },
            new SchedulerEventModel {
                Description = "Task 3 Description",
                Title = "Task 3",
                Start = new DateTime(2020,6,15,14,00,00),
                End= new DateTime(2020,6,15,16,00,00),
            },
            new SchedulerEventModel {
                Description = "Task 4 Description",
                Title = "Task 4",
                Start = new DateTime(2020,6,15,13,00,00),
                End= new DateTime(2020,6,15,14,00,00),
            },
            new SchedulerEventModel {
                Description = "Recurring Task Description",
                Title = "Recurring Task",
                Start = new DateTime(2020,6,15,11,00,00),
                End= new DateTime(2020,6,15,12,00,00),
                RecurrenceRule = "FREQ=DAILY;COUNT=10"
            }
        };

        public JsonResult Read([DataSourceRequest] DataSourceRequest request)
        {
            return Json(appointments.ToDataSourceResult(request));
        }

        public virtual JsonResult Delete([DataSourceRequest] DataSourceRequest request, SchedulerEventModel task)
        {
            if (ModelState.IsValid)
            {
                appointments.Remove(appointments.Where(x => x.TaskID == task.TaskID).FirstOrDefault());
            }

            return Json(new[] { task }.ToDataSourceResult(request, ModelState));
        }

        public virtual JsonResult Create([DataSourceRequest] DataSourceRequest request, SchedulerEventModel task)
        {
            if (ModelState.IsValid)
            {
                task.TaskID = ++appointments.LastOrDefault().TaskID;
                appointments.Add(task);
            }

            return Json(new[] { task }.ToDataSourceResult(request, ModelState));
        }

        public virtual JsonResult Update([DataSourceRequest] DataSourceRequest request, SchedulerEventModel task)
        {
            if (ModelState.IsValid)
            {
                var appointmentToUpdate = appointments.Where(x => x.TaskID == task.TaskID).FirstOrDefault();
                if (appointmentToUpdate != null)
                {
                    appointmentToUpdate = task;
                }
            }

            return Json(new[] { task }.ToDataSourceResult(request, ModelState));
        }
    }
}
