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

namespace skyscraper5.Dvb.Subtitling
{
    public class DisplayDefinitionSegment
    {
        public DisplayDefinitionSegment(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer, false);
            byte readUInt8 = ms.ReadUInt8();
            int ddsVersionNumber = readUInt8 & 0xf0 >> 4;
            VersionNumber = ddsVersionNumber;
            DisplayWindowFlag = (readUInt8 & 0x08) != 0;
            DisplayWidth = ms.ReadUInt16BE();
            DisplayHeight = ms.ReadUInt16BE();
            if (DisplayWindowFlag)
            {
                DisplayWindowHorizontalPositionMinimum = ms.ReadUInt16BE();
                DisplayWindowHorizontalPositionMaximum = ms.ReadUInt16BE();
                DisplayWindowVerticalPositionMinimum = ms.ReadUInt16BE();
                DisplayWindowVerticalPositionMaximum = ms.ReadUInt16BE();
            }
        }

        public DisplayDefinitionSegment()
        {
            DisplayWidth = 719;
            DisplayHeight = 575;
        }

        public int VersionNumber { get; set; }
        public bool DisplayWindowFlag { get; set; }
        public ushort DisplayWidth { get; set; }
        public ushort DisplayHeight { get; set; }
        public ushort? DisplayWindowHorizontalPositionMinimum { get; set; }
        public ushort? DisplayWindowHorizontalPositionMaximum { get; set; }
        public ushort? DisplayWindowVerticalPositionMinimum { get; set; }
        public ushort? DisplayWindowVerticalPositionMaximum { get; set; }
    }
}
