using Microsoft.Win32;
using System.IO;
using System.Windows;

namespace Nodify.Calculator
{
    public partial class StartupWindow : Window
    {
        public StartupWindow()
        {
            InitializeComponent();
        }

        private void NewProject_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new NewProjectDialog();
            dialog.Owner = this;
            if (dialog.ShowDialog() != true)
                return;

            ProjectManager.CreateProject(dialog.ProjectName, dialog.ProjectDirectory);
            OpenMainWindow();
        }

        private void OpenProject_Click(object sender, RoutedEventArgs e)
        {
            var ofd = new OpenFileDialog
            {
                Title = "Open Project",
                Filter = ProjectManager.ProjectFilter,
                DefaultExt = ProjectManager.ProjectExtension
            };

            if (ofd.ShowDialog() != true)
                return;

            ProjectManager.OpenProject(ofd.FileName);
            OpenMainWindow();
        }

        private void OpenMainWindow()
        {
            var mainWindow = new MainWindow();
            mainWindow.Title = $"API Visual Executor — {ProjectManager.ProjectName}";
            Application.Current.MainWindow = mainWindow;
            mainWindow.Show();
            Close();
        }
    }
}
