using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using BeWo.Core.Config;
using BeWo.ServiceProxy;


namespace BeWo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ShowLoginControl();
        }

        private void ShowLoginControl()
        {
			
			//WebRequest.DefaultWebProxy 
				
			//System.Net.WebRequest.GetSystemWebProxy()
	        
			

            var lc = new LoginControl();
            rootGrid.Children.Add(lc);
            lc.LoginSuccessful += lc_LoginSuccessful;
        }

        void lc_LoginSuccessful(object sender, EventArgs e)
        {
#if DEBUG
			//if (BeWoApp.Tenant == "demo")
			//{
			//    var dlg = new ConfirmDemoDialog();
			//    dlg.ShowDialog();
			//}
#else
			if (BeWoApp.Tenant == "demo")
			{
				var dlg = new ConfirmDemoDialog();
				dlg.ShowDialog();
			}
#endif

            rootGrid.Children.Clear();

			MainControl mc = new MainControl();
	        mc.IsMaximized = BeWoApp.AppSettings.WindowState == AppSettings.WindowStateType.Maximised;
            
            mc.LogoutClicked += new EventHandler(mc_LogoutClicked);
            rootGrid.Children.Add(mc);
        }

        void mc_LogoutClicked(object sender, EventArgs e)
        {
            rootGrid.Children.Clear();
            BeWoApp.MainControl = null;
            ShowLoginControl();
        }

        private void MainWindow_OnClosing(object sender, CancelEventArgs e)
        {
            if (BeWoApp.MainControl != null)
            {
				//BeWoApp.SaveAppSettings();

	            if (!BeWoApp.MainControl.DoSaveCheck())
	            {
		            e.Cancel = true;
		            return;
	            }
            }

            BeWoApp.CurrentBeWo.DocumentWatcher.LoescheTempDateien();

            Application.Current.Shutdown();
        }
    }
}
