using IVM.Models;
using IVM.Models.HttpResponses;
using Kendo.Mvc.UI;
using Newtonsoft.Json;

namespace IVM.Services.IVMAPIService
{
    public partial class IvmApiService
    {
        internal void DeleteAddress(string Id)
        {
            MakeRequest($"Address?id={Id}", ref Pager, Method.DElETE);
        }

        internal string UpdateAddress(IFormCollection form)
        {
            JsonHelper JHelp = new(form, "addressTypes");
            JHelp.BuildAddressJSon();

            Address address = JsonConvert.DeserializeObject<Address>(JHelp.JsonString);
            JHelp.JsonString = JsonConvert.SerializeObject(address);
            JsonString = JHelp.JsonString;
            string? response = null;
            if (JHelp.JsonString != null)
                response = MakeRequest($"Address", ref Pager, Method.PUT);
            return response;
        }
        internal object GetAddress(IQueryCollection query)
        {
            JsonHelper Query = new JsonHelper(query, "Address?activeStatus = Active");
            Query.BuildAddressUrl();
            string? jsRequest = MakeRequest(Query.JsonString, new JsonHelper() { JsonString = "{ 'Data', 'Array' }" });
            APIPagedMultiSortedResponse<List<Address>>? response = JsonConvert.DeserializeObject<APIPagedMultiSortedResponse<List<Address>>>(jsRequest);
            return new Response<List<Address>>() { Data = response.Data, Total = response.Pagination.TotalRecords };
        }

        internal object? GetAddress(DataSourceRequest request, JsonHelper jHelper)
        {
            string? jsRequest = MakeRequest($"Address?activeStatus=Active&page={request.Page}&pagesize={request.PageSize}", jHelper);
            APIPagedMultiSortedResponse<List<Address>>? response = JsonConvert.DeserializeObject<APIPagedMultiSortedResponse<List<Address>>>(jsRequest);
            return new Response<List<Address>>() { Data = response.Data, Total = response.Pagination.TotalRecords };
        }

        internal string CreateAddress(IFormCollection form)
        {
            string retval = "";
            JsonHelper JHelp = new(form, "addressTypes");
            JHelp.BuildAddressJSon();
            JsonString = JHelp.JsonString;
            //retval = MakeRequest($"Address", ref Pager, Method.POST);
            return retval;
        }
        //internal EditModel getEditRecord(int Id) { return JsonConvert.DeserializeObject<EditModel>(MakeRequest($"address/GetById?activeStatus=Active&id={Id}", ref Pager)); }
        //internal EditModel getAddressEdit(int Id) { return JsonConvert.DeserializeObject<EditModel>(MakeRequest($"address/GetById?activeStatus=Active&id={Id}", ref Pager)); }

    }
}
