using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Win32;
using System.Reflection;

namespace TwoMbit
{
    /// <summary>
    /// Interaktionslogik für Window1.xaml
    /// </summary>
    partial class SettingsWindow : Window
    {
        MainWindow parentWindow;
        Language lang;
        Config cfg;
        App app;

        public SettingsWindow(MainWindow parent, App app, Language lang, Config cfg)
        {
            InitializeComponent();
            this.app = app;
            this.lang = lang;
            this.cfg = cfg;
            parentWindow = parent;
            prepareLoadSubMenu();
            translate();
            prepareLanguageBox();
            updateBrightnessSlider();
            updateContrastSlider();
            updateGammaSlider();
            updateScanlineSlider();
            updateScanline();
            updateSmsUnit();
            updateSmsBiosUsage();
            updateNtscCable();
            updateNtscMerge();
            updateVolumeSlider();
            updateFrequencySlider();

            Input.app = app;
            Input.buttonEmpty = InputEmpty;
            Input.gridMap = InputMap;
            Input.comboDevices = InputBox;
            Input.textMap = InputGuiTxt;
            Input.comboPort1 = InputPort1Box;
            Input.comboPort2 = InputPort2Box;
            prepareGuiSelector();
        }

        void prepareGuiSelector() {
            ComboBoxItem ci;
            uint i = 0;
            foreach (KeyValuePair<Cart._System, string> ident in App.systemIdent) {
                ci = new ComboBoxItem();
                ci.Content = ident.Value;
                ci.Name = "Input" + ident.Value;
                ci.Selected += new RoutedEventHandler(selectInputGui);
                InputGuiSelector.Items.Add(ci);
                ci = new ComboBoxItem();
                ci.Content = ident.Value;
                ci.Name = "Option" + ident.Value;
                ci.Selected += new RoutedEventHandler(selectOptionGui);
                if (i == 0) ci.IsSelected = true;
                OptionGuiSelector.Items.Add(ci);
                ci = new ComboBoxItem();
                ci.Content = ident.Value;
                ci.Name = "Path" + ident.Value;
                ci.Selected += new RoutedEventHandler(selectPathGui);
                if (i == 0) ci.IsSelected = true;
                PathGuiSelector.Items.Add(ci);
                i++;
            }
        }

        void prepareLoadSubMenu() {
            if (!App.isSystemInitialized(Cart._System.SNES)) {
                snesAudioFrequencyArea.Visibility = Visibility.Collapsed;
            }
        }

