using System.Runtime.CompilerServices;
using Telerik.WinControls;
using Telerik.WinControls.Svg;
using Telerik.WinControls.UI;
using Telerik.WinControls.UI.SplashScreen;

namespace Pilz.UI.Telerik.Dialogs;

partial class RadFlyoutBase
{
    public delegate void FlyoutCreatedEventHandler(FlyoutCreatedEventArgs e);
    public delegate void FlyoutClosedEventHandler(FlyoutClosedEventArgs e);

    public static event FlyoutCreatedEventHandler FlyoutCreated
    {
        add => flyoutCreatedHandlers.Add(value);
        remove => flyoutCreatedHandlers.Remove(value);
    }

    public static event FlyoutClosedEventHandler FlyoutClosed
    {
        add => flyoutCloseHandlers.Add(value);
        remove => flyoutCloseHandlers.Remove(value);
    }

    private static readonly List<FlyoutCreatedEventHandler> flyoutCreatedHandlers = [];
    private static readonly List<FlyoutClosedEventHandler> flyoutCloseHandlers = [];

    private static object? tagToAssign = null;
    private static string? titleToAssing = null;
    private static RadSvgImage? iconToAssign = null;

    public static Control? ParentContext { get; private set; } = null;

    static RadFlyoutBase()
    {
        RadFlyoutManager.ContentCreated += RadFlyoutManager_ContentCreated;
        RadFlyoutManager.FlyoutClosed += RadFlyoutManager_FlyoutClosed;
    }

    private static void RadFlyoutManager_ContentCreated(ContentCreatedEventArgs e)
    {
        if (e.Content is RadFlyoutBase dialogBase)
        {
            if (tagToAssign != null)
                dialogBase.Tag = tagToAssign;

            if (titleToAssing != null)
                dialogBase.Title = titleToAssing;

            if (iconToAssign != null)
                dialogBase.TitleIcon = iconToAssign;

            var eventArgs = new FlyoutCreatedEventArgs(dialogBase);

            if (dialogBase is ILoadContent iLoadContent)
                iLoadContent.LoadContent();
            else if (dialogBase is ILoadContentAsync iLoadContentAsync)
                Task.Run(iLoadContentAsync.LoadContentAsync).Wait();

            foreach (var args in flyoutCreatedHandlers.ToArray())
            {
                if (ParentContext != null)
                    ParentContext?.Invoke(args, eventArgs);
                else
                    args.Invoke(eventArgs);
            }
        }
    }

    private static void RadFlyoutManager_FlyoutClosed(global::Telerik.WinControls.UI.SplashScreen.FlyoutClosedEventArgs e)
    {
        if (e.Content is RadFlyoutBase dialogBase)
        {
            var eventArgs = new FlyoutClosedEventArgs((RadFlyoutBase)e.Content);

            foreach (var args in flyoutCloseHandlers.ToArray())
            {
                if (ParentContext != null)
                    ParentContext?.Invoke(args, eventArgs);
                else
                    args.Invoke(eventArgs);
            }
        }

        ParentContext = null;
    }

    public static void Show<T>(Control controlToAssociate, object? tag = null)
    {
        Show<T>(controlToAssociate, null, null, tag: tag);
    }

    public static void Show<T>(Control controlToAssociate, string? title, RadSvgImage? icon, object? tag = null)
    {
        Show(controlToAssociate, typeof(T), title, icon, tag: tag);
    }

    public static void Show(Control controlToAssociate, Type flyoutContentType, string? title, RadSvgImage? icon, object? tag = null)
    {
        tagToAssign = tag;
        titleToAssing = title;
        iconToAssign = icon;
        ParentContext = controlToAssociate;
        CloseFlyout(); // Ensure it's closed!
        RadFlyoutManager.Show(controlToAssociate, flyoutContentType);
    }

    protected static void CloseFlyout()
    {
        if (typeof(RadFlyoutManager).GetField("flyoutInstance", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null) is FlyoutScreen instance
            && instance.IsActive)
            RadFlyoutManager.Close();
    }
}
