using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using Minio;
using Minio.Exceptions;

namespace TestMinio
{
    public partial class Form1 : Form
    {
        static  string  endpoint = "10.40.18.12:9000";
        static string accessKey = "root";
        static string secretKey = "root_password";
        // Initialize the client with access credentials.
        private static MinioClient minio = new MinioClient()
                                            .WithEndpoint(endpoint)
                                            .WithCredentials(accessKey, secretKey)
                                            .Build();

        // Create an async task for listing buckets.
       
        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            // Create an async task for listing buckets.
            var getListBucketsTask = await minio.ListBucketsAsync().ConfigureAwait(true);

            // Iterate over the list of buckets.
            foreach (var bucket in getListBucketsTask.Buckets)
            {
                Console.WriteLine($"{bucket.Name} {bucket.CreationDateDateTime} \n");
                textBox1.Text += $"{bucket.Name} {bucket.CreationDateDateTime} \n";
            }

            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                // 设置文件对话框的标题
                openFileDialog.Title = "选择要上传的文件";

                // 设置文件对话框允许选择的文件类型（例如：图片、文本文件等）
                openFileDialog.Filter = "All Files (*.*)|*.*";

                // 设置是否允许选择多个文件
                openFileDialog.Multiselect = false;

                // 打开文件对话框，并等待用户选择文件
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // 获取选择的文件路径并显示在 TextBox 中
                    label1.Text = openFileDialog.FileName;
                    Run(openFileDialog.FileName);
                
                }
            }
        }

        private async  Task<String> Run(String filePath)
        {
            var bucketName = "hbl";
            var objectName = System.IO.Path.GetFileName(filePath);
            var contentType = "application/zip";

            try
            {
                // Make a bucket on the server, if not already present.
                var beArgs = new BucketExistsArgs()
                    .WithBucket(bucketName);
                bool found = await minio.BucketExistsAsync(beArgs).ConfigureAwait(false);
                if (!found)
                {
                    var mbArgs = new MakeBucketArgs()
                        .WithBucket(bucketName);
                    await minio.MakeBucketAsync(mbArgs).ConfigureAwait(false);
                }
                // Upload a file to bucket.
                var putObjectArgs = new PutObjectArgs()
                    .WithBucket(bucketName)
                    .WithObject(objectName)
                    .WithFileName(filePath);
                await minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false);
                Console.WriteLine("Successfully uploaded " + objectName);
                var url =$"http://{endpoint}/hbl/{objectName}";
                MessageBox.Show(url);
                Console.WriteLine(url);
                return url;
            }
            catch (MinioException e)
            {
                Console.WriteLine("File Upload Error: {0}", e.Message);
            }
            return $"上传失败";
        }
    }
}
