using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using NorthwindRazorAPI.Domain;
using NorthwindRazorAPI.BusinessObject;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
 
namespace NorthwindRazor.Pages
{
     public class Products_ListCrudRedirectModel : PageModel
     {
 
         /// <summary>
         /// Handler, deletes a record.
         /// </summary>
         public async Task<IActionResultOnGetRemoveAsync(int id)
         {
             // delete data via web api
             HttpResponseMessage response = await Functions.HttpClientDeleteAsync("ProductsApi/Delete/" + id);
 
             // check the status of the httpclient delete operation
             if (response != null)
             {
                 if (response.IsSuccessStatusCode)
                     return new JsonResult(true);
                 else
                     return BadRequest(response.ReasonPhrase);
             }
 
             return BadRequest();
         }
 
         /// <summary>
         /// GET: /Products/GridData
         /// Gets the json needed by the jqgrid for use by the GridData
         /// </summary>
         public async Task<IActionResultOnGetGridDataAsync(string sidxstring sordint _pageint rows)
         {
             // get the total number of records
             string responseBody1 = await Functions.HttpClientGetAsync("ProductsApi/GetRecordCount/");
             int totalRecords = JsonSerializer.Deserialize<int>(responseBody1);
 
             // get records
             List<ProductsobjProductsCol = null;
             string responseBody2 = await Functions.HttpClientGetAsync("ProductsApi/SelectSkipAndTake/?rows=" + rows + "&page=" + _page + "&sidx=" + sidx + "&sord=" + sord);
 
             // make sure responseBody2 is not empty before deserialization
             if(!String.IsNullOrEmpty(responseBody2))
                 objProductsCol = JsonSerializer.Deserialize<List<Products>>(responseBody2);
 
             // calculate the total number of pages
             int totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
 
             // return a null in json for use by the jqgrid
             if (objProductsCol is null)
                 return new JsonResult("{ total = 0, page = 0, records = 0, rows = null }");
 
             // create a serialized json object for use by the jqgrid
             var jsonData = new
             {
                 total = totalPages,
                 _page,
                 records = totalRecords,
                 rows = (
                     from objProducts in objProductsCol
                     select new
                     {
                         id = objProducts.ProductID,
                         cell = new string[] { 
                             objProducts.ProductID.ToString(),
                             objProducts.ProductName,
                             objProducts.SupplierID.HasValue ? objProducts.SupplierID.Value.ToString() : "",
                             objProducts.CategoryID.HasValue ? objProducts.CategoryID.Value.ToString() : "",
                             objProducts.QuantityPerUnit,
                             objProducts.UnitPrice.HasValue ? objProducts.UnitPrice.Value.ToString() : "",
                             objProducts.UnitsInStock.HasValue ? objProducts.UnitsInStock.Value.ToString() : "",
                             objProducts.UnitsOnOrder.HasValue ? objProducts.UnitsOnOrder.Value.ToString() : "",
                             objProducts.ReorderLevel.HasValue ? objProducts.ReorderLevel.Value.ToString() : "",
                             objProducts.Discontinued.ToString()
                         }
                     }).ToArray()
             };
 
             return new JsonResult(jsonData);
         }
     }
}