namespace PainoBar_Helper { public partial class SettingsForm : Form { private readonly ProfileManager _profileManager; private ProfileSettings _currentSettings; private string? _currentProfileName; public SettingsForm(ProfileManager profileManager, ProfileSettings currentSettings) { InitializeComponent(); _profileManager = profileManager; _currentSettings = new ProfileSettings { PianobarPath = currentSettings.PianobarPath, EnableTextFile = currentSettings.EnableTextFile, TextFilePath = currentSettings.TextFilePath, EnableWebServer = currentSettings.EnableWebServer, WebServerPort = currentSettings.WebServerPort, EnableToastNotifications = currentSettings.EnableToastNotifications, EnableSMTC = currentSettings.EnableSMTC, OverrideCredentials = currentSettings.OverrideCredentials, ConfigFilePath = currentSettings.ConfigFilePath, PianobarUser = currentSettings.PianobarUser, PianobarPassword = currentSettings.PianobarPassword, KillProcessOnDisconnect = currentSettings.KillProcessOnDisconnect }; _currentProfileName = profileManager.ActiveProfileName; InitializeControls(); } #region Initialization private void InitializeControls() { LoadProfileList(); LoadSettingsToControls(); } private void LoadProfileList() { // Uses profiles_combo_box_selector from the Designer if (profiles_combo_box_selector == null) return; string? currentText = profiles_combo_box_selector.Text; profiles_combo_box_selector.Items.Clear(); foreach (var profile in _profileManager.Profiles) { profiles_combo_box_selector.Items.Add(profile.Name); } if (!string.IsNullOrEmpty(_currentProfileName)) profiles_combo_box_selector.SelectedItem = _currentProfileName; else if (!string.IsNullOrEmpty(currentText)) profiles_combo_box_selector.Text = currentText; } private void LoadSettingsToControls() { // Update profile name text box with current profile name if (profile_name_text_box != null && !string.IsNullOrEmpty(_currentProfileName)) profile_name_text_box.Text = _currentProfileName; // Pianobar executable path if (pianobar_path_text_box != null) pianobar_path_text_box.Text = _currentSettings.PianobarPath; // Output Settings (Tab 1) if (obs_text_output_check_box != null) obs_text_output_check_box.Checked = _currentSettings.EnableTextFile; if (output_folder_text_box != null) output_folder_text_box.Text = _currentSettings.TextFilePath; if (local_web_server_check_box != null) local_web_server_check_box.Checked = _currentSettings.EnableWebServer; if (textBox1 != null) textBox1.Text = _currentSettings.WebServerPort.ToString(); if (chk_toast_notifications != null) chk_toast_notifications.Checked = _currentSettings.EnableToastNotifications; if (chkSMTC != null) chkSMTC.Checked = _currentSettings.EnableSMTC; // Pianobar Config (Tab 2) if (chkOverrideCredentials != null) { chkOverrideCredentials.Checked = _currentSettings.OverrideCredentials; ToggleCredentialFields(chkOverrideCredentials.Checked); } if (txtConfigFilePath != null) txtConfigFilePath.Text = _currentSettings.ConfigFilePath; if (txtPianobarUser != null) txtPianobarUser.Text = _currentSettings.PianobarUser; if (txtPianobarPass != null) txtPianobarPass.Text = _currentSettings.PianobarPassword; if (chkkillpro != null) chkkillpro.Checked = _currentSettings.KillProcessOnDisconnect; } private void SaveSettingsFromControls() { // Pianobar Path if (pianobar_path_text_box != null) _currentSettings.PianobarPath = pianobar_path_text_box.Text; // Output Settings if (obs_text_output_check_box != null) _currentSettings.EnableTextFile = obs_text_output_check_box.Checked; if (output_folder_text_box != null) _currentSettings.TextFilePath = output_folder_text_box.Text; if (local_web_server_check_box != null) _currentSettings.EnableWebServer = local_web_server_check_box.Checked; if (textBox1 != null && int.TryParse(textBox1.Text, out int port)) _currentSettings.WebServerPort = port; if (chk_toast_notifications != null) _currentSettings.EnableToastNotifications = chk_toast_notifications.Checked; if (chkSMTC != null) _currentSettings.EnableSMTC = chkSMTC.Checked; // Pianobar Config if (chkOverrideCredentials != null) _currentSettings.OverrideCredentials = chkOverrideCredentials.Checked; if (txtConfigFilePath != null) _currentSettings.ConfigFilePath = txtConfigFilePath.Text; if (txtPianobarUser != null) _currentSettings.PianobarUser = txtPianobarUser.Text; if (txtPianobarPass != null) _currentSettings.PianobarPassword = txtPianobarPass.Text; if (chkkillpro != null) _currentSettings.KillProcessOnDisconnect = chkkillpro.Checked; } #endregion #region Profile Management // Wire this to profiles_combo_box_selector.SelectedIndexChanged in the Designer public void profiles_combo_box_selector_SelectedIndexChanged(object? sender, EventArgs e) { if (sender is not ComboBox cmb) return; // Update profile name text box when selection changes if (profile_name_text_box != null) profile_name_text_box.Text = cmb.Text; // If selecting an existing profile, load its settings if (cmb.SelectedItem is string profileName) { var profile = _profileManager.Profiles.FirstOrDefault(p => p.Name == profileName); if (profile != null) { _currentProfileName = profileName; _currentSettings = new ProfileSettings { PianobarPath = profile.Settings.PianobarPath, EnableTextFile = profile.Settings.EnableTextFile, TextFilePath = profile.Settings.TextFilePath, EnableWebServer = profile.Settings.EnableWebServer, WebServerPort = profile.Settings.WebServerPort, EnableToastNotifications = profile.Settings.EnableToastNotifications, EnableSMTC = profile.Settings.EnableSMTC, OverrideCredentials = profile.Settings.OverrideCredentials, ConfigFilePath = profile.Settings.ConfigFilePath, PianobarUser = profile.Settings.PianobarUser, PianobarPassword = profile.Settings.PianobarPassword, KillProcessOnDisconnect = profile.Settings.KillProcessOnDisconnect }; LoadSettingsToControls(); } } } // Wire this to profiles_combo_box_selector.TextChanged in the Designer public void profiles_combo_box_selector_TextChanged(object? sender, EventArgs e) { if (sender is not ComboBox cmb) return; // Keep profile name text box in sync with combo box text if (profile_name_text_box != null) profile_name_text_box.Text = cmb.Text; } // Wire this to new_profile_button.Click in the Designer public void new_profile_button_Click(object? sender, EventArgs e) { // Get the profile name from the combo box text string? profileName = profiles_combo_box_selector?.Text?.Trim(); if (string.IsNullOrWhiteSpace(profileName)) { MessageBox.Show("Please enter a profile name in the dropdown box.", "No Profile Name", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } // Check if profile already exists if (_profileManager.Profiles.Any(p => p.Name.Equals(profileName, StringComparison.OrdinalIgnoreCase))) { MessageBox.Show($"Profile '{profileName}' already exists. Please choose a different name.", "Profile Exists", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } try { // Create new profile with current settings SaveSettingsFromControls(); _profileManager.CreateProfile(profileName, _currentSettings); _currentProfileName = profileName; LoadProfileList(); if (profiles_combo_box_selector != null) profiles_combo_box_selector.SelectedItem = profileName; if (profile_name_text_box != null) profile_name_text_box.Text = profileName; MessageBox.Show($"Profile '{profileName}' created successfully.", "Profile Created", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show($"Failed to create profile:\n{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // Wire this to delete_profile_button.Click in the Designer public void delete_profile_button_Click(object? sender, EventArgs e) { // Get the profile name from either the combo box or the profile name text box string? profileName = profiles_combo_box_selector?.Text?.Trim(); if (string.IsNullOrWhiteSpace(profileName)) { MessageBox.Show("No profile selected.", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } // Check if profile exists if (!_profileManager.Profiles.Any(p => p.Name.Equals(profileName, StringComparison.OrdinalIgnoreCase))) { MessageBox.Show($"Profile '{profileName}' does not exist.", "Profile Not Found", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var result = MessageBox.Show($"Are you sure you want to delete profile '{profileName}'?", "Confirm Deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { try { _profileManager.DeleteProfile(profileName); // Reset to active profile or clear _currentProfileName = _profileManager.ActiveProfileName; LoadProfileList(); var activeProfile = _profileManager.GetActiveProfile(); if (activeProfile != null) { _currentSettings = activeProfile.Settings; LoadSettingsToControls(); } else { // No profiles left, clear the form _currentSettings = new ProfileSettings(); LoadSettingsToControls(); if (profiles_combo_box_selector != null) profiles_combo_box_selector.Text = string.Empty; if (profile_name_text_box != null) profile_name_text_box.Text = string.Empty; } MessageBox.Show($"Profile '{profileName}' deleted successfully.", "Profile Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show($"Failed to delete profile:\n{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } #endregion #region File Browsing // Wire this to painobar_exe_browse_button.Click in the Designer public void painobar_exe_browse_button_Click(object? sender, EventArgs e) { using var dialog = new OpenFileDialog { Filter = "Executable Files (*.exe)|*.exe|All Files (*.*)|*.*", Title = "Select Pianobar Executable", FileName = "pianobar.exe", CheckFileExists = true }; if (dialog.ShowDialog() == DialogResult.OK) { if (pianobar_path_text_box != null) pianobar_path_text_box.Text = dialog.FileName; } } // Wire this to output_folder_browse_button.Click in the Designer public void output_folder_browse_button_Click(object? sender, EventArgs e) { using var dialog = new SaveFileDialog { Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*", Title = "Select Text File Output Path", FileName = "now_playing.txt" }; if (dialog.ShowDialog() == DialogResult.OK) { if (output_folder_text_box != null) output_folder_text_box.Text = dialog.FileName; } } // Wire this to btnBrowseConfigFile.Click in the Designer public void btnBrowseConfigFile_Click(object? sender, EventArgs e) { using var dialog = new SaveFileDialog { Filter = "Config Files (*.config)|*.config|All Files (*.*)|*.*", Title = "Select Pianobar Config File Path", FileName = "pianobar.config" }; if (dialog.ShowDialog() == DialogResult.OK) { if (txtConfigFilePath != null) txtConfigFilePath.Text = dialog.FileName; } } #endregion #region Credential Override // Wire this to chkOverrideCredentials.CheckedChanged in the Designer public void chkOverrideCredentials_CheckedChanged(object? sender, EventArgs e) { if (sender is CheckBox chk) ToggleCredentialFields(chk.Checked); } private void ToggleCredentialFields(bool enabled) { if (txtConfigFilePath != null) txtConfigFilePath.Enabled = enabled; if (btnBrowseConfigFile != null) btnBrowseConfigFile.Enabled = enabled; if (txtPianobarUser != null) txtPianobarUser.Enabled = enabled; if (txtPianobarPass != null) txtPianobarPass.Enabled = enabled; if (chkkillpro != null) chkkillpro.Enabled = enabled; } #endregion #region Save/Cancel // Wire this to btnSave.Click in the Designer (or OK button) public void btnSave_Click(object? sender, EventArgs e) { try { SaveSettingsFromControls(); // Get the profile name from the text box (or combo box as fallback) string? profileName = profile_name_text_box?.Text?.Trim(); if (string.IsNullOrEmpty(profileName)) profileName = profiles_combo_box_selector?.Text?.Trim(); if (string.IsNullOrWhiteSpace(profileName)) { MessageBox.Show("Please enter a profile name before saving.", "No Profile Name", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } // Update existing profile or create new one if (_profileManager.Profiles.Any(p => p.Name.Equals(profileName, StringComparison.OrdinalIgnoreCase))) { _profileManager.UpdateProfile(profileName, _currentSettings); } else { _profileManager.CreateProfile(profileName, _currentSettings); } // Set this profile as the active profile _profileManager.SetActiveProfile(profileName); _currentProfileName = profileName; DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { MessageBox.Show($"Failed to save settings:\n{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // Wire this to btnCancel.Click in the Designer (or Cancel button) public void btnCancel_Click(object? sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } #endregion #region Helper Methods private string? PromptForProfileName(string title, string prompt) { using var form = new Form { Text = title, Width = 400, Height = 150, StartPosition = FormStartPosition.CenterParent, FormBorderStyle = FormBorderStyle.FixedDialog, MaximizeBox = false, MinimizeBox = false }; var label = new Label { Text = prompt, Left = 10, Top = 20, Width = 360 }; var textBox = new TextBox { Left = 10, Top = 50, Width = 360 }; var btnOk = new Button { Text = "OK", Left = 210, Top = 80, DialogResult = DialogResult.OK }; var btnCancel = new Button { Text = "Cancel", Left = 290, Top = 80, DialogResult = DialogResult.Cancel }; form.Controls.AddRange(new Control[] { label, textBox, btnOk, btnCancel }); form.AcceptButton = btnOk; form.CancelButton = btnCancel; return form.ShowDialog() == DialogResult.OK ? textBox.Text : null; } #endregion } }