using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using Txgy.EWS.Client.Common.MessageEvents;
using Txgy.EWS.Client.Common;
using Txgy.EWS.Client.IBLL;
using Unity;
using Txgy.EWS.Client.Models;

namespace Txgy.EWS.Client.SysModule.ViewModels
{
    public class FileUploadViewModel:PageViewModelBase
    {
        public ObservableCollection<FileInfoModel> Files { get; set; } = new ObservableCollection<FileInfoModel>();

        IUnityContainer _unityContainer;
        IEventAggregator _ea;
        IDialogService _dialogService;
        IUpgradeFileBLL _upgradeFileBLL;

        public FileUploadViewModel(
            IRegionManager regionManager,
            IUnityContainer unityContainer,
            IEventAggregator ea,
            IDialogService dialogService,
            IUpgradeFileBLL upgradeFileBLL)
            : base(regionManager, unityContainer, ea)
        {
            _unityContainer = unityContainer;
            _ea = ea;
            _dialogService = dialogService;
            _upgradeFileBLL = upgradeFileBLL;

            this.PageTitle = "更新文件上传管理";

            InitInfo();
        }

        private void InitInfo()
        {
            _ea.GetEvent<ReLoadEvent>().Subscribe(Refresh);
        }

        public override void Refresh()
        {
            this.ShowLoading();
            Files.Clear();
            Task.Run(new Action(async () =>
            {
                var files = await _upgradeFileBLL.AllFileListAsync();
                _unityContainer.Resolve<Dispatcher>().Invoke(() =>
                {
                    files.ForEach(f => { Files.Add(f); });
                });
                this.HideLoading();
            }));
        }

        public override void AddItem()
        {
            // Prism框架的Dialog对象
            _dialogService.ShowDialog("AddFileDialog");
        }
        public override bool CanExcute()
        {
            return true;
        }
    }
}
