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

namespace GridValidationDemo.Controllers
{
    public class GridController : Controller
    {
        public static List<OrderViewModel> orders = new List<OrderViewModel>();
        public GridController()
        {
            if (orders.Count == 0)
            {
                for (int i = 1; i < 50; i++)
                {
                    orders.Add(new OrderViewModel()
                    {
                        OrderID = i,
                        Freight = i * 10,
                        OrderDate = new DateTime(2016, 9, 15).AddDays(i % 7),
                        Code = "Code " + i,
                        ShipCity = "ShipCity " + i
                    });
                }
            }
        }
        public ActionResult Orders_Read([DataSourceRequest] DataSourceRequest request)
        {
            var dsResult = orders.ToDataSourceResult(request);
            return Json(dsResult);
        }


        public IActionResult CreateOrders([DataSourceRequest] DataSourceRequest request, OrderViewModel order)
        {
            if (ModelState.IsValid)
            {
                // Add a model Id so that the dataSource will treat this item as existing next time.
                order.OrderID = orders.Count + 1;

                // Save the item in the data base.
                orders.Add(order);
            }

            // Return a collection which contains only the newly created item and any validation errors.
            return Json(new[] { order }.ToDataSourceResult(request, ModelState));
        }

        public IActionResult UpdateOrders([DataSourceRequest] DataSourceRequest request, OrderViewModel order)
        {

            if (ModelState.IsValid)
            {
                // Save the item in the data base or follow with the dummy data.
                for (int i = 0; i < orders.Count; i++)
                {
                    // The example uses the model Id to identify the model that needs to be updated.
                    if (orders[i].OrderID == order.OrderID)
                    {
                        orders[i] = order;
                        break;
                    }
                }
            }

            // Return a collection which contains only the updated item and any validation errors.
            return Json(new[] { order }.ToDataSourceResult(request, ModelState));
        }

        public IActionResult DestroyOrders([DataSourceRequest] DataSourceRequest request, OrderViewModel order)
        {
            // Delete the item in the data base or follow with the dummy data.
            orders.Remove(order);

            // Return a collection which contains only the destroyed item.
            return Json(new[] { order }.ToDataSourceResult(request));
        }

        [AcceptVerbs("GET", "POST")]
        public IActionResult IsCode_Available(
            [Bind(Prefix = nameof(OrderViewModel.Code))] string Code,
            [Bind(Prefix = nameof(OrderViewModel.OrderID))] int id)
        {
            if (Code != null)
            {
                var sameID = orders.Where(p => p.OrderID == id);
                var existCode = orders.Where(p => p.Code == Code);
                if ((sameID.Count() > 0 && existCode.Count() > 0 && sameID.First().OrderID != existCode.First().OrderID)
                    || (sameID.Count() < 1 && existCode.Count() > 0))
                {
                    return Json($"The Code '{Code}' is already used!");
                }
            }

            return Json(true);
        }
    }
}
