using Avalonia;
using Avalonia.Controls;
using Ehwrj.App.Infrastructure;
using Ehwrj.App.Models;

namespace Ehwrj.App;

public partial class OverlayWindow : Window
{
    private readonly OverlaySettings _settings;

    public OverlayWindow()
        : this(new OverlaySettings())
    {
    }

    public OverlayWindow(OverlaySettings settings)
    {
        _settings = settings;
        InitializeComponent();
        Opened += OnOpened;
    }

    public void ApplyBounds()
    {
        var size = Math.Max(160, _settings.Size);
        Width = size;
        Height = size;

        var area = Screens.Primary?.WorkingArea ?? new PixelRect(0, 0, 1920, 1080);
        Position = new PixelPoint(
            area.X + area.Width - (int)Math.Round(size) - (int)Math.Round(_settings.Right),
            area.Y + (int)Math.Round(_settings.Top));
    }

    private void OnOpened(object? sender, EventArgs e)
    {
        if (TryGetPlatformHandle()?.Handle is { } handle)
        {
            Win32.MakeOverlayClickThrough(handle);
        }

        ApplyBounds();
    }
}
