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

namespace Core_Grid_AjaxHierarchyEditing.Controllers
{
    public class GridController : Controller
    {
        public ActionResult Read_Employees([DataSourceRequest] DataSourceRequest request)
        {
            var result = Enumerable.Range(1, 10).Select(i => new EmployeeViewModel
            {
                EmployeeID = i,
                FirstName = "Cool Name" + i,
                LastName = "Smith",
                Country = "Ireland",
                City = "Dublin",
                Title = "Harry Potter"
            });

            var dsResult = result.ToDataSourceResult(request);
            return Json(dsResult);
        }

        [AcceptVerbs("Post")]
        public ActionResult Update_Employees([DataSourceRequest] DataSourceRequest request, EmployeeViewModel employee)
        {
            var results = new List<EmployeeViewModel>();
            if (employee != null && ModelState.IsValid)
            {
                results.Add(employee);
            }
            return Json(results.ToDataSourceResult(request));
        }
        [AcceptVerbs("Post")]
        public ActionResult Destroy_Employees([DataSourceRequest] DataSourceRequest request, EmployeeViewModel employee)
        {
            //delete logic
            return Json(new[] { employee }.ToDataSourceResult(request, ModelState));
        }
        public ActionResult Read_Orders([DataSourceRequest] DataSourceRequest request)
        {
            var result = Enumerable.Range(1, 4).Select(i => new OrderViewModel
            {
                OrderID = i,
                ShipCountry = "Scotland",
                ShipAddress = "Edinburgh",
                ShipName = "Titanic",
                ShippedDate = DateTime.UtcNow.AddDays(i)
            });

            var dsResult = result.ToDataSourceResult(request);
            return Json(dsResult);
        }
        [AcceptVerbs("Post")]
        public ActionResult Orders_Update([DataSourceRequest] DataSourceRequest request, OrderViewModel order)
        {
            var results = new List<OrderViewModel>();
            if (order != null && ModelState.IsValid)
            {
                results.Add(order);
            }
            return Json(results.ToDataSourceResult(request));
        }

        [AcceptVerbs("Post")]
        public ActionResult Orders_Destroy([DataSourceRequest] DataSourceRequest request, OrderViewModel order)
        {
            //delete logic
            return Json(new[] { order }.ToDataSourceResult(request, ModelState));
        }

    }
}
