using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PSO2SERVER.Models
{
    public class FixedVec<T>
    {
        private List<T> _data;
        private int _capacity;

        public FixedVec(int size)
        {
            if (size <= 0)
                throw new ArgumentException("Size must be greater than zero.", nameof(size));

            _data = new List<T>(size);
            _capacity = size;
        }
        public FixedVec(int size, T defaultValue)
        {
            if (size <= 0)
                throw new ArgumentException("Size must be greater than zero.", nameof(size));

            _data = new List<T>(size);
            _capacity = size;
            for (int i = 0; i < size; i++)
            {
                _data.Add(defaultValue);
            }
        }

        public int Capacity => _capacity;

        public int Length => _data.Count;

        public void Add(T item)
        {
            if (_data.Count >= _capacity)
                throw new InvalidOperationException("FixedVec is full.");

            _data.Add(item);
        }
        public void Insert(int index, T item)
        {
            if (index < 0 || index > _data.Count)
                throw new ArgumentOutOfRangeException(nameof(index));

            if (_data.Count >= _capacity)
                throw new InvalidOperationException("FixedVec is full.");

            _data.Insert(index, item);
        }
        public void RemoveAt(int index)
        {
            if (index < 0 || index >= _data.Count)
                throw new ArgumentOutOfRangeException(nameof(index));

            _data.RemoveAt(index);
        }

        public void RemoveLast()
        {
            if (_data.Count == 0)
                throw new InvalidOperationException("FixedVec is empty.");

            _data.RemoveAt(_data.Count - 1);
        }
        public void Clear()
        {
            _data.Clear();
        }

        public T this[int index]
        {
            get
            {
                if (index < 0 || index >= _data.Count)
                    throw new IndexOutOfRangeException();
                return _data[index];
            }
            set
            {
                if (index < 0 || index >= _data.Count)
                    throw new IndexOutOfRangeException();
                _data[index] = value;
            }
        }

        public override string ToString()
        {
            return $"[{string.Join(", ", _data)}]";
        }
    }

    public class FixedSortedList<T> where T : IComparable<T>
    {
        private List<T> _list;
        private int _capacity;

        public FixedSortedList(int capacity)
        {
            if (capacity <= 0)
                throw new ArgumentException("Capacity must be greater than zero.", nameof(capacity));

            _list = new List<T>();
            _capacity = capacity;
        }

        public int Capacity => _capacity;
        public int Count => _list.Count;

        // 添加元素
        public void Add(T item)
        {
            if (_list.Count >= _capacity)
                throw new InvalidOperationException("List is full.");

            _list.Add(item);
            _list.Sort();  // 自动排序
        }

        // 获取元素
        public T Get(int index)
        {
            if (index < 0 || index >= _list.Count)
                throw new ArgumentOutOfRangeException(nameof(index));

            return _list[index];
        }

        // 清空列表
        public void Clear()
        {
            _list.Clear();
        }

        public override string ToString()
        {
            return $"[{string.Join(", ", _list)}]";
        }
    }

    public class FixedHashSet<T>
    {
        private HashSet<T> _set;
        private int _capacity;

        public FixedHashSet(int capacity)
        {
            if (capacity <= 0)
                throw new ArgumentException("Capacity must be greater than zero.", nameof(capacity));

            _set = new HashSet<T>();
            _capacity = capacity;
        }

        public int Capacity => _capacity;
        public int Count => _set.Count;

        // 添加元素
        public bool Add(T item)
        {
            if (_set.Count >= _capacity)
                throw new InvalidOperationException("HashSet is full.");

            return _set.Add(item);
        }

        // 判断元素是否存在
        public bool Contains(T item)
        {
            return _set.Contains(item);
        }

        // 清空集合
        public void Clear()
        {
            _set.Clear();
        }

        public override string ToString()
        {
            return $"[{string.Join(", ", _set)}]";
        }
    }

    public class FixedString
    {
        private string[] _array;
        private int _capacity;

        public FixedString(int capacity)
        {
            if (capacity <= 0)
                throw new ArgumentException("Capacity must be greater than zero.", nameof(capacity));

            _array = new string[capacity];
            _capacity = capacity;
        }

        public int Capacity => _capacity;
        public int Count { get; private set; } = 0;

        // 获取或设置指定索引的字符串
        public string this[int index]
        {
            get
            {
                if (index < 0 || index >= Count)
                    throw new ArgumentOutOfRangeException(nameof(index));

                return _array[index];
            }
            set
            {
                if (index < 0 || index >= Count)
                    throw new ArgumentOutOfRangeException(nameof(index));

                _array[index] = value;
            }
        }

        // 添加字符串
        public void Add(string item)
        {
            if (Count >= _capacity)
                throw new InvalidOperationException("Array is full.");

            _array[Count++] = item;
        }

        // 将数组转换为字节数组
        public byte[] ToBytes()
        {
            List<byte> byteList = new List<byte>();
            foreach (var str in _array.Take(Count))
            {
                byte[] stringBytes = System.Text.Encoding.UTF8.GetBytes(str);
                byteList.AddRange(stringBytes);
            }
            return byteList.ToArray();
        }

        // 清空数组
        public void Clear()
        {
            Array.Clear(_array, 0, _capacity);
            Count = 0;
        }

        public override string ToString()
        {
            return $"[{string.Join(", ", _array.Take(Count))}]";
        }
    }

    public class FixedInt
    {
        private int[] _array;
        private int _capacity;

        public FixedInt(int capacity)
        {
            if (capacity <= 0)
                throw new ArgumentException("Capacity must be greater than zero.", nameof(capacity));

            _array = new int[capacity];
            _capacity = capacity;
        }

        public int Capacity => _capacity;
        public int Count { get; private set; } = 0;

        // 获取或设置指定索引的整数
        public int this[int index]
        {
            get
            {
                if (index < 0 || index >= Count)
                    throw new ArgumentOutOfRangeException(nameof(index));

                return _array[index];
            }
            set
            {
                if (index < 0 || index >= Count)
                    throw new ArgumentOutOfRangeException(nameof(index));

                _array[index] = value;
            }
        }

        // 添加整数
        public void Add(int item)
        {
            if (Count >= _capacity)
                throw new InvalidOperationException("Array is full.");

            _array[Count++] = item;
        }

        // 清空数组
        public void Clear()
        {
            Array.Clear(_array, 0, _capacity);
            Count = 0;
        }

        // 将数组转换为字节数组
        public byte[] ToBytes()
        {
            List<byte> byteList = new List<byte>();
            foreach (var num in _array.Take(Count))
            {
                byte[] intBytes = BitConverter.GetBytes(num);
                byteList.AddRange(intBytes);
            }
            return byteList.ToArray();
        }

        public override string ToString()
        {
            return $"[{string.Join(", ", _array.Take(Count))}]";
        }
    }

    public class FixedUlong
    {
        private ulong[] _array;
        private int _capacity;

        public FixedUlong(int capacity)
        {
            if (capacity <= 0)
                throw new ArgumentException("Capacity must be greater than zero.", nameof(capacity));

            _array = new ulong[capacity];
            _capacity = capacity;
        }

        public int Capacity => _capacity;
        public int Count { get; private set; } = 0;

        // 获取或设置指定索引的 ulong 数值
        public ulong this[int index]
        {
            get
            {
                if (index < 0 || index >= Count)
                    throw new ArgumentOutOfRangeException(nameof(index));

                return _array[index];
            }
            set
            {
                if (index < 0 || index >= Count)
                    throw new ArgumentOutOfRangeException(nameof(index));

                _array[index] = value;
            }
        }

        // 添加 ulong 数值
        public void Add(ulong item)
        {
            if (Count >= _capacity)
                throw new InvalidOperationException("Array is full.");

            _array[Count++] = item;
        }

        // 清空数组
        public void Clear()
        {
            Array.Clear(_array, 0, _capacity);
            Count = 0;
        }

        // 将数组转换为字节数组
        public byte[] ToBytes()
        {
            List<byte> byteList = new List<byte>();
            foreach (var num in _array.Take(Count))
            {
                byte[] ulongBytes = BitConverter.GetBytes(num);
                byteList.AddRange(ulongBytes);
            }
            return byteList.ToArray();
        }

        public override string ToString()
        {
            return $"[{string.Join(", ", _array.Take(Count))}]";
        }
    }
}
