using System.Net.Http.Json;
using System.Text.Json;
using TIAM.Entities.Transfers;
using TIAMWebApp.Shared.Application.Interfaces;
using TIAMWebApp.Shared.Application.Models;
using TIAMWebApp.Shared.Application.Models.ClientSide;
using TIAMWebApp.Shared.Application.Models.ClientSide.UI.WizardModels;
using TIAMWebApp.Shared.Application.Utility;

namespace TIAMWebApp.Shared.Application.Services
{
    public class TransferDataService : ITransferDataService
    {
        private readonly HttpClient http;
        private readonly LogToBrowserConsole logToBrowserConsole;

        public TransferDataService(HttpClient http, LogToBrowserConsole logToBrowserConsole) 
        {
            this.http = http;
            this.logToBrowserConsole = logToBrowserConsole;
        }


        public async Task<List<TransferDestination>> GetDestinationsAsync()
        {
            
            var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransferDestinations}";
            //var url = $"{APIUrls.GetTransferDestinations}";
            logToBrowserConsole.LogToBC(url);
            List<TransferDestination>? response = await http.GetFromJsonAsync<List<TransferDestination>>(url);
            if(response == null)
                return new List<TransferDestination>();
            return response;
        }

        public async Task<TransferDestination?> GetTransferDestinationbyCoordinatesAsync(string destinationId)
        {
            var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransferDestinationByCoordinates}";
            return await http.GetFromJsonAsync<TransferDestination>(url);
        }

        public async Task<TransferDestination?> GetTransferDestinationbyAddressAsync(string destinationId)
        {
            var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransferDestinationByAddress}";
            return await http.GetFromJsonAsync<TransferDestination>(url);
        }

        public async Task<TransferDestination?> CreateTransferDestination(TransferDestination model)
        {
            var url = $"{Setting.ApiBaseUrl}/{APIUrls.CreateTransferDestination}";
            var response = await http.PostAsJsonAsync(url, model);

            //var result = new WizardProcessorResult();

            //if (response.IsSuccessStatusCode)
            //{
            //    result.IsSucces = true;
            //    result.ResultJson = await response.Content.ReadAsStringAsync();
            //}

            if (!response.IsSuccessStatusCode)
                return null;

            var result = (TransferDestination)(await response.Content.ReadFromJsonAsync(typeof(TransferDestination)))!;
            
            return result;
        }

        public async Task<Transfer?> CreateTransfer(TransferWizardModel model)
        {            
            logToBrowserConsole.LogToBC("CreateTransfer called");
            logToBrowserConsole.LogToBC(model.PickupAddress);
            logToBrowserConsole.LogToBC(model.Destination);
            logToBrowserConsole.LogToBC(model.EmailAddress);
            logToBrowserConsole.LogToBC(model.PhoneNumber);
            logToBrowserConsole.LogToBC(model.TripDate.ToString());
            logToBrowserConsole.LogToBC(model.NumberOfPassengers.ToString());
            model.Id = Guid.NewGuid();

            var transfer = model.CopyToTransfer();


            var url = $"{Setting.ApiBaseUrl}/{APIUrls.CreateTransfer}";
            var response = await http.PostAsJsonAsync(url, transfer);


            //var result = new WizardProcessorResult();

            //if (response.IsSuccessStatusCode)
            //{
            //    result.IsSucces = true;
            //    result.ResultJson = await response.Content.ReadAsStringAsync();
            //}

            if (!response.IsSuccessStatusCode)
                return null;

            var result = (Transfer)(await response.Content.ReadFromJsonAsync(typeof(Transfer)))!;

            return result;
        }

        public async Task<List<Transfer>?> CreateTransfers(List<TransferWizardModel> modelList)
        {
            List<Transfer> transferList = new List<Transfer>();
            foreach (var model in modelList)
            {
                model.Id = Guid.NewGuid();
                var transfer = model.CopyToTransfer();
                transferList.Add(transfer);
            }
           
            var url = $"{Setting.ApiBaseUrl}/{APIUrls.CreateTransfers}";
            var response = await http.PostAsJsonAsync(url, transferList);


            //var result = new WizardProcessorResult();

            //if (response.IsSuccessStatusCode)
            //{
            //    result.IsSucces = true;
            //    result.ResultJson = await response.Content.ReadAsStringAsync();
            //}

            if (!response.IsSuccessStatusCode)
                return null;

            var result = (List<Transfer>)(await response.Content.ReadFromJsonAsync(typeof(List<Transfer>)))!;

            return result;
        }

        public async Task<TransferDestination?> UpdateTransferDestination(TransferDestination model)
        {
            var url = $"{Setting.ApiBaseUrl}/{APIUrls.UpdateTransferDestination}";
            var response = await http.PostAsJsonAsync(url, model);

            //var result = new WizardProcessorResult();

            //if (response.IsSuccessStatusCode)
            //{
            //    result.IsSucces = true;
            //    result.ResultJson = await response.Content.ReadAsStringAsync();
            //}

            if (!response.IsSuccessStatusCode)
                return null;

            var result = (TransferDestination)(await response.Content.ReadFromJsonAsync(typeof(TransferDestination)))!;

            return result;
        }

        public async Task<Transfer?> GetTransferByIdAsync(Guid id)
        {
            var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransferById}";
            //var url = $"{APIUrls.GetTransferDestinations}";
            logToBrowserConsole.LogToBC(url);
            Transfer? response = await http.GetFromJsonAsync<Transfer>(url);
            if (response == null)
                return new Transfer();
            return response;
        }

        public async Task<List<Transfer>> GetTransfersAsync()
        {
            var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransfers}";
            //var url = $"{APIUrls.GetTransferDestinations}";
            logToBrowserConsole.LogToBC(url);
            //add json settings ignore readonly properties
            JsonSerializerOptions options = new JsonSerializerOptions
            {
                IgnoreReadOnlyProperties = true
            };

            List<Transfer>? response = await http.GetFromJsonAsync<List<Transfer>>(url, options);
            if (response == null)
                return new List<Transfer>();
            return response;
        }
    }
}
