using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls.Primitives;
using System;

namespace Sonex.Client.Controls.DataGridView
{
    // Odpowiedzialnosc pliku: plynne przewijanie ScrollBar przez timer i krokowe przyspieszenie.
    public sealed partial class DataGridView
    {
        // Pomocnicza klasa izolujaca animowane przewijanie od glownej logiki DataGridView.
        public sealed class SmoothStepScroller : IDisposable
        {
            private readonly DispatcherTimer _timer;
            private readonly ScrollBar _scroll;
            private double _step; // ile jednostek na tick
            public bool IsWorking => _timer.IsEnabled;
            private int _direction; // -1 = start, +1 = end

            public SmoothStepScroller(ScrollBar scroll, int step = 1, int intervalMs = 16)
            {
                _scroll = scroll;
                _step = Math.Max(1, step);

                _timer = new DispatcherTimer();
                _timer.Interval = TimeSpan.FromMilliseconds(intervalMs);
                _timer.Tick += OnTick;
            }

            public void GoToStart(double intervalMs = 16, double step = 1)
            {
                if (_scroll.Value == 0)
                    return;
                _step = Math.Max(1, step);
                _timer.Interval = TimeSpan.FromMilliseconds(intervalMs);
                _direction = -1;
                if (!_timer.IsEnabled)
                {
                    _timer.Interval = TimeSpan.FromMilliseconds(100);
                    _timer.Start();
                }
            }

            public void GoToEnd(double intervalMs = 16, double step = 1)
            {
                if (_scroll.Value == _scroll.Maximum)
                    return;
                _step = Math.Max(1, step);
                _timer.Interval = TimeSpan.FromMilliseconds(intervalMs);
                _direction = 1;
                if (!_timer.IsEnabled)
                {
                    _timer.Interval = TimeSpan.FromMilliseconds(100);
                    _timer.Start();
                }
            }

            public void Stop()
            {
                if (_timer.IsEnabled)
                    _timer.Stop();
            }

            public void Dispose()
            {
                Stop();
                _timer.Tick -= OnTick;
            }

            private void OnTick(object? sender, object e)
            {
                if (_direction == 0) return;


                double newValue = _scroll.Value + (_direction * _step);

                if (newValue <= _scroll.Minimum)
                {
                    _scroll.Value = _scroll.Minimum;
                    Stop();
                    return;
                }

                if (newValue >= _scroll.Maximum)
                {
                    _scroll.Value = _scroll.Maximum;
                    Stop();
                    return;
                }

                _scroll.Value = newValue;

                //_timer.Interval = TimeSpan.FromMilliseconds(_timer.Interval.TotalMilliseconds * 0.8d);
                //_timer.Start();
            }
        }
    }
}
