using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Mvc;
using TelerikAspNetCoreApp136.Models;

namespace TelerikAspNetCoreApp136.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        private List<SchedulerEventModel> appointments = new List<SchedulerEventModel> {
            new SchedulerEventModel {
                Description = "Task 1 Description",
                Title = "Task 1",
                Start = new DateTime(2019,10,29,7,00,00),
                End= new DateTime(2019,10,29,8,30,00),
            },
            new SchedulerEventModel {
                Description = "Task 2 Description",
                Title = "Task 2",
                Start = new DateTime(2019,10,29,14,00,00),
                End= new DateTime(2019,10,29,15,00,00),
            }
        };

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

        public JsonResult Read_Grid([DataSourceRequest] DataSourceRequest request)
        {
            List<SchedulerEventModel> gridData = new List<SchedulerEventModel> {
                new SchedulerEventModel {
                    Description = "Task 10 Description",
                    Title = "Task 10",
                    Start = new DateTime(2019,10,29,4,00,00),
                    End= new DateTime(2019,10,29,5,30,00),
                },
                new SchedulerEventModel {
                    Description = "Task 11 Description",
                    Title = "Task 11",
                    Start = new DateTime(2019,10,29,18,00,00),
                    End= new DateTime(2019,10,29,19,00,00),
                },
                new SchedulerEventModel {
                    Description = "Task 12 Description",
                    Title = "Task 12",
                    Start = new DateTime(2019,10,29,11,00,00),
                    End= new DateTime(2019,10,29,12,00,00),
                },
            };

            return Json(gridData.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));
        }
    }
}
