using System.IO;
using System.Text;
using skyscraper5.Skyscraper.IO;

namespace skyscraper5.Mhp.Descriptors.InteractionTransportSelectors
{
    public class InteractionTransportSelector : TransportProtocolSelector
    {
        internal InteractionTransportSelector(MemoryStream ms) : base(3)
        {
            byte readUInt8 = ms.ReadUInt8();
            UrlBase = Encoding.ASCII.GetString(ms.ReadBytes(readUInt8));

            byte urlExtensionCount = ms.ReadUInt8();
            UrlExtensions = new string[urlExtensionCount];
            for (int i = 0; i < urlExtensionCount; i++)
            {
                byte urlExtensionLength = ms.ReadUInt8();
                UrlExtensions[i] = Encoding.ASCII.GetString(ms.ReadBytes(urlExtensionLength));
            }
        }

        public string UrlBase { get; private set; }

        public string[] UrlExtensions { get; private set; }

        protected bool Equals(InteractionTransportSelector other)
        {
            return UrlBase == other.UrlBase && UrlExtensions.Equals(other.UrlExtensions);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((InteractionTransportSelector)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return (UrlBase.GetHashCode() * 397) ^ UrlExtensions.GetHashCode();
            }
        }

        public override string ToString()
        {
            return UrlBase;
        }
    }
}
