using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Input;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;

namespace Sonex.Client.UserControls.DataGridView
{
    public sealed partial class DataGridView
    {
        private AreaType _ActualArea = AreaType.None;
        private AreaType _HandleArea = AreaType.None;
        private bool MouseLeftButtonDown = false;
        private bool MouseRightButtonDown = false;
        DateTime lastPointerPressedTime = DateTime.MinValue;
        private void _Canvas_PointerExited(object sender, PointerRoutedEventArgs e)
        {
            HeaderHover = null;
            RequestRender();
        }

        private void _Canvas_DoubleTapped(object sender, PointerPoint e)
        {
            Focus(Microsoft.UI.Xaml.FocusState.Programmatic);
            var area = GetPoiterArea(e.Position);
            if (area == _HandleArea)
            {
                return;
            }
            else
            {
                _HandleArea = area;
                switch (area)
                {
                    case AreaType.Cell:
                        OnCellDoubleTapped(e);
                        break;
                    case AreaType.Header:
                        OnHeaderDoubleTapped(e);
                        break;
                    case AreaType.Index:
                        OnIndexDoubleTapped(e);
                        break;
                    default: break;
                }
            }
        }
        private void _Canvas_PointerPressed(object sender, PointerRoutedEventArgs e)
        {


            PointerPoint? pointer = e.GetCurrentPoint(_Canvas);
            if (pointer!=null && lastPointerPressedTime.AddMilliseconds(300) > DateTime.Now)
                _Canvas_DoubleTapped(sender, (PointerPoint)pointer);
            lastPointerPressedTime = DateTime.Now;

            if (pointer != null)
            {
                MouseLeftButtonDown = pointer.Properties.IsLeftButtonPressed;
                MouseRightButtonDown = pointer.Properties.IsLeftButtonPressed;
            }
            _Canvas.CapturePointer(e.Pointer); // przejęcie
            Focus(Microsoft.UI.Xaml.FocusState.Programmatic);
            e.Handled = true;
            var area = GetPoiterArea(e.GetCurrentPoint(_Canvas));
            if (area == _HandleArea)
            {
                return;
            }
            else
            {
                _HandleArea = area;
                switch (area)
                {
                    case AreaType.Cell:
                        OnCellPressed(e);
                        break;
                    case AreaType.Header:
                        _=OnHeaderPressedAsync(e);
                        break;
                    case AreaType.Index:
                        OnIndexPressed(e);
                        break;
                    default: break;
                }
            }
            base.OnPointerPressed(e);

        }



        private void _Canvas_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            //HeaderHover = null;

            if (MouseLeftButtonDown)
            {
                switch (_HandleArea)
                {
                    case AreaType.Cell:
                        OnCellDrag(e);
                        break;
                    case AreaType.Header:
                        OnHeaderDrag(e);
                        break;
                    case AreaType.Index:
                        OnIndexDrag(e);
                        break;
                    default:
                        _ActualArea = GetPoiterArea(e.GetCurrentPoint(_Canvas));
                        break;
                }
            }
            else
            {
                var area = GetPoiterArea(e.GetCurrentPoint(_Canvas));
                if (area != _ActualArea)
                {
                    _ActualArea = area;
                    HeaderHover = null;
                    RequestRender();
                }
                switch (_ActualArea)
                {
                    case AreaType.Cell:
                        //OnCellMoved(e);
                        break;
                    case AreaType.Header:
                        OnHeaderMoved(e);
                        break;
                    case AreaType.Index:
                        //OnIndexMoved(e);
                        break;
                    default:
                        break;
                }
                
            }
        }
        private void _Canvas_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            PointerPoint? pointer = e.GetCurrentPoint(_Canvas);

            Focus(Microsoft.UI.Xaml.FocusState.Programmatic);
            e.Handled = false;
            //base.OnPointerReleased(e);
            //_Canvas.ReleasePointerCapture(e.Pointer); // zwolnienie przejęcia
            switch (_HandleArea)
            {
                case AreaType.Cell:
                    OnCellReleased(e);
                    break;
                case AreaType.Header:
                    OnHeaderReleased(e);
                    break;
                case AreaType.Index:
                    OnIndexReleased(e);
                    break;
                default:
                    Debug.WriteLine("Outside clicked");
                    break;
            }
            _HandleArea = AreaType.None;
            if (pointer != null)
            {
                MouseLeftButtonDown = pointer.Properties.IsLeftButtonPressed;
                MouseRightButtonDown = pointer.Properties.IsLeftButtonPressed;
            }
            base.OnPointerReleased(e);

        }

        enum AreaType
        {
            Cell,
            Header,
            Index,
            None
        }

        private AreaType GetPoiterArea(PointerPoint p)
        {
            return GetPoiterArea(p.Position);
        }
        private AreaType GetPoiterArea(Point p)
        {
            if (p.Y < _HeaderHeight) return AreaType.Header;
            if (p.X < _IndexColWidth) return AreaType.Index;
            return AreaType.Cell;
        }
    }
}
