using ARMeilleure.Diagnostics.EventSources;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ARMeilleure.Common
{
    /// 
    /// Represents a table of guest address to a value.
    /// 
    /// Type of the value
    unsafe class AddressTable : IDisposable where TEntry : unmanaged
    {
        /// 
        /// Represents a level in an .
        /// 
        public readonly struct Level
        {
            /// 
            /// Gets the index of the  in the guest address.
            /// 
            public int Index { get; }
            /// 
            /// Gets the length of the  in the guest address.
            /// 
            public int Length { get; }
            /// 
            /// Gets the mask which masks the bits used by the .
            /// 
            public ulong Mask => ((1ul << Length) - 1) << Index;
            /// 
            /// Initializes a new instance of the  structure with the specified
            ///  and .
            /// 
            /// Index of the 
            /// Length of the 
            public Level(int index, int length)
            {
                (Index, Length) = (index, length);
            }
            /// 
            /// Gets the value of the  from the specified guest .
            /// 
            /// Guest address
            /// Value of the  from the specified guest 
            public int GetValue(ulong address)
            {
                return (int)((address & Mask) >> Index);
            }
        }
        private bool _disposed;
        private TEntry** _table;
        private readonly List _pages;
        /// 
        /// Gets the bits used by the  of the  instance.
        /// 
        public ulong Mask { get; }
        /// 
        /// Gets the s used by the  instance.
        /// 
        public Level[] Levels { get; }
        /// 
        /// Gets or sets the default fill value of newly created leaf pages.
        /// 
        public TEntry Fill { get; set; }
        /// 
        /// Gets the base address of the .
        /// 
        ///  instance was disposed
        public IntPtr Base
        {
            get
            {
                if (_disposed)
                {
                    throw new ObjectDisposedException(null);
                }
                lock (_pages)
                {
                    return (IntPtr)GetRootPage();
                }
            }
        }
        /// 
        /// Constructs a new instance of the  class with the specified list of
        /// .
        /// 
        ///  is null
        /// Length of  is less than 2
        public AddressTable(Level[] levels)
        {
            if (levels == null)
            {
                throw new ArgumentNullException(nameof(levels));
            }
            if (levels.Length < 2)
            {
                throw new ArgumentException("Table must be at least 2 levels deep.", nameof(levels));
            }
            _pages = new List(capacity: 16);
            Levels = levels;
            Mask = 0;
            foreach (var level in Levels)
            {
                Mask |= level.Mask;
            }
        }
        /// 
        /// Determines if the specified  is in the range of the
        /// .
        /// 
        /// Guest address
        ///  if is valid; otherwise 
        public bool IsValid(ulong address)
        {
            return (address & ~Mask) == 0;
        }
        /// 
        /// Gets a reference to the value at the specified guest .
        /// 
        /// Guest address
        /// Reference to the value at the specified guest 
        ///  instance was disposed
        ///  is not mapped
        public ref TEntry GetValue(ulong address)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(null);
            }
            if (!IsValid(address))
            {
                throw new ArgumentException($"Address 0x{address:X} is not mapped onto the table.", nameof(address));
            }
            lock (_pages)
            {
                return ref GetPage(address)[Levels[^1].GetValue(address)];
            }
        }
        /// 
        /// Gets the leaf page for the specified guest .
        /// 
        /// Guest address
        /// Leaf page for the specified guest 
        private TEntry* GetPage(ulong address)
        {
            TEntry** page = GetRootPage();
            for (int i = 0; i < Levels.Length - 1; i++)
            {
                ref Level level = ref Levels[i];
                ref TEntry* nextPage = ref page[level.GetValue(address)];
                if (nextPage == null)
                {
                    ref Level nextLevel = ref Levels[i + 1];
                    nextPage = i == Levels.Length - 2 ?
                        (TEntry*)Allocate(1 << nextLevel.Length, Fill, leaf: true) :
                        (TEntry*)Allocate(1 << nextLevel.Length, IntPtr.Zero, leaf: false);
                }
                page = (TEntry**)nextPage;
            }
            return (TEntry*)page;
        }
        /// 
        /// Lazily initialize and get the root page of the .
        /// 
        /// Root page of the 
        private TEntry** GetRootPage()
        {
            if (_table == null)
            {
                _table = (TEntry**)Allocate(1 << Levels[0].Length, fill: IntPtr.Zero, leaf: false);
            }
            return _table;
        }
        /// 
        /// Allocates a block of memory of the specified type and length.
        /// 
        /// Type of elements
        /// Number of elements
        /// Fill value
        ///  if leaf; otherwise 
        /// Allocated block
        private IntPtr Allocate(int length, T fill, bool leaf) where T : unmanaged
        {
            var size = sizeof(T) * length;
            var page = (IntPtr)NativeAllocator.Instance.Allocate((uint)size);
            var span = new Span((void*)page, length);
            span.Fill(fill);
            _pages.Add(page);
            AddressTableEventSource.Log.Allocated(size, leaf);
            return page;
        }
        /// 
        /// Releases all resources used by the  instance.
        /// 
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        /// 
        /// Releases all unmanaged and optionally managed resources used by the 
        /// instance.
        /// 
        ///  to dispose managed resources also; otherwise just unmanaged resouces
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                foreach (var page in _pages)
                {
                    Marshal.FreeHGlobal(page);
                }
                _disposed = true;
            }
        }
        /// 
        /// Frees resources used by the  instance.
        /// 
        ~AddressTable()
        {
            Dispose(false);
        }
    }
}