        public void translate()
        {
            set_input.Content = lang.get("set_input");
            adjust_video.Content = lang.get("adjust_video");
            adjust_audio.Content = lang.get("adjust_audio");
            set_options.Content = lang.get("options");
            set_paths.Content = lang.get("set_paths");

            AudioGuiVolume.Text = lang.get("volume");
            AudioGuiVolumeDefault.Content = lang.get("default");
            smsAudioGuiFrequencyDefault.Content = lang.get("default");
            snesAudioGuiFrequencyDefault.Content = lang.get("default");

            VideoGuiBrightness.Text = lang.get("brightness");
            VideoGuiBrightnessDefault.Content = lang.get("default");
            VideoGuiContrast.Text = lang.get("contrast");
            AudioGuiContrastDefault.Content = lang.get("default");
            VideoGuiGama.Text = lang.get("gamma");
            VideoGuiGammaDefault.Content = lang.get("default");
            VideoGuiNtscMerge.Content = lang.get("ntsc_merge");
            VideoGuiNtscMerge.ToolTip = lang.get("ntsc_merge_fields");

            if (App.isSystemInitialized(Cart._System.SNES)) {
                PathGuiSnesRom.Text = lang.get("rom_path");
                PathGuiSnesRomBox.Text = cfg.get<string>("snes_path_rom") == "" ? lang.get("app_path") : cfg.get<string>("snes_path_rom");
                PathGuiSnesRomDefault.Content = lang.get("default");
                PathGuiSnesRomSelect.Content = lang.get("select");
                PathGuiSnesSram.Text = lang.get("sram_path");
                PathGuiSnesSramBox.Text = cfg.get<string>("snes_path_sram") == "" ? lang.get("app_path") : cfg.get<string>("snes_path_sram");
                PathGuiSnesSramDefault.Content = lang.get("default");
                PathGuiSnesSramSelect.Content = lang.get("select");
                PathGuiSnesSavestate.Text = lang.get("state_path");
                PathGuiSnesSavestateBox.Text = cfg.get<string>("snes_path_savestate") == "" ? lang.get("app_path") : cfg.get<string>("snes_path_savestate");
                PathGuiSnesSavestateDefault.Content = lang.get("default");
                PathGuiSnesSavestateSelect.Content = lang.get("select");
                PathGuiSnesCheat.Text = lang.get("cheat_path");
                PathGuiSnesCheatBox.Text = cfg.get<string>("snes_path_cheat") == "" ? lang.get("app_path") : cfg.get<string>("snes_path_cheat");
                PathGuiSnesCheatDefault.Content = lang.get("default");
                PathGuiSnesCheatSelect.Content = lang.get("select");
                PathGuiSnesSufami.Text = lang.get("st_bios");
                PathGuiSnesSufamiBox.Text = cfg.get<string>("snes_path_sufami_turbo_bios") == "" ? lang.get("no_bios") : cfg.get<string>("snes_path_sufami_turbo_bios");
                PathGuiSnesSufamiDefault.Content = lang.get("default");
                PathGuiSnesSufamiSelect.Content = lang.get("select");
                PathGuiSnesBsx.Text = lang.get("bsx_bios");
                PathGuiSnesBsxBox.Text = cfg.get<string>("snes_path_bsx_bios") == "" ? lang.get("no_bios") : cfg.get<string>("snes_path_bsx_bios");
                PathGuiSnesBsxDefault.Content = lang.get("default");
                PathGuiSnesBsxSelect.Content = lang.get("select");
            }
            PathGuiSmsRom.Text = lang.get("rom_path");
            PathGuiSmsRomBox.Text = cfg.get<string>("sms_path_rom") == "" ? lang.get("app_path") : cfg.get<string>("sms_path_rom");
            PathGuiSmsRomDefault.Content = lang.get("default");
            PathGuiSmsRomSelect.Content = lang.get("select");
            PathGuiSmsSram.Text = lang.get("sram_path");
            PathGuiSmsSramBox.Text = cfg.get<string>("sms_path_sram") == "" ? lang.get("app_path") : cfg.get<string>("sms_path_sram");
            PathGuiSmsSramDefault.Content = lang.get("default");
            PathGuiSmsSramSelect.Content = lang.get("select");
            PathGuiSmsSavestate.Text = lang.get("state_path");
            PathGuiSmsSavestateBox.Text = cfg.get<string>("sms_path_savestate") == "" ? lang.get("app_path") : cfg.get<string>("sms_path_savestate");
            PathGuiSmsSavestateDefault.Content = lang.get("default");
            PathGuiSmsSavestateSelect.Content = lang.get("select");
            PathGuiSmsCheat.Text = lang.get("cheat_path");
            PathGuiSmsCheatBox.Text = cfg.get<string>("sms_path_cheat") == "" ? lang.get("app_path") : cfg.get<string>("sms_path_cheat");
            PathGuiSmsCheatDefault.Content = lang.get("default");
            PathGuiSmsCheatSelect.Content = lang.get("select");
            PathGuiSmsBiosJp.Text = lang.get("sms_bios_jp");
            PathGuiSmsBiosJpBox.Text = cfg.get<string>("sms_path_jp_bios") == "" ? lang.get("no_bios") : cfg.get<string>("sms_path_jp_bios");
            PathGuiSmsBiosJpDefault.Content = lang.get("default");
            PathGuiSmsBiosJpSelect.Content = lang.get("select");
            PathGuiSmsBiosUs.Text = lang.get("sms_bios_us");
            PathGuiSmsBiosUsBox.Text = cfg.get<string>("sms_path_us_bios") == "" ? lang.get("no_bios") : cfg.get<string>("sms_path_us_bios");
            PathGuiSmsBiosUsDefault.Content = lang.get("default");
            PathGuiSmsBiosUsSelect.Content = lang.get("select");

            OptionGuiLanguage.Text = lang.get("language");
            OptionGuiSmsUnit.Text = lang.get("sms_unit");
            OptionGuiSmsUseBiosCheck.Content = lang.get("sms_use_bios");

            InputEmpty.Content = lang.get("empty");
            InputNone.Content = lang.get("none");
            InputGuiTxt.Text = lang.get("register_input");
        }

        void unpauseEmulation(object sender, EventArgs e) {
            parentWindow._StateChanged(sender, e);
        }

