using System;
using System.IO;
using System.Windows;
using System.Diagnostics;
using System.Windows.Controls;

using Fiesta_Client.Panels;
using Fiesta_Client.API;

namespace Fiesta_Client
{
    public partial class MainWindow : Window
    {
        private Main MainPanel;

        public MainWindow()
        {
            InitializeComponent();

            Title = String.Format("{0} Launcher", Web.Settings()["Server-Name"]);

            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            if (File.Exists(String.Format("{0}\\Launcher\\Background.mp4", AppDomain.CurrentDomain.BaseDirectory)))
            {
                Background.Source = new Uri(String.Format("{0}\\Launcher\\Background.mp4", AppDomain.CurrentDomain.BaseDirectory));
                Background.Play();
            }

            if(File.Exists(String.Format("{0}\\Launcher\\Music.mp3", AppDomain.CurrentDomain.BaseDirectory)))
            {
                Music.Source = new Uri(String.Format("{0}\\Launcher\\Music.mp3", AppDomain.CurrentDomain.BaseDirectory));

                if (!File.Exists(String.Format("{0}\\Launcher\\Music.txt", AppDomain.CurrentDomain.BaseDirectory))) { Music.Play(); }
            }

            ShowMain(false);
        }

        public void PlayMusic() { Music.Play(); }

        public void StopMusic() { Music.Stop(); }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { }

        public void ShowMain(Boolean InstallGame, String InstallLocation = "")
        {
            Panel.Children.Clear();

            if (MainPanel == null) { MainPanel = new Main(this, InstallGame, InstallLocation); }

            DockPanel.SetDock(MainPanel, Dock.Left);

            Panel.Children.Add(MainPanel);
        }

        public void ShowBuy()
        {
            Panel.Children.Clear();

            var BuyPanel = new Buy(this);
            DockPanel.SetDock(BuyPanel, Dock.Left);

            Panel.Children.Add(BuyPanel);
        }

        public void ShowResolution()
        {
            Panel.Children.Clear();

            var ResolutionPanel = new Resolution(this);
            DockPanel.SetDock(ResolutionPanel, Dock.Left);

            Panel.Children.Add(ResolutionPanel);
        }

        private void Background_MediaEnded(object sender, RoutedEventArgs e)
        {
            Background.Position = TimeSpan.Zero;
            Background.Play();
        }

        private void Music_MediaEnded(object sender, RoutedEventArgs e)
        {
            Music.Position = TimeSpan.Zero;
            Music.Play();
        }
    }
}
