using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper5.Dvb.Subtitling.Model
{
    public class Page
    {
        public Page()
        {
            Regions = new RegionCompositionSegment[255];
            ObjectData = new List<ObjectDataSegment>();
        }

        public DisplayDefinitionSegment DisplayDefinition { get; set; }
        public RegionCompositionSegment[] Regions { get; set; }
        public PageCompositionSegment PageComposition { get; set; }
        public List<ObjectDataSegment> ObjectData { get; set; }

        private ClutFamily[] clutFamilies;
        public ClutFamily GetClutFamiliy(byte clutId)
        {
            if (clutFamilies == null)
                clutFamilies = new ClutFamily[255];

            if (clutFamilies[clutId] == null)
                clutFamilies[clutId] = new ClutFamily();

            return clutFamilies[clutId];
        }

        public ObjectDimensions GenerateObjectDimensions(ushort objectId)
        {
            for (int i = 0; i < Regions.Length; i++)
            {
                if (Regions[i] == null)
                    continue;

                RegionCompositionSegment rcsCandidate = Regions[i];
                foreach (RegionCompositionSegment.Object regionCandidate in rcsCandidate.Objects)
                {
                    if (regionCandidate.Id == objectId)
                    {
                        int width = rcsCandidate.RegionWidth - regionCandidate.HorizontalPosition;
                        int height = rcsCandidate.RegionHeight - regionCandidate.VerticalPosition;
                        return new ObjectDimensions(width, height);
                    }
                }
            }

            return null;
        }
    }
}
