using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Input;

namespace Xilium.CefGlue.Avalonia
{
    /// <summary>
    /// Provides handles for mouse cursors.
    /// </summary>
    internal class CursorsProvider
    {
        private static IDictionary<IntPtr, Cursor> _cache = new ConcurrentDictionary<IntPtr, Cursor>();

        static CursorsProvider()
        {
            foreach(StandardCursorType type in Enum.GetValues(typeof(StandardCursorType)))
            {
                var cursor = new Cursor(type);
                var platformImpl = cursor.PlatformImpl;
                if (platformImpl != null)
                {
                    var platformImplType = platformImpl.GetType();
                    var handleProperty = platformImplType.GetProperties().FirstOrDefault(x => x.Name == "Handle");
                    if (handleProperty != null)
                    {
                        var handle = (IntPtr)handleProperty.GetValue(platformImpl);
                        _cache[handle] = cursor;
                    }
                }
            }
        }

        public static Cursor GetCursorFromHandle(IntPtr handle)
        {
            if (_cache.TryGetValue(handle, out var cursor))
            {
                return cursor;
            }
            return Cursor.Default;
        }
    }
}