using Microsoft.UI.Input;

using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.System;
using static Sonex.UserControls.DataGridView.DataGridView;

namespace Sonex.UserControls.DataGridView
{
    public sealed partial class DataGridView
    {
        private int? _IndexFrom = null;
        private int? _IndexTo = null;
        bool RightButton;
        bool addOrRemove;
        private void OnIndexPressed(PointerRoutedEventArgs e)
        {

            PointerPoint? pointer = e.GetCurrentPoint(_Canvas);
            var row = GetRowIndexAtPosition(pointer.Position);

            bool Shift = e.KeyModifiers.HasFlag(VirtualKeyModifiers.Shift);
            bool Alt = e.KeyModifiers.HasFlag(VirtualKeyModifiers.Menu);
            bool Ctrl = e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control);

            _IndexFrom = row;
            _IndexTo = row;

            addOrRemove = !_SelectedRows.Has(_IndexFrom.Value);
            RequestRender();
        }

        private void OnIndexDrag(PointerRoutedEventArgs e)
        {
            if (e != null)
            {
                var p = e.GetCurrentPoint(_Canvas);
                if (p != null)
                {
                    _LastPointer = p;
                }
            }
            var row = GetRowIndexAtPosition(_LastPointer.Position);
            _IndexTo = row;

            if (_IndexFrom == null || _IndexTo == null)
                return;

            int min = Math.Min(_IndexFrom.Value, _IndexTo.Value);
            int max = Math.Max(_IndexFrom.Value, _IndexTo.Value);

            if (MouseLeftButtonDown && addOrRemove)
            {
                for (int i = min; i <= max; i++)
                    _SelectedRows.Add(i);
            }
            else if (MouseLeftButtonDown && !addOrRemove)
            {
                for (int i = min; i <= max; i++)
                    _SelectedRows.Remove(i);
            }


            if (_LastPointer != null && MouseLeftButtonDown)
            {
                if (_LastPointer.Position.Y > ActualHeight - 80)
                {
                    double power = Math.Clamp(((ActualHeight - _LastPointer.Position.Y) / 80d) * 400, 20, 400);
                    _ScrollMoverVertical.GoToEnd(power);
                }
                else if (_LastPointer.Position.Y < _HeaderHeight + _RowsHeight)
                {
                    double power = Math.Clamp(_LastPointer.Position.Y / (_HeaderHeight + _RowsHeight) * 400, 20, 400);
                    _ScrollMoverVertical.GoToStart(power);
                }
                else
                    _ScrollMoverVertical.Stop();
            }

            RequestRender();
        }


        private void OnIndexReleased(PointerRoutedEventArgs e)
        {
            PointerPoint? pointer = e.GetCurrentPoint(_Canvas);
            var row = GetRowIndexAtPosition(pointer.Position);
            _IndexTo = row;
            bool Shift = e.KeyModifiers.HasFlag(VirtualKeyModifiers.Shift);
            bool Alt = e.KeyModifiers.HasFlag(VirtualKeyModifiers.Menu);
            bool Ctrl = e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control);




            if (_IndexFrom == null || _IndexTo == null)
                return;

            int min = Math.Min(_IndexFrom.Value, _IndexTo.Value);
            int max = Math.Max(_IndexFrom.Value, _IndexTo.Value);

            if (MouseLeftButtonDown && addOrRemove)
            {
                for (int i = min; i <= max; i++)
                    _SelectedRows.Add(i);
            }
            else if (MouseLeftButtonDown && !addOrRemove)
            {
                for (int i = min; i <= max; i++)
                    _SelectedRows.Remove(i);
            }
            _ScrollMoverVertical.Stop();
            _IndexFrom = null;
            _IndexTo = null;
            UpdateFooterTextSelected();
            RequestRender();
        }

        private void OnIndexDoubleTapped(PointerPoint e)
        {
            var row = GetRowIndexAtPosition(e.Position);
            _SelectedRows.Add(row);
            foreach (var column in _Columns)
                column.isSelected = true;//!column.IsHiden;
            RequestRender();
        }
    }
}
