using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sonex.Client.UserControls.DataGridView
{
    public sealed partial class DataGridView
    {
        bool _NeedCalcHorizontalScroll = true;
        bool _NeedCalcVerticalScroll = true;
        double _HorizontalScrollOffset = 0;
        int _TopRowIndex = 0;
        int _MaxIndexOnScreen = 0;

        private void CalcHorizontalScroll()
        {
            if (_NeedCalcHorizontalScroll == false) return;
            var newValue = Math.Max(0, _TotalColWidth - _Canvas.ActualWidth + _IndexColWidth);
            foreach (var column in _Columns)
            {
                if(column.IsHiden)
                    { continue; }
                if (column.IsPinned)
                    column.LeftOnScreen = column.Left;
                else
                    column.LeftOnScreen = column.Left - _HorizontalScrollOffset;
            }

            foreach (Column column in _Columns)
            {
                if (column.IsHiden)
                {
                    column.IsOnScreen = false;
                    continue;
                }
                if (column.IsPinned)
                {
                    column.IsOnScreen = (column.LeftOnScreen < ActualWidth) && (column.LeftOnScreen + column.Width > _IndexColWidth);
                }
                else
                {
                    column.IsOnScreen = (column.LeftOnScreen < ActualWidth) && ((column.LeftOnScreen + column.Width) > (_IndexColWidth + _PinedColWidth));
                }
            }
            if (_ScrollHorizontal.Maximum != newValue)
                _ScrollHorizontal.Maximum = newValue;
            _NeedCalcHorizontalScroll = false;
        }
        private void _ScrollHorizontal_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            
            _HorizontalScrollOffset = e.NewValue;
            //foreach (var column in _ColumnsHeaders)
            //{
            //    if (column.IsPinned)
            //        column.LeftOnScreen = column.Left;
            //    else
            //        column.LeftOnScreen = column.Left - _HorizontalScrollOffset;
            //}
            if (MouseLeftButtonDown && _SelectRange.From != null)
                OnCellDrag(null);
            else if (MouseLeftButtonDown && _IndexFrom != null && _IndexTo != null)
                OnIndexDrag(null);
            //_Canvas.Focus(Microsoft.UI.Xaml.FocusState.Pointer);
            _NeedCalcHorizontalScroll = true;
            RequestRender();
        }
        private void _ScrollVertical_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            _TopRowIndex = (int)e.NewValue;

            if (MouseLeftButtonDown && _SelectRange.From != null)
                OnCellDrag(null);
            else if (MouseLeftButtonDown && _IndexFrom != null && _IndexTo != null)
                OnIndexDrag(null);
            //_Canvas.Focus(Microsoft.UI.Xaml.FocusState.Pointer);
            RequestRender();
        }
        private void CalcVerticalScroll()
        {
            if (_NeedCalcVerticalScroll == false) return;
            int newValue;
            if (_ItemsSourceFiltred == null || _ItemsSourceFiltred.Count == 0)
            {
                newValue = 0;
            }
            else
            {
                newValue = Math.Max(0, _ItemsSourceFiltred.Count - _MaxIndexOnScreen + 1);
            }
            if (_ScrollVertical.Maximum != newValue)
                _ScrollVertical.Maximum = newValue;
            _NeedCalcVerticalScroll = false;

        }
    }
}
