using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using Tango.Core.Commands;
using Tango.Core.DI;
using Tango.FileSystem;
using Tango.FSE.Common.Notifications;
using Tango.FSE.Common.Storage;
using Tango.SharedUI;
namespace Tango.FSE.UI.Storage
{
///
/// Currently not in use!
///
internal class StorageViewVM : ViewModel
{
private FileSystemManager _manager;
private Action _okCompletionAction;
private Action _cancelCompletionAction;
[TangoInject(Mode = TangoInjectMode.WhenAvailable)]
private INotificationProvider NotificationProvider { get; set; }
private bool _isOpened;
public bool IsOpened
{
get { return _isOpened; }
private set { _isOpened = value; RaisePropertyChangedAuto(); }
}
private String _title;
public String Title
{
get { return _title; }
private set { _title = value; RaisePropertyChangedAuto(); }
}
private String _fileName;
public String FileName
{
get { return _fileName; }
set { _fileName = value; RaisePropertyChangedAuto(); }
}
private FileSystemItem _currentItem;
public FileSystemItem CurrentItem
{
get { return _currentItem; }
set { _currentItem = value; RaisePropertyChangedAuto(); OnCurrentItemChanged(); }
}
private String _currentPath;
public String CurrentPath
{
get { return _currentPath; }
set { _currentPath = value; RaisePropertyChangedAuto(); }
}
public String Filter { get; set; } = "*.*";
private bool _isBusy;
public bool IsBusy
{
get { return _isBusy; }
private set { _isBusy = value; RaisePropertyChangedAuto(); }
}
private List _drives;
public List Drives
{
get { return _drives; }
set { _drives = value; RaisePropertyChangedAuto(); }
}
public ObservableCollection SelectedItems { get; set; }
private FileSystemItem _selectedItem;
public FileSystemItem SelectedItem
{
get { return _selectedItem; }
set { _selectedItem = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
}
private SelectionMode _selectionMode;
public SelectionMode SelectionMode
{
get { return _selectionMode; }
set { _selectionMode = value; RaisePropertyChangedAuto(); }
}
private StorageMode _storageMode;
public StorageMode StorageMode
{
get { return _storageMode; }
set { _storageMode = value; RaisePropertyChangedAuto(); }
}
public RelayCommand OKCommand { get; }
public RelayCommand CancelCommand { get; }
public RelayCommand BackCommand { get; set; }
public RelayCommand NavigateCommand { get; set; }
public RelayCommand OpenCommand { get; set; }
public RelayCommand NavigateSpecialFolderCommand { get; set; }
public RelayCommand NavigateToFolderCommand { get; set; }
public StorageViewVM()
{
_manager = new FileSystemManager();
SelectedItems = new ObservableCollection();
NavigateCommand = new RelayCommand(NavigateToCurrentPath);
OpenCommand = new RelayCommand(OpenFileSystemItem);
BackCommand = new RelayCommand(NavigateBack, () => !(CurrentItem is FolderItem) || !(CurrentItem as FolderItem).IsRoot);
NavigateSpecialFolderCommand = new RelayCommand(NavigateToSpecialFolder);
NavigateToFolderCommand = new RelayCommand(async (x) => await Navigate(x));
OKCommand = new RelayCommand(OnAccept, CanOK);
CancelCommand = new RelayCommand(OnCancel);
}
private void OnAccept()
{
IsOpened = false;
_okCompletionAction?.Invoke();
}
private void OnCancel()
{
IsOpened = false;
_cancelCompletionAction?.Invoke();
}
private async void NavigateToSpecialFolder(string folder)
{
Environment.SpecialFolder specialFolder = (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), folder);
await Navigate(null, specialFolder);
}
private async void NavigateBack()
{
if (CurrentItem.Path.Length == 3)
{
await Navigate(null);
}
else
{
String parent = Path.GetDirectoryName(CurrentItem.Path);
await Navigate(parent);
}
}
private async void NavigateToCurrentPath()
{
await Navigate(CurrentPath);
}
private async void OpenFileSystemItem(FileSystemItem item)
{
if (item == null) return;
if (item.Type == FileSystemItemType.Folder || item.Type == FileSystemItemType.Drive)
{
await Navigate(item.Path);
}
else if (item.Type == FileSystemItemType.File)
{
//TODO: Download/Open file?...
}
}
private async Task Navigate(String path, Environment.SpecialFolder? specialFolder = null)
{
try
{
IsBusy = true;
Mouse.OverrideCursor = Cursors.AppStarting;
if (path != null)
{
CurrentItem = await _manager.GetFolder(path, StorageMode == StorageMode.SelectFolder, Filter) as FileSystemItem;
}
else if (specialFolder != null)
{
CurrentItem = await _manager.GetFolder(specialFolder.Value, StorageMode == StorageMode.SelectFolder, Filter) as FileSystemItem;
}
else
{
CurrentItem = await _manager.GetFolder(String.Empty, StorageMode == StorageMode.SelectFolder, Filter) as FileSystemItem;
}
}
catch (Exception ex)
{
IsBusy = false;
Mouse.OverrideCursor = null;
await NotificationProvider.ShowError($"Error navigating to the specified path.\n{ex.FlattenMessage()}");
}
finally
{
IsBusy = false;
Mouse.OverrideCursor = null;
}
}
private void OnCurrentItemChanged()
{
CurrentPath = CurrentItem.Path;
InvalidateRelayCommands();
}
private bool CanOK()
{
if (StorageMode == StorageMode.SelectFolder && CurrentItem.Type == FileSystemItemType.Drive)
{
return false;
}
return true;
}
}
}