using Register_office.Properties;
using Register_office.View.LoginV;
using Register_office.View.MenuV;
using Register_office.View.RegistrationV;
using Register_office.ViewModel;

namespace Register_office
{
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // To customize application configuration such as set high DPI settings or default font,
            // see https://aka.ms/applicationconfiguration.
            ApplicationConfiguration.Initialize();
			if (!Init()) return;
			Application.Run(new LoginForm());
		}

		private static bool Init()
		{
			GeneralViewModel.Instance.Initialize();
			while (!RegisterOfficeFactory.CanConnect)
			{
				MessageBox.Show("Ошибка подключения к базе данных! " +
					"Обратитесь к администратору базы данных за помощью настройки подключения",
					"Подключение к базе данных", MessageBoxButtons.OK, MessageBoxIcon.Error);

				ConnectionParametersForm form = new();

				{
					form.tbHost.Text = Settings.Default.Host;
					form.tbDataBase.Text = Settings.Default.Database;
					form.tbUsername.Text = Settings.Default.Username;
					form.tbPassword.Text = Settings.Default.Password;
				}


				if (form.ShowDialog() == DialogResult.OK)
				{
					Settings.Default.Host = form.tbHost.Text;
					Settings.Default.Database = form.tbDataBase.Text;
					Settings.Default.Username = form.tbUsername.Text;
					Settings.Default.Password = form.tbPassword.Text;
					Settings.Default.Save();
				}
				else return false;
				GeneralViewModel.Instance.Initialize();
			}
			return true;
		}
	}
}