using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.Graphics.Canvas.UI.Xaml;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace Sonex.Client.Pages;

/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class BlankPage3 : Page
{
    public BlankPage3()
    {
        InitializeComponent();

        Canvas.Loaded += (_, _) => UpdateScrollbars();
        Canvas.SizeChanged += (_, _) => UpdateScrollbars();

        V.Minimum = 0;
        H.Minimum = 0;

        Loaded += (_, _) =>
        {
            V.Minimum = 0; V.Maximum = 100; V.ViewportSize = 10; V.Value = 50;
            H.Minimum = 0; H.Maximum = 100; H.ViewportSize = 10; H.Value = 50;
        };
    }
    private void Canvas_Draw(CanvasControl sender, CanvasDrawEventArgs args) 
    {
        args.DrawingSession.Clear(Microsoft.UI.Colors.White);
        args.DrawingSession.DrawText($"Hello WinUI 3!\n\r ox:{_ox} oy:{_oy}", 100 - (float)_ox, 100 - (float)_oy, Microsoft.UI.Colors.Black);
    }
    private const double Total = 1000;

    private void UpdateScrollbars()
    {
        var vw = Canvas.ActualWidth;
        var vh = Canvas.ActualHeight;
        if (vw <= 0 || vh <= 0) return;

        V.ViewportSize = vh;
        V.Maximum = Math.Max(0, Total - vh);

        H.ViewportSize = vw;
        H.Maximum = Math.Max(0, Total - vw);
    }

    double _ox, _oy;

    private void Scroll_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
    {
        _ox = H.Value;
        _oy = V.Value;
        Canvas.Invalidate(); // przerysuj wg offsetów
    }
}

