using Newtonsoft.Json;
using skyscraper5.Skyscraper.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper5.DNS.Protocol.ResourceRecords.Records
{
	internal class DnsKeyResourceRecord : BaseResourceRecord, SimpleDnsRecord
	{
		public DnsKeyResourceRecord(IResourceRecord record, byte[] message, int dataOffset) : base(record)
		{
			MemoryStream ms = new MemoryStream(Data);
			ushort flags = ms.ReadUInt16BE();
			KeySigningKey = (flags & 0x0001) != 0;
			KeyRevoked = (flags & 0x0080) != 0;
			ZoneKey = (flags & 0x0100) != 0;
			Protocol = ms.ReadUInt8();
			Algorithm = ms.ReadUInt8();
			this.PublicKey = ms.ReadBytes(ms.GetAvailableBytes());
		}

		public bool KeySigningKey { get; }
		public bool KeyRevoked { get; }
		public bool ZoneKey { get; }
		public byte Protocol { get; }
		public byte Algorithm { get; }
		public byte[] PublicKey { get; }

		public string GetDataAsString()
		{
			dynamic child = new
			{
				KeySigningKey = KeySigningKey,
				KeyRevoked = KeyRevoked,
				ZoneKey = ZoneKey,
				Protocol = Protocol,
				Algorithm = Algorithm,
				PublicKey = PublicKey
			};
			string result = JsonConvert.SerializeObject(child);
			return result;
		}
	}
}
