using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Decal.Interop.Filters;

namespace Decal.DecalFilters
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    public class VendorImpl : IVendor
    {
        private readonly List<WorldObjectImpl> _items = new List<WorldObjectImpl>();
        private int _index = -1;

        public int MerchantID { get; internal set; }
        public int BuyCategories { get; internal set; }
        public int BuyValue { get; internal set; }
        public float BuyRate { get; internal set; }
        public float SellRate { get; internal set; }
        public int Count => _items.Count;
        public int Quantity => _items.Count;

        public void Reset() => _index = -1;

        public bool Next(ref WorldObject ppObject)
        {
            _index++;
            if (_index < _items.Count)
            {
                ppObject = (WorldObject)(object)_items[_index];
                return true;
            }
            ppObject = null;
            return false;
        }

        public void Pop() { }
        public void ByAll() => _index = -1;
        public void ByName(string strName) { }
        public void ByNameSubstring(string strSubstring) { }
        public void ByObjectClass(eObjectClass Class) { }
        public void ByCategory(int nCategory) { }

        public WorldObject GetByID(int pObjectId)
        {
            foreach (var item in _items)
                if (item.GUID == pObjectId) return (WorldObject)(object)item;
            return null;
        }

        public IEnumerator GetEnumerator()
        {
            _index = -1;
            return new VendorEnumerator(this);
        }

        private class VendorEnumerator : IEnumerator
        {
            private readonly VendorImpl _vendor;
            public VendorEnumerator(VendorImpl v) { _vendor = v; }
            public object Current => _vendor._index >= 0 && _vendor._index < _vendor._items.Count
                ? _vendor._items[_vendor._index] : null;
            public bool MoveNext() { _vendor._index++; return _vendor._index < _vendor._items.Count; }
            public void Reset() => _vendor._index = -1;
        }
    }
}
