using Minio;
using Minio.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoviDok.BLL
{
    class MinioHandler
    {
        private MinioClient Client = null;

        public class MinioResult
        {
            bool Success;
            string Data;

            public MinioResult(bool Success, string Data)
            {
                this.Success = Success;
                this.Data = Data;
            }
        }

        public MinioHandler()
        {
            Client = new MinioClient(
                "192.168.0.160:9000",
                "secretaccesskey",
                "secretsecretkey");
        }

        public async Task<MinioResult> Upload(string BucketName, string FilePath, string ObjectName)
        {
            try
            {
                // Make a bucket on the server, if not already present.
                bool found = await Client.BucketExistsAsync(BucketName);
                if (!found)
                {
                    await Client.MakeBucketAsync(BucketName);
                }
                // Upload a file to bucket.
                await Client.PutObjectAsync(BucketName, ObjectName, FilePath);
                return new MinioResult(true, BucketName + ":" + ObjectName);
            }
            catch (MinioException e)
            {
                return new MinioResult(false, e.Message);
            }
        }

        public async Task<MinioResult> GetImage(string BucketName, string FilePath, string ObjectName)
        {
            try
            {
                await Client.GetObjectAsync(BucketName, ObjectName, FilePath);
                return new MinioResult(true, FilePath);
            }
            catch (MinioException e)
            {
                return new MinioResult(false, e.Message);
            }
        }
    }
}