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.Text;
using System.Threading.Tasks;
using Windows.System;
using Windows.UI.Core;

namespace Sonex.Client.UserControls.DataGridView
{
    public sealed partial class DataGridView
    {
        private CellVector? GetCellByKey(VirtualKey key, bool ctrl = false)
        {
            if (_ActiveCell == null) return null;
            if (_ItemsSourceFiltred == null) return null;

            // Ustawianie kolumny
            int colIndex;

            if (key == VirtualKey.Left && ctrl) // Na koniec w lewo
                colIndex = 0;
            else if (key == VirtualKey.Right && ctrl)
                colIndex = _Columns.Count-1;
            else if (key == VirtualKey.Left)
                colIndex = Math.Max(_Columns.IndexOf(_ActiveCell.Header) - 1, 0);
            else if (key == VirtualKey.Right)
                colIndex = Math.Min(_Columns.IndexOf(_ActiveCell.Header) + 1, _Columns.Count - 1);
            else
                colIndex = _Columns.IndexOf(_ActiveCell.Header);


            // Ustawianie wiersza
            int rowIndex = 0;

            if ((key == VirtualKey.Up && ctrl) || key == VirtualKey.Home)
                rowIndex = 0;
            else if ((key == VirtualKey.Down && ctrl) || key == VirtualKey.End)
                rowIndex = _ItemsSourceFiltred.Count - 1;
            else if (key == VirtualKey.Up)
                rowIndex = Math.Max(_ActiveCell.RowIndex - 1, 0);
            else if (key == VirtualKey.Down)
                rowIndex = Math.Min(_ActiveCell.RowIndex + 1, _ItemsSourceFiltred.Count - 1);
            else
                rowIndex = _ActiveCell.RowIndex;


            return new CellVector(_Columns[colIndex], rowIndex);
        }
        public void SelectLeftCell()
        {
            if (_ActiveCell == null) return;
            var index = _Columns.IndexOf(_ActiveCell.Header)-1;
            if (index < 0) return;
            if (index >= _Columns.Count) return;
            _ActiveCell.Header = _Columns[index];
            RequestRender();
        }
        public void SelectRightCell()
        {
            
            if (_ActiveCell == null) return;
            var index = _Columns.IndexOf(_ActiveCell.Header) + 1;
            if (index < 0) return;
            if (index >= _Columns.Count) return;
            _ActiveCell.Header = _Columns[index];
            RequestRender();
        }
        private void _Canvas_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            var focused = FocusManager.GetFocusedElement(XamlRoot);

            if (focused is TextBox
                or PasswordBox
                or RichEditBox
                or AutoSuggestBox
                or ComboBox)   // często też warto dodać
            {
                return; // NIE ustawiaj e.Handled
            }
            e.Handled = true;

            VirtualKey key = e.Key;
            bool ctrl = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);
            bool shift = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
            bool alt = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Menu).HasFlag(CoreVirtualKeyStates.Down);

            if(ctrl && key == VirtualKey.C)
            {
                SelectToClipboard(shift);
            }
            else if (ctrl && key == VirtualKey.A)
            {
                SellectAll();
                RequestRender();
                UpdateFooterTextSelected();

            }
            else if (ctrl && key == VirtualKey.D)
            {
                UnSellectAll();
                RequestRender();
                UpdateFooterTextSelected();
            }
            //else if (key == VirtualKey.Left || key == VirtualKey.Right || key == VirtualKey.Up || key == VirtualKey.Down || shift || alt)
            else if (key == VirtualKey.Left || key == VirtualKey.Right || key == VirtualKey.Up || key == VirtualKey.Down)
            {
                var Vector = GetCellByKey(key, ctrl||alt);
                if (Vector == null) return;

                if ((shift || alt) && _SelectRange.From != null)
                {
                    _SelectRange.To = new CellVector(Vector.Header, Vector.RowIndex);
                    _SelectRange.Remove = alt;
                    _ActiveCell = new CellVector(Vector.Header, Vector.RowIndex);
                    GoToActiveCell();
                }
                else if ((shift || alt) && _SelectRange.From == null && _ActiveCell != null)
                {
                    _SelectRange.From = new(_ActiveCell.Header, _ActiveCell.RowIndex);
                    _SelectRange.To = new CellVector(Vector.Header, Vector.RowIndex);
                    _SelectRange.Remove = alt;
                    _ActiveCell = new CellVector(Vector.Header, Vector.RowIndex);
                    GoToActiveCell();

                }
                else
                {
                    UnSellectAll();
                    _SelectRange.From = new CellVector(Vector.Header, Vector.RowIndex);
                    _SelectRange.To = new CellVector(Vector.Header, Vector.RowIndex);
                    _SelectRange.Remove = false;
                    _ActiveCell = new CellVector(Vector.Header, Vector.RowIndex);
                    GoToActiveCell();

                }



            }



            RequestRender();

        }
        private void _Canvas_KeyUp(object sender, KeyRoutedEventArgs e)
        {
            //e.Handled = true;
            if(e.Key == VirtualKey.Shift || e.Key == VirtualKey.Menu)
            {
                _ApplySelectRange();
                RequestRender();
            }
        }


    }
}
