using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using MPF.Core.Data;
using MPF.Core.UI.ComboBoxItems;
using SabreTools.RedumpLib.Web;
using WPFCustomMessageBox;
namespace MPF.UI.Core.ViewModels
{
public class OptionsViewModel
{
#region Fields
///
/// Current set of options
///
public Options Options { get; }
///
/// Flag for if settings were saved or not
///
public bool SavedSettings { get; internal set; }
#endregion
#region Lists
///
/// List of available internal programs
///
public List> InternalPrograms => PopulateInternalPrograms();
///
/// Current list of supported system profiles
///
public List Systems => RedumpSystemComboBoxItem.GenerateElements().ToList();
#endregion
///
/// Constructor
///
public OptionsViewModel(Options baseOptions)
{
Options = new Options(baseOptions);
}
#region Population
///
/// Get a complete list of supported internal programs
///
private static List> PopulateInternalPrograms()
{
var internalPrograms = new List { InternalProgram.DiscImageCreator, InternalProgram.Aaru, InternalProgram.Redumper };
return internalPrograms.Select(ip => new Element(ip)).ToList();
}
#endregion
#region UI Commands
///
/// Browse and set a path based on the invoking button
///
internal void BrowseForPath(Window parent, System.Windows.Controls.Button button)
{
// If the button is null, we can't do anything
if (button == null)
return;
// Strips button prefix to obtain the setting name
string pathSettingName = button.Name.Substring(0, button.Name.IndexOf("Button"));
// TODO: hack for now, then we'll see
bool shouldBrowseForPath = pathSettingName == "DefaultOutputPath";
string currentPath = TextBoxForPathSetting(parent, pathSettingName)?.Text;
string initialDirectory = AppDomain.CurrentDomain.BaseDirectory;
if (!shouldBrowseForPath && !string.IsNullOrEmpty(currentPath))
initialDirectory = Path.GetDirectoryName(Path.GetFullPath(currentPath));
CommonDialog dialog = shouldBrowseForPath
? (CommonDialog)CreateFolderBrowserDialog()
: CreateOpenFileDialog(initialDirectory);
using (dialog)
{
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
string path = string.Empty;
bool exists = false;
if (shouldBrowseForPath && dialog is FolderBrowserDialog folderBrowserDialog)
{
path = folderBrowserDialog.SelectedPath;
exists = Directory.Exists(path);
}
else if (dialog is OpenFileDialog openFileDialog)
{
path = openFileDialog.FileName;
exists = File.Exists(path);
}
if (exists)
{
Options[pathSettingName] = path;
TextBoxForPathSetting(parent, pathSettingName).Text = path;
}
else
{
CustomMessageBox.Show(
"Specified path doesn't exist!",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error
);
}
}
}
}
///
/// Test Redump login credentials
///
#if NET48
public (bool?, string) TestRedumpLogin(Window parent, string username, string password)
#else
public async Task<(bool?, string)> TestRedumpLogin(Window parent, string username, string password)
#endif
{
#if NET48
return RedumpWebClient.ValidateCredentials(username, password);
#else
return await RedumpHttpClient.ValidateCredentials(username, password);
#endif
}
#endregion
#region UI Functionality
///
/// Create an open folder dialog box
///
private static FolderBrowserDialog CreateFolderBrowserDialog() => new FolderBrowserDialog();
///
/// Create an open file dialog box
///
private static OpenFileDialog CreateOpenFileDialog(string initialDirectory)
{
return new OpenFileDialog()
{
InitialDirectory = initialDirectory,
Filter = "Executables (*.exe)|*.exe",
FilterIndex = 0,
RestoreDirectory = true,
};
}
///
/// Find a TextBox by setting name
///
/// Setting name to find
/// TextBox for that setting
private System.Windows.Controls.TextBox TextBoxForPathSetting(Window parent, string name) =>
parent.FindName(name + "TextBox") as System.Windows.Controls.TextBox;
#endregion
}
}