using HeBianGu.Base.WpfBase;
using HeBianGu.Service.Mvc;
using Hopetry.Services;
using Microsoft.Win32;
using Minio.DataModel.Args;
using Minio.DataModel;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Minio;
using Microsoft.Extensions.Configuration;

namespace HeBianGu.App.Disk
{
    [ViewModel("Loyout")]
    internal class LoyoutViewModel : MvcViewModelBase
    {
        private string _path;
        /// <summary> 说明  </summary>
        public string Path
        {
            get { return _path; }
            set
            {
                _path = value;
                RaisePropertyChanged("Path");
            }
        }


        private string _nearPath;
        /// <summary> 说明  </summary>
        public string NearPath
        {
            get { return _nearPath; }
            set
            {
                _nearPath = value;
                RaisePropertyChanged("NearPath");
            }
        }


        private string _sharePath;
        /// <summary> 说明  </summary>
        public string SharePath
        {
            get { return _sharePath; }
            set
            {
                _sharePath = value;
                RaisePropertyChanged("SharePath");
            }
        }



        protected override void Init()
        {
            Path = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);

            NearPath = Environment.GetFolderPath(Environment.SpecialFolder.Recent);

            SharePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

           // LinkActions.Add(new LinkAction() { Action = "Near", Controller = "Loyout", DisplayName = "最近使用", Logo = "\xe6f3" });
            LinkActions.Add(new LinkAction() { Action = "Explorer", Controller = "Loyout", DisplayName = "全部文件", Logo = "" });
           // LinkActions.Add(new LinkAction() { Action = "Image", Controller = "Loyout", DisplayName = "    图片", Logo = "" });
           // LinkActions.Add(new LinkAction() { Action = "Video", Controller = "Loyout", DisplayName = "    视频", Logo = "" });
           // LinkActions.Add(new LinkAction() { Action = "Document", Controller = "Loyout", DisplayName = "    文档", Logo = "" });
           // LinkActions.Add(new LinkAction() { Action = "Music", Controller = "Loyout", DisplayName = "    音乐", Logo = "" });
           // LinkActions.Add(new LinkAction() { Action = "Explorer", Controller = "Loyout", DisplayName = "    种子", Logo = "" });
            //LinkActions.Add(new LinkAction() { Action = "Recent", Controller = "Loyout", DisplayName = "    其他", Logo = "" });
           // LinkActions.Add(new LinkAction() { Action = "Space", Controller = "Loyout", DisplayName = "隐藏空间", Logo = "\xe613" });
            //LinkActions.Add(new LinkAction() { Action = "Share", Controller = "Loyout", DisplayName = "我的分享", Logo = "\xe764" });
           // LinkActions.Add(new LinkAction() { Action = "Near", Controller = "Loyout", DisplayName = "回收站", Logo = "\xe618" });

            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>
                      {
                          SelectLink = LinkActions[0];
                      }));

        }


        protected override void Loaded(string args)
        {

        }

        #region  文件上传
        private SendViewModel _sendViewModel;
        private IConfiguration config;
        public ICommand UploadCommand { get; }
        public LoyoutViewModel(SendViewModel sendViewModel, IConfiguration _config)
        {
            _sendViewModel = sendViewModel;
            config=_config;
            UploadCommand = new AsyncRelayCommand(async () => await UploadFile());
        }


        private async Task UploadFile()
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog();

            openFileDialog.Multiselect = true; // 可选择多个文件
            openFileDialog.Filter = "All Files (*.*)|*.*";
            //openFileDialog.Multiselect = false; 

            if (openFileDialog.ShowDialog() == true)
            {
                foreach (string filePath in openFileDialog.FileNames)
                {
                    // 处理选择的文件
                    UploadFileToMinIOWithProgress(filePath);
                }
            }
        }

        private async void UploadFileToMinIOWithProgress(string filePath)
        {
            var minioConfig = config.GetSection("Minio");
            IMinioClient client = new MinioClient()
                    .WithEndpoint(minioConfig["Endpoint"])
                    .WithCredentials(minioConfig["AccessKey"], minioConfig["SecretKey"]);
            try
            {
                string bucketName = "test";
                string objectName = System.IO.Path.GetFileName(filePath);

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

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

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

                // 上传文件并提供进度反馈
                await client.PutObjectAsync(putObjectArgs);
                MessageBox.Show("文件上传成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"上传失败: {ex.Message}");
            }
        }
        #endregion

    }

    internal class DataFileViewModel : ObservableSourceViewModel<TestViewModel>
    {
        protected override void Init()
        {
            base.Init();



        }
    }
}
