using System;
using System.Windows;
using Valg.EVA.Skanning.UserInterfaceElements.MainWindowProvider;

namespace Valg.EVA.Skanning.UserInterfaceElements.LightBox
{
    public class LightBoxViewModel : ViewModelBase, ILightBoxViewModel
    {
        private readonly IMainWindowProvider mainWindowProvider;
        private bool showLightBox;

        public LightBoxViewModel(IMainWindowProvider mainWindowProvider)
        {
            this.mainWindowProvider = mainWindowProvider;
        }

        protected override void SubscribeToViewModelSpecificEvents()
        {
            SubscribeToEvent<ShowLightBoxEvent, LightBoxEventArgs>(ShowLightBox);
        }

        private void ShowLightBox(LightBoxEventArgs e)
        {
            var window = CreateWindow(e);

            if (window != null)
            {
                window.EnsureArrowCursorForWindow();

                ShowWindow(window, e.Blocking);
            }
            else
            {
                ToogleVisibility(e);
            }
        }

        
        
        
        private void ShowNonBlockingModal(Window window)
        {
            var parent = window.Owner;
            var initialSize = parent.RenderSize;
            var initialLocation = parent.PointToScreen(new Point(0, 0));

            EventHandler forceActivateOfModalWindow = (s, e) => window.Activate();
            parent.Activated += forceActivateOfModalWindow;
            parent.IsEnabled = false;

            EventHandler locationChangedEventHandler = (s, e) =>
            {
                parent.Left = initialLocation.X;
                parent.Top = initialLocation.Y;
            };
            parent.LocationChanged += locationChangedEventHandler;

            SizeChangedEventHandler keepSizeHandler = (s, e) =>
            {
                e.Handled = true;
                parent.Height = initialSize.Height;
                parent.Width = initialSize.Width;
            };

            parent.SizeChanged += keepSizeHandler;

            window.Closed += (EventHandler)((s, e) =>
            {
                parent.IsEnabled = true;
                parent.Activated -= forceActivateOfModalWindow;
                parent.SizeChanged -= keepSizeHandler;
                parent.LocationChanged -= locationChangedEventHandler;
                parent.Activate();

                LightBoxVisible = false;
            });

            window.Show();
        }

        private void ToogleVisibility(LightBoxEventArgs e)
        {
            LightBoxVisible = e.LightBoxVisible;
        }

        private void ShowWindow(Window window, bool blocking)
        {
            var owner = mainWindowProvider.GetMainWindow();
           
            {

                try
                {
                    window.Owner = mainWindowProvider.GetMainWindow();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    
                }

                LightBoxVisible = true;

                    if (blocking)
                    {
                        window.ShowDialog();

                        LightBoxVisible = false;
                    }
                    else
                    {
                        ShowNonBlockingModal(window);
                    }                
             }
            

            

            

            
            
            

            
            
            
            
            
            
        }

        private Window CreateWindow(LightBoxEventArgs e)
        {
            Window windowInstance = null;
            if (e.WindowInstance != null)
            {
                windowInstance = e.WindowInstance;
            }
            else
            {
                var constructor = e.WindowType?.GetConstructor(new Type[0]);
                var container = constructor?.Invoke(null) as Window;
                if (container != null)
                {
                    container.DataContext = e.DataContext;
                    windowInstance = container;
                }
            }
            return windowInstance;
        }

        public bool LightBoxVisible
        {
            get { return showLightBox; }
            private set
            {
                if (!Equals(showLightBox, value))
                {
                    if (!value)
                    {
                        var leroy = 123;
                        Console.WriteLine(leroy);
                    }
                    showLightBox = value;
                    OnPropertyChanged();
                }
            }
        }
    }
}
