using System.Net.Http.Json;
using TIAM.Entities.TransferDestinations;
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.BaseUrl}/{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.BaseUrl}/{APIUrls.GetTransferDestinationByCoordinates}";
            return await http.GetFromJsonAsync<TransferDestination>(url);
        }

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

        public async Task<TransferDestination?> CreateTransferDestination(TransferDestination model)
        {
            var url = $"{Setting.BaseUrl}/{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<TransferWizardModel?> CreateTransfer(TransferWizardModel model)
        {
            var url = $"{Setting.BaseUrl}/{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 = (TransferWizardModel)(await response.Content.ReadFromJsonAsync(typeof(TransferWizardModel)))!;

            return result;
        }

        public async Task<TransferDestination?> UpdateTransferDestination(TransferDestination model)
        {
            var url = $"{Setting.BaseUrl}/{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;
        }
    }
}
