using NetinaShop.Core.Utilities;

namespace NetinaShop.Infrastructure.Services;

public class UploadFileService : IUploadFileService
{
    private readonly IStorageService _storageService;

    public UploadFileService(IStorageService storageService)
    {
        _storageService = storageService;
    }
    public async Task<FileUploadResponse> UploadImageAsync(FileUploadRequest uploadRequest)
    {
        var bytes = Convert.FromBase64String(uploadRequest.StringBaseFile);
        using var originalMedFileStream = new MemoryStream(bytes);
        using var originalThumbFileStream = new MemoryStream(bytes);
        using var thumbnailFileStream = new MemoryStream();
        using var mediumFileStream = new MemoryStream();

        await uploadRequest.ImageResize(originalMedFileStream, mediumFileStream, 1280);
        await uploadRequest.ImageResize(originalThumbFileStream, thumbnailFileStream, 200);
        var medFileName = await _storageService.UploadObjectFromFileAsync(uploadRequest.FileName, $"{uploadRequest.FileUploadType.ToDisplay()}/Med", uploadRequest.ContentType, mediumFileStream);
        await _storageService.UploadObjectFromFileAsync(medFileName, $"{uploadRequest.FileUploadType.ToDisplay()}/Thumb", uploadRequest.ContentType, thumbnailFileStream, false);
        var response = new FileUploadResponse
        {
            FileName = medFileName,
            FileLocation = $"{uploadRequest.FileUploadType.ToDisplay()}/Med/{medFileName}",
            FileUrl = $"https://storage.vesmook.com/{uploadRequest.FileUploadType.ToDisplay()}/Med/{medFileName}"
        };
        return response;
    }
}