        void prepareLanguageBox()
        {
            OptionGuiLanguageList.Items.Clear();
            string langpath = App.APP_DIR + App.LANGUAGE_DIR;
            string[] filePaths = Directory.GetFiles(@langpath, "*.xml");
            if (filePaths.Count() == 0) return;
            string langName = "";
            ListBoxItem lbi;

            foreach(string path in filePaths) {
                langName = System.IO.Path.GetFileName(path);
                lbi = new ListBoxItem();
                if (langName == cfg.get<string>("language_file")) {
                    lbi.IsSelected = true;
                }
                langName = langName.Substring(0, langName.Length - 4);
                lbi.Content = langName;
                lbi.Style = FindResource("lbi") as Style;
                lbi.Selected += setNewLang;
                OptionGuiLanguageList.Items.Add( lbi );
            }
        }

        void setNewLang(object sender, RoutedEventArgs e)
        {
            ListBoxItem lbi = (ListBoxItem)sender;
            cfg.set<string>("language_file", lbi.Content + ".xml");
            lang.setLang(lbi.Content + ".xml");
            parentWindow.translateAll();
        }

        private void disableAllSettings()
        {
            InputGui.Visibility = Visibility.Collapsed;
            CheatsGui.Visibility = Visibility.Collapsed;
            PathGui.Visibility = Visibility.Collapsed;
            AudioGui.Visibility = Visibility.Collapsed;
            VideoGui.Visibility = Visibility.Collapsed;
            OptionGui.Visibility = Visibility.Collapsed;
        }

        public void selectView(object sender, RoutedEventArgs e)
        {
            Input.endAllPolling();
            string sender_name;
            if (sender.GetType() == typeof(MenuItem)) {
                sender_name = ((MenuItem)sender).Name;
            } else if (sender.GetType() == typeof(ListViewItem)) {
                sender_name = ((ListViewItem)sender).Name;
            } else {
                return;
            }
            disableAllSettings();

            switch (sender_name) {
                case "set_input":
                    Selector.SelectedItem = set_input;
                    InputGui.Visibility = Visibility.Visible;
                    break;
                case "set_paths":
                    Selector.SelectedItem = set_paths;
                    PathGui.Visibility = Visibility.Visible;
                    break;
                case "set_cheats":
                    Selector.SelectedItem = set_cheats;
                    CheatsGui.Visibility = Visibility.Visible;
                    break;
                case "adjust_audio":
                    Selector.SelectedItem = adjust_audio;
                    AudioGui.Visibility = Visibility.Visible;
                    break;
                case "adjust_video":
                    Selector.SelectedItem = adjust_video;
                    VideoGui.Visibility = Visibility.Visible;
                    break;
                case "set_options":
                    Selector.SelectedItem = set_options;
                    OptionGui.Visibility = Visibility.Visible;
                    break;
            }
            Selector.Focus();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Input.endAllPolling();
            parentWindow.destroySettingsWindow();
            parentWindow.ShowActivated = true;
        }

        void clearPath(object sender, RoutedEventArgs e)
        {
            Button clear = (Button)sender;
            switch(clear.Name) {
                case "PathGuiSmsRomDefault": cfg.set<string>("sms_path_rom", ""); break;
                case "PathGuiSmsSramDefault": cfg.set<string>("sms_path_sram", ""); break;
                case "PathGuiSmsSavestateDefault": cfg.set<string>("sms_path_savestate", ""); break;
                case "PathGuiSmsCheatDefault": cfg.set<string>("sms_path_cheat", ""); break;
                case "PathGuiSmsBiosJpDefault": cfg.set<string>("sms_path_jp_bios", ""); break;
                case "PathGuiSmsBiosUsDefault": cfg.set<string>("sms_path_us_bios", ""); break;
                case "PathGuiSnesRomDefault": cfg.set<string>("snes_path_rom", ""); break;
                case "PathGuiSnesSramDefault": cfg.set<string>("snes_path_sram", ""); break;
                case "PathGuiSnesSavestateDefault": cfg.set<string>("snes_path_savestate", ""); break;
                case "PathGuiSnesCheatDefault": cfg.set<string>("snes_path_cheat", ""); break;
                case "PathGuiSnesSufamiDefault": cfg.set<string>("snes_path_sufami_turbo_bios", ""); break;
                case "PathGuiSnesBsxDefault": cfg.set<string>("snes_path_bsx_bios", ""); break;
            }
            translate();
        }

