using System;
using System.Collections.Generic;
using System.Linq;
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 Microsoft.Win32;

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

        public enum Type { SUFAMI_TURBO, BSX, BSX_SLOTTED };

        public LoaderWindow(MainWindow parentWindow, App app, Language lang, Config cfg)
        {
            InitializeComponent();
            this.parentWindow = parentWindow;
            this.cfg = cfg;
            this.lang = lang;
            this.app = app;
            translate();
        }

        public void translate()
        {
            SlotASelect.Content = lang.get("select");
            SlotBSelect.Content = lang.get("select");
            OKButton.Content = lang.get("ok");
            CancelButton.Content = lang.get("cancel");
        }

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

        public void prepare(string fn, Type type)
        {
            SlotABox.Text = fn;
            SlotBBox.Text = "";
            this.type = type;

            switch(type) {
                case Type.SUFAMI_TURBO:
                    Window.Title = "Sufami Turbo Cart Loader";
                    SlotBTxt.Text = "Slot B";
                    break;
                case Type.BSX:
                    Window.Title = "BSX Cart Loader";
                    SlotBTxt.Visibility = Visibility.Hidden;
                    SlotBBox.Visibility = Visibility.Hidden;
                    SlotBSelect.Visibility = Visibility.Hidden;
                    break;
                case Type.BSX_SLOTTED:
                    Window.Title = "BSX Slotted Cart Loader";
                    SlotBTxt.Visibility = Visibility.Hidden;
                    SlotBBox.Visibility = Visibility.Hidden;
                    SlotBSelect.Visibility = Visibility.Hidden;
                    break;
            }
        }

        void load(object sender, RoutedEventArgs e)
        {
            app.loadFromLoaderGui(SlotABox.Text, SlotBBox.Text, this.type);
        }

        void cancel(object sender, RoutedEventArgs e)
        {
            parentWindow.closeLoaderGui();
        }

        void select(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.DefaultExt = ".smc";

            dlg.InitialDirectory = cfg.get<string>("snes_path_rom");
            dlg.Filter = "all (*.*) | *.*"
             + "|SNES (.smc .sfc .swc .fig .bs .st .rar .zip .7z .gz .bz2 .arj .tar) | *.smc; *.sfc; *.swc; *.fig; *.bs; *.st; *.rar; *.zip; *.7z; *.gz; *.bz2; *.arj; *.tar";

            Nullable<bool> result = false;
            try {
                result = dlg.ShowDialog();
            } catch {
                dlg.InitialDirectory = App.APP_DIR;
                result = dlg.ShowDialog();
            }

            if (result == true) {
                string senderName = ((Button)sender).Name;
                switch (senderName) {
                    case "SlotASelect":
                        SlotABox.Text = dlg.FileName;
                        break;
                    case "SlotBSelect":
                        SlotBBox.Text = dlg.FileName;
                        break;
                }
            }
        }
    }
}
