using C4IT.Logging;
using FasdDesktopUi.Basics.Models;
using System;
using System.Linq;
using System.Windows;

namespace FasdDesktopUi.Pages
{
    public class CustomPopupBase : Window, IDisposable, IBlurInvoker
    {
        public CustomPopupBase()
        {
            this.IsVisibleChanged += SettingsPageBase_IsVisibleChanged;
        }

        private void SettingsPageBase_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if (!(sender is CustomPopupBase senderPage))
                    return;

                if (senderPage.Visibility != Visibility.Visible)
                    return;

                foreach (var window in Application.Current.Windows.OfType<CustomPopupBase>().Where(w => w.IsVisible && w != sender).ToArray())
                {
                    window.Hide();
                    window.Dispose();
                }
            }
            catch (Exception ex)
            {
                cLogManager.LogException(ex);
            }
        }


        public void BlurInvoker_IsActiveChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                BlurInvoker.InvokeVisibilityChanged(this, new EventArgs());
            }
            catch (Exception E)
            {
                cLogManager.LogException(E);
            }
        }

        public bool BlurInvoker_IsActive => IsVisible;

        public virtual void Dispose() { }
    }
}
