using Newtonsoft.Json;
using skyscraper5.Dvb.DataBroadcasting.IntModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper5.DNS.Protocol.ResourceRecords.Records
{
	public class ServiceResourceRecord : BaseResourceRecord, SimpleDnsRecord
	{
		public ServiceResourceRecord(IResourceRecord record, byte[] message, int dataOffset) : base(record)
		{
			Head head = Marshalling.Struct.GetStruct<Head>(message, dataOffset, Head.SIZE);

			Priority = head.Priority;
			Weight = head.Weight;
			Port = head.Port;
			Target = Domain.FromArray(message, dataOffset + Head.SIZE);
		}

		[Marshalling.Endian(Marshalling.Endianness.Big)]
		[StructLayout(LayoutKind.Sequential, Pack = 4)]
		private struct Head
		{
			public const int SIZE = 6;

			private ushort priority;
			private ushort weight;
			private ushort port;

			public ushort Priority
			{
				get { return priority; }
				set { priority = value; }
			}

			public ushort Weight
			{
				get { return weight; }
				set { weight = value; }
			}

			public ushort Port
			{
				get { return port; }
				set { port = value; }
			}
		}

		public ushort Priority { get; }
		public ushort Weight { get; }
		public ushort Port { get; }
		public Domain Target { get; }

		public string GetDataAsString()
		{
			dynamic child = new
			{
				Priority = Priority,
				Weight = Weight,
				Port = Port,
				Target = Target.ToString()
			};
			string result = JsonConvert.SerializeObject(child);
			return result;
		}
	}
}
