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

namespace skyscraper8.Skyscraper.Security.AccessControl
{
	internal class TandbergKey : Validatable
	{
		public ushort ChannelId { get; }
		public byte StreamId { get; }
		public byte[] Key { get; }

		public TandbergKey(ushort channelId, byte streamId, byte[] key)
		{
			ChannelId = channelId;
			StreamId = streamId;
			Key = key;

			if (key == null)
				Valid = false;
			else if (key.Length != 8)
				Valid = false;
			else
				Valid = true;
		}

		protected bool Equals(TandbergKey other)
		{
			ulong ourKey = BitConverter.ToUInt64(Key, 0);
			ulong theirKey = BitConverter.ToUInt64(other.Key, 0);
			return ChannelId == other.ChannelId && StreamId == other.StreamId && ourKey == theirKey;
		}

		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((TandbergKey)obj);
		}

		public override int GetHashCode()
		{
			ulong numericKey = BitConverter.ToUInt64(Key, 0);
			return HashCode.Combine(ChannelId, StreamId, numericKey);
		}
	}
}
