using System.Drawing;
using KioskApp.Portal.Helper;
using KioskApp.Portal.Platforms.Events;
using KioskApp.Portal.Platforms.Linux;
using KioskApp.Portal.Platforms.Windows;
using KioskApp.Portal.Widgets;

namespace KioskApp.Portal;

// NOTE: The Window class is a facade for the platform-specific window implementations.
// It provides a common interface for creating, resizing, and closing windows.
// The Window class uses the OsReflectionHelper class to determine the appropriate platform-specific implementation.

// TODO: Redesign the Window class because right now its just random stuff that i need

public abstract class Window
{
    public abstract event EventHandler<WindowClosingEventArgs> Closing; 
    
    
    private List<Widget> _widgets;
    protected Window(WindowOptions options)
    {
        Title = options.Title;
        Width = options.Width;
        Height = options.Height;
        _widgets = [];
    }

    public string Title { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
    
    public T AddWidget<T>() where T : Widget
    {
        var baseWidget = OsReflectionHelper.GetSupportedClassForCurrentOs(typeof(T));
        
        var widget = (T)Activator.CreateInstance(baseWidget);
        _widgets.Add(widget);
        widget.Initialize(this);
        return widget;
    }

    public static Window Create(WindowOptions options)
    {
        if (OperatingSystem.IsWindows()) return new WindowsWindow(options); 
        if (OperatingSystem.IsLinux()) return new LinuxWindow(options);
        // if (OperatingSystem.IsMacOS()) return new MacWindow(options);
        throw new PlatformNotSupportedException("Platform not supported.");
    }

    protected abstract void InternalCreate();
    
    protected abstract void InternalResize(int width, int height);

    protected abstract bool Close();

    public abstract void Show();
    
    public abstract T GetNativeWindow<T>() where T : class;

    public abstract IntPtr GetNativeHandle();

    public abstract SizeF GetClientSize();
    
    public abstract void Fullscreen();
    
    protected List<Widget> GetWidgets()
    {
        return _widgets;
    }
}