using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;

namespace Decal.Adapter.Wrappers;

public sealed class IndexedCollection<InternalIndexType, IndexType, RType> : MarshalByRefObject, IEnumerable<RType>, IEnumerable where InternalIndexType : struct, IConvertible where IndexType : struct, IConvertible
{
	private InternalIndexType myDataType;

	private IIndexedProvider<InternalIndexType> myWrap;

	public RType this[IndexType index] => (RType)myWrap.GetIndexedObject(myDataType, index.ToInt32(CultureInfo.InvariantCulture));

	public int Count => myWrap.Count(myDataType);

	internal IndexedCollection(IIndexedProvider<InternalIndexType> wrap, InternalIndexType data)
	{
		myDataType = data;
		myWrap = wrap;
	}

	IEnumerator<RType> IEnumerable<RType>.GetEnumerator()
	{
		return (IEnumerator<RType>)myWrap.GetEnumerator(myDataType);
	}

	IEnumerator IEnumerable.GetEnumerator()
	{
		return myWrap.GetEnumerator(myDataType);
	}
}