        void selectPathGui(object sender, RoutedEventArgs e) {
            ComboBoxItem item = (ComboBoxItem)sender;
            string _value = item.Content.ToString();
            foreach (KeyValuePair<Cart._System, string> ident in App.systemIdent) {
                string area = "PathGui" + ident.Value;
                StackPanel sp = (StackPanel)this.FindName(area);
                sp.Visibility = Visibility.Collapsed;
                if (ident.Value == _value) {
                    sp.Visibility = Visibility.Visible;
                }
            }
        }

        void selectOptionGui(object sender, RoutedEventArgs e) {
            ComboBoxItem item = (ComboBoxItem)sender;
            string _value = item.Content.ToString();
            foreach (KeyValuePair<Cart._System, string> ident in App.systemIdent) {
                string area = "OptionGui" + ident.Value;
                StackPanel sp = (StackPanel)this.FindName(area);
                sp.Visibility = Visibility.Collapsed;
                if (ident.Value == _value) {
                    sp.Visibility = Visibility.Visible;
                }
            }
        }

        void selectInputGui(object sender, RoutedEventArgs e) {
            if (InputBox == null) return;
            ComboBoxItem item = (ComboBoxItem)sender;
            string _value = item.Content.ToString();
            foreach (KeyValuePair<Cart._System, string> ident in App.systemIdent) {
                if (ident.Value == _value) {
                    Input.getInputByIdent(ident.Key).prepare();
                    return;
                }
            }
            Input.clearUI();
        }

        void remapInput(object sender, System.Windows.Input.MouseButtonEventArgs e) {
            IInputElement element = e.MouseDevice.DirectlyOver;
            if (element != null && element is FrameworkElement) {
                if (((FrameworkElement)element).Parent is DataGridCell) {
                    var grid = sender as DataGrid;
                    if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1) {
                        Input.MAP((uint)grid.SelectedIndex);
                    }
                }
            }
        }

        void unmapDevice(object sender, RoutedEventArgs e) {
            Input.UNMAP();
        }

        void setPath(object sender, RoutedEventArgs e)
        {
            string curPath = "";
            string cfgKey = "";
            Button select = (Button)sender;
            switch(select.Name) {
                case "PathGuiSmsRomSelect":
                    cfgKey = "sms_path_rom";
                    curPath = cfg.get<string>(cfgKey);
                    break;
                case "PathGuiSmsSramSelect":
                    cfgKey = "sms_path_sram";
                    curPath = cfg.get<string>(cfgKey);
                    break;
                case "PathGuiSmsSavestateSelect":
                    cfgKey = "sms_path_savestate";
                    curPath = cfg.get<string>(cfgKey);
                    break;
                case "PathGuiSmsCheatSelect":
                    cfgKey = "sms_path_cheat";
                    curPath = cfg.get<string>(cfgKey);
                    break;
                case "PathGuiSnesRomSelect":
                    cfgKey = "snes_path_rom";
                    curPath = cfg.get<string>(cfgKey);
                    break;
                case "PathGuiSnesSramSelect":
                    cfgKey = "snes_path_sram";
                    curPath = cfg.get<string>(cfgKey);
                    break;
                case "PathGuiSnesSavestateSelect":
                    cfgKey = "snes_path_savestate";
                    curPath = cfg.get<string>(cfgKey);
                    break;
                case "PathGuiSnesCheatSelect":
                    cfgKey = "snes_path_cheat";
                    curPath = cfg.get<string>(cfgKey);
                    break;
            }

            var fbd = new System.Windows.Forms.FolderBrowserDialog();
            var result = System.Windows.Forms.DialogResult.None;

            try {
                fbd.SelectedPath = curPath == "" ? App.APP_DIR : curPath;
                result = fbd.ShowDialog();
            } catch {
                fbd.SelectedPath = curPath;
                result = fbd.ShowDialog();
            }

            if (result == System.Windows.Forms.DialogResult.OK) {
                cfg.set<string>(cfgKey, fbd.SelectedPath);
                translate();
            }
        }

