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

namespace skyscraper5.DNS.Protocol.ResourceRecords.Records
{
	internal class NSec3Record : BaseResourceRecord
	{
		public NSec3Record(IResourceRecord record, byte[] message, int dataOffset)
			: base(record)
		{
			MemoryStream ms = new MemoryStream(Data);
			HashAlgorithm = (HashAlgorithmEnum)ms.ReadUInt8();
			NSec3Flags = ms.ReadUInt8();
			NSec3Iterations = ms.ReadUInt16BE();
			byte saltLength = ms.ReadUInt8();
			if (saltLength > 0)
				Salt = ms.ReadBytes(8);
			byte hashLength = ms.ReadUInt8();
			if (hashLength > 0)
				if (ms.GetAvailableBytes() >= hashLength)
				Hash = ms.ReadBytes(hashLength);
		}

		public HashAlgorithmEnum HashAlgorithm { get; set; }
		
		private byte NSec3Flags;

		public ushort NSec3Iterations { get; }

		public bool AdditionalInsecureDelegationsAllowed => (NSec3Flags & 0x01) != 0;

		public byte[] Hash { get; }
		public byte[] Salt { get; }

		public enum HashAlgorithmEnum : byte
		{
			SHA1 = 1
		}
	}
}
