using Netina.Common.Extensions;
using Netina.Common.Models.Api;
using Netina.Core.Abstracts;
using Netina.Core.Utilities;

namespace Netina.Infrastructure.Services;

public class UploadFileService : IUploadFileService
{
    private readonly IStorageService _storageService;
    private readonly SiteSettings _siteSetting;

    public UploadFileService(IStorageService storageService,IOptionsSnapshot<SiteSettings> optionsSnapshot)
    {
        _storageService = storageService;
        _siteSetting = optionsSnapshot.Value;
        
    }
    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);
        string medFileName;
        if (uploadRequest.FileUploadType == FileUploadType.Logo)
        {
            uploadRequest.FileName = $"Main.{uploadRequest.FileName.Split('.').Last()}";
            medFileName = await _storageService.UploadObjectFromFileAsync(uploadRequest.FileName, $"{uploadRequest.FileUploadType.ToDisplay()}/Med", uploadRequest.ContentType, mediumFileStream,false);
            await _storageService.UploadObjectFromFileAsync(medFileName, $"{uploadRequest.FileUploadType.ToDisplay()}/Thumb", uploadRequest.ContentType, thumbnailFileStream, false);
        }
        else
        {
            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 = $"{_siteSetting.StorageBaseUrl}/{uploadRequest.FileUploadType.ToDisplay()}/Med/{medFileName}"
        };
        return response;
    }

    public async Task<FileUploadResponse> UploadFileByteAsync(FileUploadRequest uploadRequest)
    {

        var medFileName = await _storageService.UploadObjectFromFileAsync(uploadRequest.FileName, $"{uploadRequest.FileUploadType.ToDisplay()}", uploadRequest.ContentType, uploadRequest.FileBytes,false);
        var response = new FileUploadResponse
        {
            FileName = medFileName,
            FileLocation = $"{uploadRequest.FileUploadType.ToDisplay()}/{medFileName}",
            FileUrl = $"https://storage.vesmook.com/{uploadRequest.FileUploadType.ToDisplay()}/{medFileName}"
        };
        return response;
    }
}