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 TelerikMvcApp106.Models;

namespace TelerikMvcApp106.Controllers
{
    public class SchedulerController : Controller
    {
        public static List<TaskViewModel> Tasks { get; set; }
        // GET: Scheduler
        [HttpPost]
        public ActionResult Pdf_Export_Save(string contentType, string base64, string fileName)
        {
            var fileContents = Convert.FromBase64String(base64);

            return File(fileContents, contentType, fileName);
        }

        public virtual JsonResult Pdf_Export_Read([DataSourceRequest] DataSourceRequest request)
        {
            if (Tasks == null)
            {
                Tasks = new List<TaskViewModel>()
                {
                    new TaskViewModel()
                    {
                        TaskID=1,
                        Title = "Item 1",
                        Start = DateTime.Now,
                        End= DateTime.Now.AddHours(2)
                    }
                };
            }
            
            return Json(Tasks.ToDataSourceResult(request));
        }

        public virtual JsonResult Pdf_Export_Destroy([DataSourceRequest] DataSourceRequest request, TaskViewModel task)
        {
            if (ModelState.IsValid)
            {
                var target = Tasks.First(x => x.TaskID == task.TaskID);
                Tasks.Remove(target);
            }

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

        public virtual JsonResult Pdf_Export_Create([DataSourceRequest] DataSourceRequest request, TaskViewModel task)
        {
            if (ModelState.IsValid)
            {
                task.TaskID = Tasks.Count + 1;
                Tasks.Add(task);
            }

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

        public virtual JsonResult Pdf_Export_Update([DataSourceRequest] DataSourceRequest request, TaskViewModel task)
        {
            if (ModelState.IsValid)
            {
                var target = Tasks.First(x => x.TaskID == task.TaskID);
                target = task;
            }

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