        void setFile(object sender, RoutedEventArgs e)
        {
            string cfgKey = "";
            Button select = (Button)sender;

            OpenFileDialog dlg = new OpenFileDialog();
            dlg.DefaultExt = ".*";
            dlg.InitialDirectory = App.APP_DIR;
            dlg.Filter = "all (*.*) | *.*";
            Nullable<bool> result = dlg.ShowDialog();
            if (result != true) return;
            Cart testCart = new Cart(app, cfg, lang);
            switch (select.Name) {
                case "PathGuiSnesSufamiSelect":
                    cfgKey = "snes_path_sufami_turbo_bios";
                    if (!testCart.testFile(Cart._System.SNES, dlg.FileName, Header.Type.SNES_STROM_BIOS)) {
                        app.showError(lang.get("invalid_bios"));
                        return;
                    }
                    break;
                case "PathGuiSnesBsxSelect":
                    cfgKey = "snes_path_bsx_bios";
                    if (!testCart.testFile(Cart._System.SNES, dlg.FileName, Header.Type.SNES_BSXROM_BIOS)) {
                        app.showError(lang.get("invalid_bios"));
                        return;
                    }
                    break;
                case "PathGuiSmsBiosJpSelect":
                    cfgKey = "sms_path_jp_bios";
                    if (!testCart.testFile(Cart._System.SMS, dlg.FileName, Header.Type.SMS_BIOS) || (testCart.getRegion() != Header.Region.JAPAN)) {
                        app.showError(lang.get("invalid_bios"));
                        return;
                    }
                    break;
                case "PathGuiSmsBiosUsSelect":
                    cfgKey = "sms_path_us_bios";
                    if (!testCart.testFile(Cart._System.SMS, dlg.FileName, Header.Type.SMS_BIOS) || (testCart.getRegion() == Header.Region.JAPAN)) {
                        app.showError(lang.get("invalid_bios"));
                        return;
                    }
                    break;
            }
            cfg.set<string>(cfgKey, dlg.FileName);
            translate();
        }
        //options
        void setSmsUnit(object sender, RoutedEventArgs e) {
            RadioButton but = (RadioButton)sender;
            switch (but.Name) {
                case "OptionGuiSmsUnit1": cfg.set<int>("sms_unit", 0); break;
                case "OptionGuiSmsUnit2": cfg.set<int>("sms_unit", 1); break;
            }
        }

        void updateSmsUnit() {
            int val = cfg.get<int>("sms_unit");
            switch (val) {
                case 0: OptionGuiSmsUnit1.IsChecked = true; break;
                case 1: OptionGuiSmsUnit2.IsChecked = true; break;
            }
        }

        void useSmsBios(object sender, RoutedEventArgs e) {
            cfg.toggle("sms_use_bios");
            updateSmsBiosUsage();
        }

        void updateSmsBiosUsage() {
            bool state = cfg.get<bool>("sms_use_bios");
            OptionGuiSmsUseBiosCheck.IsChecked = state;
        }

        //video
        void setBrightnessSlider(object sender, RoutedEventArgs e) {
            Slider sli = (Slider)sender;
            cfg.set<int>("video_brightness", (int)sli.Value);
            updateBrightnessSlider();
        }

        void updateBrightnessSlider() {
            int val = cfg.get<int>("video_brightness");
            VideoGuiBrightnessBox.Text = Convert.ToString(val-100);
            VideoGuiBrightnessSlider.Value = val;
            app.updateSwFilter();
        }

        void setBrightnessDefault(object sender, RoutedEventArgs e) {
            cfg.init("video_brightness");
            updateBrightnessSlider();
        }

        void setContrastSlider(object sender, RoutedEventArgs e) {
            Slider sli = (Slider)sender;
            cfg.set<int>("video_contrast", (int)sli.Value);
            updateContrastSlider();
        }

        void updateContrastSlider() {
            int val = cfg.get<int>("video_contrast");
            VideoGuiContrastBox.Text = Convert.ToString(val);
            VideoGuiContrastSlider.Value = val;
            app.updateSwFilter();
        }

        void setContrastDefault(object sender, RoutedEventArgs e) {
            cfg.init("video_contrast");
            updateContrastSlider();
        }

        void setGammaSlider(object sender, RoutedEventArgs e) {
            Slider sli = (Slider)sender;
            cfg.set<int>("video_gamma", (int)sli.Value + 50);
            updateGammaSlider();
        }

        void updateGammaSlider() {
            int val = cfg.get<int>("video_gamma");
            VideoGuiGammaBox.Text = Convert.ToString((double)val / 100);
            VideoGuiGammaSlider.Value = val - 50;
            app.updateSwFilter();
        }

