using GxPress.Common.AppOptions;
using GxPress.Common.Tools;
using GxPress.Service.Implement;
using GxPress.Service.Interface.Storage;
using Microsoft.Extensions.DependencyInjection;
using Minio;

namespace GxPress.Api.ServiceExtensions
{
    public static class StorageExtension
    {
        public static IServiceCollection AddStorage(this IServiceCollection services, StorageOptions options)
        {
            var isSettings = false;
            if (options.StorageType == "minio" && !string.IsNullOrEmpty(options.Endpoint))
            {
                isSettings = true;

                // Initialize the client with access credentials.
                var minio = new MinioClient(options.Endpoint,
                                options.AccessKey,
                                options.SecretKey
                                );
                if (options.SSL){
                    minio.WithSSL();
                }

                var minIOStorage = new MinIOStorage(minio);

                services.AddSingleton<IStorage>(minIOStorage);
            }

            if (!isSettings)
            {
                var fileStorage = new FileStorage();
                services.AddSingleton<IStorage>(fileStorage);
            }
            return services;
        }
    }
}