namespace HistoryView.Views;

public partial class MainWindow : Window
{
    internal MainWindow(IEventAggregator eventAggregator, IRegionManager regionManager, MonitorHelper monitorHelper)
    {
        InitializeComponent();
        this._eventAggregator = eventAggregator;
        this._eventAggregator.GetEvent<WindowSwitch>().Subscribe(SwitchWindow);
        this.Loaded += MainWindow_Loaded;
        this._regionManager = regionManager;
        this._monitorHelper = monitorHelper;
        //MonitorHelper.LoadScreensInfo();
    }

    private readonly IRegionManager _regionManager;
    private readonly IEventAggregator _eventAggregator;
    private readonly MonitorHelper _monitorHelper;
    public static string DefaultRegion = "Monitor";

    private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        //this.DragMove();
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        this.PagesLoading ??= new Loading();
        this.Content = PagesLoading;
    }

    private object? PagesMainWindow;
    private object? PagesLoading;
    private object? PagesLogin;
    private void SwitchWindow()
    {
        Application.Current.Dispatcher.Invoke(() =>
        {
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            switch (this._eventAggregator.GetEvent<WindowSwitch>().Page)
            {
                case Pages.Loading:
                    double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
                    double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
                    double windowWidth = this.Width;
                    double windowHeight = this.Height;
                    this.Left = (screenWidth / 2) - (windowWidth / 2);
                    this.Top = (screenHeight / 2) - (windowHeight / 2);
                    break;

                case Pages.Login:
                case Pages.MainWindow:
                    this.SizeToContent = SizeToContent.Manual;
                    this.WindowState = System.Windows.WindowState.Normal;
                    this.WindowStyle = System.Windows.WindowStyle.None;
                    this.ResizeMode = System.Windows.ResizeMode.NoResize;
                    this.Topmost = false;

                    if (!_monitorHelper.Monitors.TryGetValue(_monitorHelper.SelectedDisplay, out MonitorInfo? monitor) || monitor is null)
                    {
                        this.Left = 0;
                        this.Top = 0;
                        this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
                        this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;

                    }
                    else
                    {
                        this.Left = monitor.StartPixHorizontal;
                        this.Top = monitor.StartPixVertical;
                        this.Width = monitor.ResolutionHorizontal;
                        this.Height = monitor.ResolutionVertical;
                    }
                    break;
                default:
                    break;
            }


            switch (this._eventAggregator.GetEvent<WindowSwitch>().Page)
            {
                case Pages.Loading:
                    this.PagesLoading ??= new Loading();
                    this.Content = PagesLoading;
                    break;
                case Pages.Login:
                    this.PagesLogin ??= new Login();
                    this.Content = PagesLogin;
                    break;
                case Pages.MainWindow:
                    this.PagesMainWindow ??= new Status();
                    this.Content = PagesMainWindow;
                    this._regionManager.Regions["ModuleContent"].RequestNavigate("GasPanel");
                    this._regionManager.Regions["ModuleContent"].RequestNavigate(DefaultRegion);
                    break;
                default:
                    break;
            }

        });
    }

    private void Window_Closed(object sender, EventArgs e)
    {

    }
}
