using System.Collections.Concurrent;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Channels;
using System.Windows.Threading;
using FileUploader.Models;
using Microsoft.Extensions.Configuration;
using Minio;
using Minio.ApiEndpoints;
using Minio.DataModel;
using Minio.DataModel.Args;
using Minio.DataModel.Notification;
using Minio.Exceptions;

namespace Hopetry.Services
{
    public partial class MinioService
    {
        #region 多文件上传
        public async void UploadFileToMinIOWithProgress(string filePath)
        {
            try
            {
                string bucketName = "test";
                string objectName = System.IO.Path.GetFileName(filePath);

                //判断桶是否存在
                var beArgs = new BucketExistsArgs().WithBucket(bucketName);
                bool found = await _minioClient.BucketExistsAsync(beArgs).ConfigureAwait(false);
                if (!found)
                {
                    var mbArgs = new MakeBucketArgs()
                        .WithBucket(bucketName);
                    await _minioClient.MakeBucketAsync(mbArgs).ConfigureAwait(false);
                }

                var progress = new Progress<ProgressReport>(progressReport =>
                {
                    //Dispatcher.Invoke(() =>
                    //{
                    //    progressBar.Value = progressReport.Percentage;// 更新进度条的值
                    //});
                });

                PutObjectArgs putObjectArgs = new PutObjectArgs()
                                  .WithBucket(bucketName)
                                  .WithObject(objectName)
                                  .WithFileName(filePath)
                                  //.WithContentType("application/octet-stream")
                                  .WithProgress(progress);

                // 上传文件并提供进度反馈
                await _minioClient.PutObjectAsync(putObjectArgs);

            }
            catch (Exception ex)
            {
                throw new Exception($"上传文件失败: {ex.Message}");
            }
        }
        #endregion
    }
}
