using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TelerikAspNetCoreApp173.Data;

namespace TelerikAspNetCoreApp173.Pages
{
    public class IndexModel : PageModel
    {
        public static IList<OrderViewModel> orders;
        public static IList<EmployeeViewModel> employees;

        public void OnGet()
        {
            if (orders == null)
            {
                orders = new List<OrderViewModel>();

                Enumerable.Range(1, 10).ToList().ForEach(i => orders.Add(new OrderViewModel
                {
                    OrderID = i + 1,
                    Freight = i * 10,
                    OrderDate = new DateTime(2016, 9, 15).AddDays(i % 7),
                    ShipName = "ShipName " + i,
                    ShipCity = "ShipCity " + i
                }));

            }
            if (employees == null)
            {
                employees = new List<EmployeeViewModel>();

                Enumerable.Range(1, 10).ToList().ForEach(i => employees.Add(new EmployeeViewModel
                {
                    EmployeeID = i,
                    FirstName = "Cool Name" + i,
                    LastName = "Smith",
                    Country = "Ireland",
                    City = "Dublin",
                    Title = "Harry Potter"
                }));

            }
        }
        public ActionResult OnPostReadEmployees([DataSourceRequest] DataSourceRequest request)
        {
            var dsResult = employees.ToDataSourceResult(request);
            return new JsonResult(dsResult);
        }
        public ActionResult OnPostCreateEmployees([DataSourceRequest] DataSourceRequest request, EmployeeViewModel employee)
        {
            var results = new List<EmployeeViewModel>();
            if (employee != null && ModelState.IsValid)
            {
                results.Add(employee);
            }
            return new JsonResult(results.ToDataSourceResult(request));
        }

        public ActionResult OnPostUpdateEmployees([DataSourceRequest] DataSourceRequest request, EmployeeViewModel employee)
        {
            var results = new List<EmployeeViewModel>();
            if (employee != null && ModelState.IsValid)
            {
                results.Add(employee);
            }
            return new JsonResult(results.ToDataSourceResult(request));
        }
        public ActionResult OnPostDestroyEmployees([DataSourceRequest] DataSourceRequest request, EmployeeViewModel employee)
        {
            return new JsonResult(new[] { employee }.ToDataSourceResult(request, ModelState));
        }
        public JsonResult OnPostReadOrders([DataSourceRequest] DataSourceRequest request)
        {
            return new JsonResult(orders.ToDataSourceResult(request));
        }

        public JsonResult OnPostCreateOrders([DataSourceRequest] DataSourceRequest request, OrderViewModel order)
        {
            order.OrderID = orders.Count + 2;
            orders.Add(order);

            return new JsonResult(new[] { order }.ToDataSourceResult(request, ModelState));
        }

        public JsonResult OnPostUpdateOrders([DataSourceRequest] DataSourceRequest request, OrderViewModel order)
        {

            var results = new List<OrderViewModel>();
            if (order != null && ModelState.IsValid)
            {
                results.Add(order);
            }
            return new JsonResult(results.ToDataSourceResult(request));
        }

        public JsonResult OnPostDestroyOrders([DataSourceRequest] DataSourceRequest request, OrderViewModel order)
        {
            orders.Remove(orders.First(x => x.OrderID == order.OrderID));

            return new JsonResult(new[] { order }.ToDataSourceResult(request, ModelState));
        }
    }
}
