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

namespace skyscraper5.Scte35.Descriptors
{
	[SkyscraperPlugin]
	[TsDescriptor(0x03, "SCTE35")]
    public class _0x03_TimeDescriptor : TsDescriptor
    {
        public _0x03_TimeDescriptor(byte[] buffer)
        {
            if (buffer.Length < 16)
            {
                Valid = false;
                return;
            }
            MemoryStream ms = new MemoryStream(buffer);
            Identifier = ms.ReadUInt32BE();

            byte[] taiSecondsBuffer = new byte[8];
            taiSecondsBuffer[7] = ms.ReadUInt8();
            taiSecondsBuffer[6] = ms.ReadUInt8();
            taiSecondsBuffer[5] = ms.ReadUInt8();
            taiSecondsBuffer[4] = ms.ReadUInt8();
            taiSecondsBuffer[3] = ms.ReadUInt8();
            taiSecondsBuffer[2] = ms.ReadUInt8();

            TaiSeconds = BitConverter.ToUInt64(taiSecondsBuffer);
            if (TaiSeconds > 281474976710656UL)
            {
                Valid = false;
                return;
            }
            TaiNs = ms.ReadUInt32BE();
            UtcOffset = ms.ReadUInt16BE();
            Valid = true;
        }

        public ushort UtcOffset { get; set; }

        public uint TaiNs { get; set; }

        public ulong TaiSeconds { get; set; }

        public uint Identifier { get; private set; }
    }
}