        void setGammaDefault(object sender, RoutedEventArgs e) {
            cfg.init("video_gamma");
            updateGammaSlider();
        }

        void setScanlineSlider(object sender, RoutedEventArgs e) {
            Slider sli = (Slider)sender;
            cfg.set<int>("video_scanline_intensity", (int)sli.Value);
            updateScanlineSlider();
        }

        void updateScanlineSlider() {
            int val = cfg.get<int>("video_scanline_intensity");
            VideoGuiScanlinesBox.Text = Convert.ToString(val) + " %";
            VideoGuiScanlinesSlider.Value = val;
            app.updateScanline();
        }

        void enableScanline(object sender, RoutedEventArgs e) {
            CheckBox chk = (CheckBox)sender;
            cfg.set<bool>("video_scanlines", (bool)chk.IsChecked );
            updateScanline();
        }

        void updateScanline() {
            bool chk = cfg.get<bool>("video_scanlines");
            VideoGuiScanlinesSlider.IsEnabled = chk;
            VideoGuiScanlinesActive.IsChecked = chk;
            app.updateScanline();
        }

        void setNtscCable(object sender, RoutedEventArgs e) {
            RadioButton but = (RadioButton)sender;
            switch (but.Name) {
                case "VideoGuiNtscAv": cfg.set<int>("ntsc_filter", 0); break;
                case "VideoGuiNtscSvideo": cfg.set<int>("ntsc_filter", 1); break;
                case "VideoGuiNtscRgb": cfg.set<int>("ntsc_filter", 2); break;
            }
            updateNtscCable();
        }

        void updateNtscCable() {
            int val = cfg.get<int>("ntsc_filter");
            switch (val) {
                case 0: VideoGuiNtscAv.IsChecked = true; break;
                case 1: VideoGuiNtscSvideo.IsChecked = true; break;
                case 2: VideoGuiNtscRgb.IsChecked = true; break;
            }
            app.updateSwFilter();
        }

        void setNtscMerge(object sender, RoutedEventArgs e) {
            cfg.toggle("ntsc_filter_merge");
            updateNtscMerge();
        }

        void updateNtscMerge() {
            bool state = cfg.get<bool>("ntsc_filter_merge");
            VideoGuiNtscMerge.IsChecked = state;
            app.updateSwFilter();
        }
        //audio
        void setVolumeSlider(object sender, RoutedEventArgs e) {
            Slider sli = (Slider)sender;
            cfg.set<int>("audio_volume", (int)sli.Value);
            updateVolumeSlider();
        }

        void updateVolumeSlider() {
            int val = cfg.get<int>("audio_volume");
            AudioGuiVolumeBox.Text = Convert.ToString(val) + " %";
            AudioGuiVolumeSlider.Value = val;
            app.updateAudio();
        }

        void setVolumeDefault(object sender, RoutedEventArgs e) {
            cfg.init("audio_volume");
            updateVolumeSlider();
        }

        void setFrequencySlider(object sender, RoutedEventArgs e) {
            Slider sli = (Slider)sender;
            foreach (KeyValuePair<Cart._System, string> pre in App.systemIdent) {
                if (pre.Value + "AudioGuiFrequencySlider" == sli.Name) {
                    cfg.set<int>(pre.Value + "_audio_frequency", (int)sli.Value);
                    break;
                }
            }
            updateFrequencySlider();
        }

        void updateFrequencySlider() {
            int val = cfg.get<int>("sms_audio_frequency");
            smsAudioGuiFrequencyBox.Text = Convert.ToString(val-300) + " Hz";
            smsAudioGuiFrequencySlider.Value = val;
            if (App.isSystemInitialized(Cart._System.SNES)) {
                val = cfg.get<int>("snes_audio_frequency");
                snesAudioGuiFrequencyBox.Text = Convert.ToString(val - 300) + " Hz";
                snesAudioGuiFrequencySlider.Value = val;
            }
            app.updateAudio();
        }

        void setFrequencyDefault(object sender, RoutedEventArgs e) {
            Button but = (Button)sender;

            foreach (KeyValuePair<Cart._System, string> pre in App.systemIdent) {
                if (pre.Value + "AudioGuiFrequencyDefault" == but.Name) {
                    cfg.init(pre.Value + "_audio_frequency");
                    break;
                }
            }
            updateFrequencySlider();
        }
    }
}
