using FluentIcons.Common.Internals;
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.Runtime;
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 async Task OnHeaderPressedAsync(PointerRoutedEventArgs e)
        {

            PointerPoint? pointer = e.GetCurrentPoint(_Canvas);
            var column = GetColumnAtPosition(pointer.Position);

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

            if (column != null && HoverHeaderArea == HeaderArea.Text)
            {
            }
            if (column != null && HoverHeaderArea == HeaderArea.Filter)
            {
                 column.ShowFilter();
                return;
            }
            else if (column != null && HoverHeaderArea == HeaderArea.Pin)
            {
                if(column.IsPinned)
                    UnPin(column.Title);
                else
                    Pin(column.Title);
            }
            else if (column != null && HoverHeaderArea == HeaderArea.ColHide)
            {
                    HideColumn(column.Title);
            }
            else if (column != null && HoverHeaderArea == HeaderArea.Sort)
            {
                if (_ItemsSourceFiltred?.Count > 500000)
                {
                    LoadingRing = true;
                }
                //column.Sort();
                await column.SortAsync();
                LoadingRing = false;
            }
            RequestRender();
        }
        Column? HeaderHover = null;
        HeaderArea HoverHeaderArea = HeaderArea.None;
        bool isHeaderFilterHover => HoverHeaderArea == HeaderArea.Filter;
        bool isHeaderSortHover => HoverHeaderArea == HeaderArea.Sort;
        bool isHeaderColHideHover => HoverHeaderArea == HeaderArea.ColHide;
        bool isHeaderPinHover => HoverHeaderArea == HeaderArea.Pin;
        bool isHeaderStparatorHover => HoverHeaderArea == HeaderArea.Separator;
        bool isHeaderTextHover => HoverHeaderArea == HeaderArea.Text;
        bool isHeaderHover => HoverHeaderArea != HeaderArea.None;
        private void OnHeaderDrag(PointerRoutedEventArgs e)
        {
            PointerPoint? pointer = e.GetCurrentPoint(_Canvas);
            HeaderHitInfo hitInfo = GetHeaderAreaAtPosition(pointer.Position);

            //if (HeaderHover?.Title == hitInfo.Column?.Title && HoverHeaderArea == hitInfo.Area)
            //{
            //    return;
            //}
            //HeaderHover = hitInfo.Column;
            //HoverHeaderArea = hitInfo.Area;

            //Debug.WriteLine($"OnHeaderDrag: {hitInfo.Column?.Title ?? "null"} | {hitInfo.Area} ");
            RequestRender();
        }
        private void OnHeaderMoved(PointerRoutedEventArgs e)
        {
            PointerPoint? pointer = e.GetCurrentPoint(_Canvas);
            HeaderHitInfo hitInfo = GetHeaderAreaAtPosition(pointer.Position);

            if (HeaderHover?.Title == hitInfo.Column?.Title && HoverHeaderArea == hitInfo.Area)
            {
                return;
            }
            HeaderHover = hitInfo.Column;
            HoverHeaderArea = hitInfo.Area;

            RequestRender();
        }

        private void OnHeaderReleased(PointerRoutedEventArgs e)
        {

            PointerPoint? pointer = e.GetCurrentPoint(_Canvas);
            var column = GetColumnAtPosition(pointer.Position);

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

            if (column != null && HoverHeaderArea == HeaderArea.Text)
            {
                column.isSelected = !column.isSelected;
            }
            if (column != null && HoverHeaderArea == HeaderArea.Filter)
            {
                //column.ShowFilter();
            }
            if (column != null && HoverHeaderArea == HeaderArea.Sort)
            {
            }
            else
            {
                RequestRender();
            }
            UpdateFooterTextSelected();

        }

        private void OnHeaderDoubleTapped(PointerPoint e)
        {
            RequestRender();
        }
        private HeaderHitInfo GetHeaderAreaAtPosition(Point position)
        {
            // lewy górny róg
            if (position.X < _IndexColWidth)
                return new HeaderHitInfo(null, HeaderArea.Settings);

            // jeśli nie wykryje kolumny 
            var column = GetColumnAtPosition(position);
            if (column == null)
                return new HeaderHitInfo(null, HeaderArea.None);

            
            var right = column.LeftOnScreen + column.Width;

            if (position.X + 8 >= right)
                return new HeaderHitInfo(column, HeaderArea.Separator);

            if (position.Y < 28)
            {
                if (position.X - column.LeftOnScreen < 28)
                    return new HeaderHitInfo(column, HeaderArea.Filter); 
                if (position.X- column.LeftOnScreen < 48)
                    return new HeaderHitInfo(column, HeaderArea.Sort);
                if (position.X + 24 >= right)
                    return new HeaderHitInfo(column, HeaderArea.Pin);
                if (position.X + 44 >= right)
                    return new HeaderHitInfo(column, HeaderArea.ColHide);
            }

            return new HeaderHitInfo(column, HeaderArea.Text);
        }
        public enum HeaderArea
        {
            None = 0,
            Text = 1,
            Separator = 2,
            Settings = 3,
            Filter = 4,
            Sort = 5,
            Pin = 6,
            ColHide = 7
        }
        public record HeaderHitInfo(Column? Column, HeaderArea Area);

    }
}
