using Core_Grid_CRUD.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_CRUD.Controllers
{
    public class GridController : Controller
    {
        public ActionResult FooterData()
        {
            var data = 1234.231;
            return Json(data);
        }
        public virtual List<OrderViewModel> GetFirst()
        {

            var results = Enumerable.Range(1,1).Select(i => new OrderViewModel
            {
                OrderID = i,
                Freight = i % 2 == 0 ? (i - 1) * 10 : i * 10,
                OrderDate = new DateTime(2016, 9, 15).AddDays(i % 7),
                //ExpectedDate = null,
                ShipName = "ShipName"+i,
                ShipCity = "ShipCity " + i,
                Arrived = i % 3 == 1
            });

            return results.ToList();
        }
        public virtual List<OrderViewModel> GetAll()
        {

            var results = Enumerable.Range(1, 50).Select(i => new OrderViewModel
            {
                OrderID = i,
                Freight = i % 2 == 0 ? (i - 1) * 10 : i * 10,
                OrderDate = new DateTime(2016, 9, 15).AddDays(i % 7),
                //ExpectedDate = null,
                ShipName = "ShipName"+i,
                ShipCity = "ShipCity " + i,
                Arrived = i%3==1
            });

            return results.ToList();
        }

        public ActionResult Orders_Read([DataSourceRequest] DataSourceRequest request, DateTime date, Boolean firstRequest)
        {
            

                var result = GetAll();
                var dsResult = result.ToDataSourceResult(request);
                return Json(dsResult);
           
            
        }
        public ActionResult Orders_Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")] IEnumerable<OrderViewModel> orders)
        {
            IEnumerable<OrderViewModel> results = new List<OrderViewModel>();
            if (orders != null && ModelState.IsValid)
            {
                foreach(var order in orders)
                {
                    order.Freight+=10;
                }
                results = orders;
            }
            return Json(results.ToDataSourceResult(request));
        }
        public ActionResult Orders_Create([DataSourceRequest] DataSourceRequest request,
            [Bind(Prefix = "models")] List<OrderViewModel> orders)
        {

            var results = new List<OrderViewModel>();
            if (orders != null && ModelState.IsValid)
            {
                results = orders;
            }
            return Json(results.ToDataSourceResult(request, ModelState));

        }
        public ActionResult Orders_Destroy([DataSourceRequest] DataSourceRequest request,
            [Bind(Prefix = "models")] List<OrderViewModel> orders)
        {
            //ModelState.AddModelError("Destroy", "An unexplained error");
            var results = new List<OrderViewModel>();
            if (orders != null && ModelState.IsValid)
            {
                foreach (var order in orders)
                {
                    results.Add(order);
                }
            }
            return Json(results.ToDataSourceResult(request, ModelState));
        }

    }
}
