using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using WebApplication6.Models;

namespace WebApplication6.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        [HttpPost]
        public ActionResult GetAllOrders([DataSourceRequest] DataSourceRequest request)
        {
            Orders orders = new Orders();
            List<Order> orderList = orders.GetAll();
            DataSourceResult result = orderList.ToDataSourceResult(request,
              order => new OrderViewModel
              {
                  Id = order.Id,
                  LastName = order.LastName,
                  OrderCurrencyCode = order.OrderCurrencyCode,
                  OrderCurrencyCultureCode = order.OrderCurrencyCultureCode,
                  TotalAmount = order.TotalAmount,
                  TotalAmountUsd = order.TotalAmountUsd,
                  OrderDate = order.OrderDate
              }
            );

            return Json(result, new JsonSerializerOptions() { PropertyNameCaseInsensitive = false });
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
