using Newtonsoft.Json;

namespace skyscraper5.Mpeg2.Descriptors
{
    public class UserDefinedDescriptor : TsDescriptor
    {
        public byte DescriptorTag { get; set; }
        public byte[] Data { get; set; }

        [JsonConstructor]
        public UserDefinedDescriptor(byte DescriptorTag, byte[] data)
        {
            this.DescriptorTag = DescriptorTag;
            this.Data = data;
            this.Valid = true;
        }

		public override byte GetDescriptorId()
		{
            return DescriptorTag;
		}

		public override bool DescriptorEquals(TsDescriptor thatDescriptor)
		{
            UserDefinedDescriptor that = thatDescriptor as UserDefinedDescriptor;
            if (that == null)
                return false;

            if (this.DescriptorTag != that.DescriptorTag)
                return false;

            if (this.Data.Length != that.Data.Length)
                return false;

            for (int i = 0; i < this.Data.Length; i++)
            {
                if (this.Data[i] != that.Data[i])
                    return false;
            }
            return true;
		}

		public override string ToString()
		{
            return String.Format("Unknown user defined descriptor: 0x{0:X2} ({1} bytes long)", DescriptorTag, Data.Length);
		}
	}
}
