using Azure;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
using Microsoft.IdentityModel.Tokens;
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using TIAM.Entities.TransferDestinations;
using TIAMWebApp.Shared.Application.Interfaces;
using TIAMWebApp.Shared.Application.Models.ClientSide;
using TIAMWebApp.Shared.Application.Models;
using static System.Net.WebRequestMethods;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace TIAMWebApp.Shared.Application.Services
{
    public class ImageDataService : IImageDataService
    {
        private readonly HttpClient _http;

        public ImageDataService(HttpClient httpClient)
        {
            _http = httpClient;
        }        

        public async Task<List<string>> GetImagesAsync(string userId)
        {
            var response = await _http.GetAsync("images");

            if (response.IsSuccessStatusCode)
            {
                var fileNames = await response.Content.ReadFromJsonAsync<List<string>>();
                return fileNames;
            }

            throw new ApplicationException($"Status code: {response.StatusCode}, Error: {response.ReasonPhrase}");
        }

        public async Task<Stream> GetImageAsync(string name)
        {
            var response = await _http.GetAsync($"image/{name}");

            if (response.IsSuccessStatusCode)
            {
                var stream = await response.Content.ReadAsStreamAsync();
                return stream;
            }

            throw new ApplicationException($"Status code: {response.StatusCode}, Error: {response.ReasonPhrase}");
        }

        public async Task<string> UploadImageAsync(MultipartContent image)
        {
            var url = $"{Setting.BaseUrl}/{APIUrls.UploadImage}";
            var response = await _http.PostAsync(url, image);

            //var result = new WizardProcessorResult();

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

            if (!response.IsSuccessStatusCode)
                return "Oops, something wrong";

            var result = await response.Content.ReadAsStringAsync();

            return result;
        }
    }

}
