using System;
using System.Windows;

namespace StandardScene.Fass2Simulator
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            Fass2SimBootstrap.ApplyCommandLine(e.Args);

            if (HasTestArg(e.Args))
            {
                var code = Fass2SimSelfTests.Run(e.Args);
                Shutdown(code);
                return;
            }

            base.OnStartup(e);
            Exit += (_, __) => Fass2SimFileLogger.Shutdown();
            var window = new MainWindow();
            MainWindow = window;
            window.Show();
        }

        private static bool HasTestArg(string[] args)
        {
            if (args == null)
            {
                return false;
            }

            foreach (var arg in args)
            {
                if (string.Equals(arg, "--motion-test", StringComparison.OrdinalIgnoreCase)
                    || string.Equals(arg, "--action-test", StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
            }

            return false;
        }
    }
}
