using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using MPF.Core.UI.ViewModels;
using MPF.UI.Core.UserControls;
using SabreTools.RedumpLib;
using SabreTools.RedumpLib.Data;
using WPFCustomMessageBox;
using WinForms = System.Windows.Forms;
#pragma warning disable IDE1006 // Naming Styles
namespace MPF.UI.Core.Windows
{
public partial class MainWindow : WindowBase
{
///
/// Read-only access to the current main view model
///
public MainViewModel MainViewModel => DataContext as MainViewModel ?? new MainViewModel();
#if NET35
#region Top Menu Bar
private MenuItem? _AboutMenuItem => ItemHelper.FindChild(this, "AboutMenuItem");
private MenuItem? _AppExitMenuItem => ItemHelper.FindChild(this, "AppExitMenuItem");
private MenuItem? _CheckForUpdatesMenuItem => ItemHelper.FindChild(this, "CheckForUpdatesMenuItem");
private MenuItem? _DebugViewMenuItem => ItemHelper.FindChild(this, "DebugViewMenuItem");
private MenuItem? _CheckDumpMenuItem => ItemHelper.FindChild(this, "CheckDumpMenuItem");
private MenuItem? _CreateIRDMenuItem => ItemHelper.FindChild(this, "CreateIRDMenuItem");
private MenuItem? _OptionsMenuItem => ItemHelper.FindChild(this, "OptionsMenuItem");
#endregion
#region Settings
private ComboBox? _DriveLetterComboBox => ItemHelper.FindChild(this, "DriveLetterComboBox");
private ComboBox? _DriveSpeedComboBox => ItemHelper.FindChild(this, "DriveSpeedComboBox");
private ComboBox? _DumpingProgramComboBox => ItemHelper.FindChild(this, "DumpingProgramComboBox");
private CheckBox? _EnableParametersCheckBox => ItemHelper.FindChild(this, "EnableParametersCheckBox");
private ComboBox? _MediaTypeComboBox => ItemHelper.FindChild(this, "MediaTypeComboBox");
private Button? _OutputPathBrowseButton => ItemHelper.FindChild(this, "OutputPathBrowseButton");
private TextBox? _OutputPathTextBox => ItemHelper.FindChild(this, "OutputPathTextBox");
private ComboBox? _SystemTypeComboBox => ItemHelper.FindChild(this, "SystemTypeComboBox");
#endregion
#region Controls
private Button? _CopyProtectScanButton => ItemHelper.FindChild(this, "CopyProtectScanButton");
private Button? _MediaScanButton => ItemHelper.FindChild(this, "MediaScanButton");
private Button? _StartStopButton => ItemHelper.FindChild(this, "StartStopButton");
private Button? _UpdateVolumeLabel => ItemHelper.FindChild(this, "UpdateVolumeLabel");
#endregion
#region Status
private LogOutput? _LogOutput => ItemHelper.FindChild(this, "LogOutput");
#endregion
#endif
///
/// Constructor
///
public MainWindow()
{
#if NET40_OR_GREATER || NETCOREAPP
InitializeComponent();
#endif
#if NET452_OR_GREATER || NETCOREAPP
var chrome = new System.Windows.Shell.WindowChrome
{
CaptionHeight = 0,
ResizeBorderThickness = new Thickness(0),
};
System.Windows.Shell.WindowChrome.SetWindowChrome(this, chrome);
#endif
}
///
/// Handler for MainWindow OnContentRendered event
///
protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);
// Disable buttons until we load fully
MainViewModel.StartStopButtonEnabled = false;
MainViewModel.MediaScanButtonEnabled = false;
MainViewModel.UpdateVolumeLabelEnabled = false;
MainViewModel.CopyProtectScanButtonEnabled = false;
// Add the click handlers to the UI
AddEventHandlers();
// Display the debug option in the menu, if necessary
if (MainViewModel.Options.ShowDebugViewMenuItem)
#if NET35
_DebugViewMenuItem!.Visibility = Visibility.Visible;
#else
DebugViewMenuItem.Visibility = Visibility.Visible;
#endif
#if NET35
MainViewModel.Init(_LogOutput!.EnqueueLog, DisplayUserMessage, ShowDiscInformationWindow);
#else
MainViewModel.Init(LogOutput.EnqueueLog, DisplayUserMessage, ShowDiscInformationWindow);
#endif
// Set the UI color scheme according to the options
ApplyTheme();
// Check for updates, if necessary
if (MainViewModel.Options.CheckForUpdatesOnStartup)
CheckForUpdates(showIfSame: false);
// Handle first-run, if necessary
if (MainViewModel.Options.FirstRun)
{
// Show the options window
ShowOptionsWindow("Welcome to MPF, Explore the Options");
}
}
#region UI Functionality
///
/// Add all event handlers
///
public void AddEventHandlers()
{
// Menu Bar Click
#if NET35
_AboutMenuItem!.Click += AboutClick;
_AppExitMenuItem!.Click += AppExitClick;
_CheckForUpdatesMenuItem!.Click += CheckForUpdatesClick;
_DebugViewMenuItem!.Click += DebugViewClick;
_CheckDumpMenuItem!.Click += CheckDumpMenuItemClick;
_CreateIRDMenuItem!.Click += CreateIRDMenuItemClick;
_OptionsMenuItem!.Click += OptionsMenuItemClick;
#else
AboutMenuItem.Click += AboutClick;
AppExitMenuItem.Click += AppExitClick;
CheckForUpdatesMenuItem.Click += CheckForUpdatesClick;
DebugViewMenuItem.Click += DebugViewClick;
CheckDumpMenuItem.Click += CheckDumpMenuItemClick;
CreateIRDMenuItem.Click += CreateIRDMenuItemClick;
OptionsMenuItem.Click += OptionsMenuItemClick;
#endif
// User Area Click
#if NET35
_CopyProtectScanButton!.Click += CopyProtectScanButtonClick;
_EnableParametersCheckBox!.Click += EnableParametersCheckBoxClick;
_MediaScanButton!.Click += MediaScanButtonClick;
_UpdateVolumeLabel!.Click += UpdateVolumeLabelClick;
_OutputPathBrowseButton!.Click += OutputPathBrowseButtonClick;
_StartStopButton!.Click += StartStopButtonClick;
#else
CopyProtectScanButton.Click += CopyProtectScanButtonClick;
EnableParametersCheckBox.Click += EnableParametersCheckBoxClick;
MediaScanButton.Click += MediaScanButtonClick;
UpdateVolumeLabel.Click += UpdateVolumeLabelClick;
OutputPathBrowseButton.Click += OutputPathBrowseButtonClick;
StartStopButton.Click += StartStopButtonClick;
#endif
// User Area SelectionChanged
#if NET35
_SystemTypeComboBox!.SelectionChanged += SystemTypeComboBoxSelectionChanged;
_MediaTypeComboBox!.SelectionChanged += MediaTypeComboBoxSelectionChanged;
_DriveLetterComboBox!.SelectionChanged += DriveLetterComboBoxSelectionChanged;
_DriveSpeedComboBox!.SelectionChanged += DriveSpeedComboBoxSelectionChanged;
_DumpingProgramComboBox!.SelectionChanged += DumpingProgramComboBoxSelectionChanged;
#else
SystemTypeComboBox.SelectionChanged += SystemTypeComboBoxSelectionChanged;
MediaTypeComboBox.SelectionChanged += MediaTypeComboBoxSelectionChanged;
DriveLetterComboBox.SelectionChanged += DriveLetterComboBoxSelectionChanged;
DriveSpeedComboBox.SelectionChanged += DriveSpeedComboBoxSelectionChanged;
DumpingProgramComboBox.SelectionChanged += DumpingProgramComboBoxSelectionChanged;
#endif
// User Area TextChanged
#if NET35
_OutputPathTextBox!.TextChanged += OutputPathTextBoxTextChanged;
#else
OutputPathTextBox.TextChanged += OutputPathTextBoxTextChanged;
#endif
}
///
/// Browse for an output file path
///
public void BrowseFile()
{
// Get the current path, if possible
string currentPath = MainViewModel.OutputPath;
if (string.IsNullOrEmpty(currentPath) && !string.IsNullOrEmpty(MainViewModel.Options.DefaultOutputPath))
currentPath = Path.Combine(MainViewModel.Options.DefaultOutputPath, "track.bin");
else if (string.IsNullOrEmpty(currentPath))
currentPath = "track.bin";
if (string.IsNullOrEmpty(currentPath))
currentPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory!, "track.bin");
// Get the full path
currentPath = Path.GetFullPath(currentPath);
// Get the directory
var directory = Path.GetDirectoryName(currentPath);
// Get the filename
string filename = Path.GetFileName(currentPath);
WinForms.FileDialog fileDialog = new WinForms.SaveFileDialog
{
FileName = filename,
InitialDirectory = directory,
};
WinForms.DialogResult result = fileDialog.ShowDialog();
if (result == WinForms.DialogResult.OK)
{
MainViewModel.OutputPath = fileDialog.FileName;
}
}
///
/// Check for available updates
///
/// True to show the box even if it's the same, false to only show if it's different
public void CheckForUpdates(bool showIfSame)
{
(bool different, string message, var url) = MainViewModel.CheckForUpdates();
// If we have a new version, put it in the clipboard
if (different && !string.IsNullOrEmpty(url))
Clipboard.SetText(url);
if (showIfSame || different)
CustomMessageBox.Show(this, message, "Version Update Check", MessageBoxButton.OK, different ? MessageBoxImage.Exclamation : MessageBoxImage.Information);
}
///
/// Display a user message using a CustomMessageBox
///
/// Title to display to the user
/// Message to display to the user
/// Number of options to display
/// true for inquiry, false otherwise
/// true for positive, false for negative, null for neutral
public bool? DisplayUserMessage(string title, string message, int optionCount, bool flag)
{
// Set the correct button style
var button = optionCount switch
{
1 => MessageBoxButton.OK,
2 => MessageBoxButton.YesNo,
3 => MessageBoxButton.YesNoCancel,
// This should not happen, but default to "OK"
_ => MessageBoxButton.OK,
};
// Set the correct icon
MessageBoxImage image = flag ? MessageBoxImage.Question : MessageBoxImage.Exclamation;
// Display and get the result
MessageBoxResult result = CustomMessageBox.Show(this, message, title, button, image);
return result switch
{
MessageBoxResult.OK or MessageBoxResult.Yes => true,
MessageBoxResult.No => false,
_ => null,
};
}
///
/// Build a dummy SubmissionInfo and display it for testing
///
public void ShowDebugDiscInfoWindow()
{
var submissionInfo = MainViewModel.CreateDebugSubmissionInfo();
var result = ShowDiscInformationWindow(submissionInfo);
Formatter.ProcessSpecialFields(result.Item2);
}
///
/// Show the disc information window
///
/// SubmissionInfo object to display and possibly change
/// Dialog open result
public (bool?, SubmissionInfo?) ShowDiscInformationWindow(SubmissionInfo? submissionInfo)
{
if (MainViewModel.Options.ShowDiscEjectReminder)
CustomMessageBox.Show(this, "It is now safe to eject the disc", "Eject", MessageBoxButton.OK, MessageBoxImage.Information);
var discInformationWindow = new DiscInformationWindow(MainViewModel.Options, submissionInfo)
{
Focusable = true,
Owner = this,
ShowActivated = true,
ShowInTaskbar = true,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
};
discInformationWindow.Closed += delegate { this.Activate(); };
bool? result = discInformationWindow.ShowDialog();
// Copy back the submission info changes, if necessary
if (result == true)
submissionInfo = (discInformationWindow.DiscInformationViewModel.SubmissionInfo.Clone() as SubmissionInfo)!;
return (result, submissionInfo!);
}
///
/// Show the Check Dump window
///
public void ShowCheckDumpWindow()
{
// Hide MainWindow while Check GUI is open
this.Hide();
var checkDumpWindow = new CheckDumpWindow(this)
{
Focusable = true,
Owner = this,
ShowActivated = true,
ShowInTaskbar = true,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
};
checkDumpWindow.Closed += delegate {
// Unhide Main window after Check window has been closed
this.Show();
this.Activate();
};
checkDumpWindow.Show();
}
///
/// Show the Create IRD window
///
public void ShowCreateIRDWindow()
{
// Hide MainWindow while Create IRD UI is open
this.Hide();
var createIRDWindow = new CreateIRDWindow(this)
{
Focusable = true,
Owner = this,
ShowActivated = true,
ShowInTaskbar = true,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
};
createIRDWindow.Closed += delegate {
// Unhide Main window after Create IRD window has been closed
this.Show();
this.Activate();
};
createIRDWindow.Show();
}
///
/// Show the Options window
///
public void ShowOptionsWindow(string? title = null)
{
var optionsWindow = new OptionsWindow(MainViewModel.Options)
{
Focusable = true,
Owner = this,
ShowActivated = true,
ShowInTaskbar = true,
Title = title ?? "Options",
WindowStartupLocation = WindowStartupLocation.CenterOwner,
};
optionsWindow.Closed += delegate { this.Activate(); };
optionsWindow.Closed += OnOptionsUpdated;
optionsWindow.Show();
}
///
/// Set the UI color scheme according to the options
///
private void ApplyTheme()
{
Theme theme;
if (MainViewModel.Options.EnableDarkMode)
theme = new DarkModeTheme();
else
theme = new LightModeTheme();
theme.Apply();
}
#endregion
#region Event Handlers
///
/// Handler for OptionsWindow OnUpdated event
///
public void OnOptionsUpdated(object? sender, EventArgs e)
{
// Get the options window
var optionsWindow = (sender as OptionsWindow);
if (optionsWindow?.OptionsViewModel == null)
return;
bool savedSettings = optionsWindow.OptionsViewModel.SavedSettings;
var options = optionsWindow.OptionsViewModel.Options;
MainViewModel.UpdateOptions(savedSettings, options);
// Set the UI color scheme according to the options
ApplyTheme();
}
#region Menu Bar
///
/// Handler for AboutMenuItem Click event
///
public void AboutClick(object sender, RoutedEventArgs e)
{
string aboutText = MainViewModel.CreateAboutText();
CustomMessageBox.Show(this, aboutText, "About", MessageBoxButton.OK, MessageBoxImage.Information);
}
///
/// Handler for AppExitMenuItem Click event
///
public void AppExitClick(object sender, RoutedEventArgs e) =>
Application.Current.Shutdown();
///
/// Handler for CheckDumpMenuItem Click event
///
public void CheckDumpMenuItemClick(object sender, RoutedEventArgs e) =>
ShowCheckDumpWindow();
///
/// Handler for CreateIRDMenuItem Click event
///
public void CreateIRDMenuItemClick(object sender, RoutedEventArgs e) =>
ShowCreateIRDWindow();
///
/// Handler for CheckForUpdatesMenuItem Click event
///
public void CheckForUpdatesClick(object sender, RoutedEventArgs e)
=> CheckForUpdates(showIfSame: true);
///
/// Handler for DebugViewMenuItem Click event
///
public void DebugViewClick(object sender, RoutedEventArgs e) =>
ShowDebugDiscInfoWindow();
///
/// Handler for OptionsMenuItem Click event
///
public void OptionsMenuItemClick(object sender, RoutedEventArgs e) =>
ShowOptionsWindow();
#endregion
#region User Area
///
/// Handler for CopyProtectScanButton Click event
///
#if NET40
public void CopyProtectScanButtonClick(object sender, RoutedEventArgs e)
#else
public async void CopyProtectScanButtonClick(object sender, RoutedEventArgs e)
#endif
{
#if NET40
var (output, error) = MainViewModel.ScanAndShowProtection();
#else
var (output, error) = await MainViewModel.ScanAndShowProtection();
#endif
if (!MainViewModel.LogPanelExpanded)
{
if (!string.IsNullOrEmpty(output) && string.IsNullOrEmpty(error))
CustomMessageBox.Show(this, output, "Detected Protection(s)", MessageBoxButton.OK, MessageBoxImage.Information);
else
CustomMessageBox.Show(this, "An exception occurred, see the log for details", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
///
/// Handler for DriveLetterComboBox SelectionChanged event
///
public void DriveLetterComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
MainViewModel.InitializeUIValues(removeEventHandlers: true, rescanDrives: false);
}
///
/// Handler for DriveSpeedComboBox SelectionChanged event
///
public void DriveSpeedComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
MainViewModel.EnsureDiscInformation();
}
///
/// Handler for DumpingProgramComboBox SelectionChanged event
///
public void DumpingProgramComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
MainViewModel.ChangeDumpingProgram();
}
///
/// Handler for EnableParametersCheckBox Click event
///
public void EnableParametersCheckBoxClick(object sender, RoutedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
MainViewModel.ToggleParameters();
}
///
/// Handler for MediaScanButton Click event
///
public void MediaScanButtonClick(object sender, RoutedEventArgs e) =>
MainViewModel.InitializeUIValues(removeEventHandlers: true, rescanDrives: true);
///
/// Handler for MediaTypeComboBox SelectionChanged event
///
public void MediaTypeComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
MainViewModel.ChangeMediaType(e.RemovedItems, e.AddedItems);
}
///
/// Handler for OutputPathBrowseButton Click event
///
public void OutputPathBrowseButtonClick(object sender, RoutedEventArgs e)
{
BrowseFile();
MainViewModel.EnsureDiscInformation();
}
///
/// Handler for OutputPathTextBox TextChanged event
///
public void OutputPathTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
MainViewModel.EnsureDiscInformation();
}
///
/// Handler for StartStopButton Click event
///
public void StartStopButtonClick(object sender, RoutedEventArgs e) =>
MainViewModel.ToggleStartStop();
///
/// Handler for SystemTypeComboBox SelectionChanged event
///
public void SystemTypeComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
MainViewModel.ChangeSystem();
}
///
/// Handler for UpdateVolumeLabel Click event
///
public void UpdateVolumeLabelClick(object sender, RoutedEventArgs e)
{
if (MainViewModel.CanExecuteSelectionChanged)
{
if (MainViewModel.Options.FastUpdateLabel)
MainViewModel.FastUpdateLabel(removeEventHandlers: true);
else
MainViewModel.InitializeUIValues(removeEventHandlers: true, rescanDrives: false);
}
}
#endregion
#endregion // Event Handlers
}
}