using System;

/*
    Copyright (c) 2010 flixmonk. (http://DVDSwarm.codeplex.com)
    All Rights Reserved.
        
    Redistribution and use in source and binary forms, with or without modification,
    are permitted provided that the following conditions are met:
        
    * Redistributions of source code must retain the above copyright notice, this list
      of conditions and the following disclaimer.
        
    * Redistributions in binary form must reproduce the above copyright notice, this
    list of conditions and the following disclaimer in the documentation and/or other
    materials provided with the distribution. 
        
    * Neither the name of The Flixmonk nor the names of its contributors may be used to
    endorse or promote products derived from this software without specific prior written
    permission.
        
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
    SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
    OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
    TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace DVDSwarm.Dvd
{
    /// <summary>
    /// Contains information about the DVD subpicture stream ids. Member of the ProgramChain class.
    /// </summary>
    sealed public class SubpictureStreamRef
    {
        #region Fields

        private Int32 _fullscreenTrackId = 0;
        private Int32 _index = 0;
        private String _idString = string.Empty;
        private Boolean _isPresent = true;
        private Int32 _letterboxTrackId = 0;
        private Int32 _panScanTrackId = 0;
        private Int32 _widescreenTrackId = 0;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the fullscreen subpicture stream track number. Values start at 0x20.
        /// </summary>
        public Int32 FullscreenTrackId
        {
            get
            {
                return _fullscreenTrackId;
            }
        }
        /// <summary>
        /// Gets the subpicture stream index associated with the TitleSet.Subpictures property.
        /// </summary>
        public Int32 Index
        {
            get
            {
                return _index;
            }
        }
        /// <summary>
        /// Gets whether the subpicture is present.
        /// </summary>
        public Boolean IsPresent
        {
            get
            {
                return _isPresent;
            }
        }
        /// <summary>
        /// Gets the letterboxed subpicture stream track number. Values start at 0x20.
        /// </summary>
        public Int32 LetterboxTrackId
        {
            get
            {
                return _letterboxTrackId;
            }
        }
        /// <summary>
        /// Gets the Pan &amp; Scan subpicture stream track number. Values start at 0x20.
        /// </summary>
        public Int32 PanScanTrackId
        {
            get
            {
                return _panScanTrackId;
            }
        }
        /// <summary>
        /// Gets the widescreen subpicture stream track number. Values start at 0x20.
        /// </summary>
        public Int32 WidescreenTrackId
        {
            get
            {
                return _widescreenTrackId;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Creates a new instance of the SubpictureStreamRef class.
        /// </summary>
        /// <param name="value">The index for the subpicture in the range 0-31.</param>
        /// <param name="bytes">A byte array containing bytes to parse.</param>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown when value is less than zero or greater than 31.</exception>
        /// <exception cref="System.ArgumentNullException">Thrown when bytes is null.</exception>
        /// <exception cref="System.ArgumentException">Thrown when bytes length is not four bytes.</exception>
        public SubpictureStreamRef(Int32 value, byte[] bytes)
        {
            if (value < 0 || value > 31)
            {
                throw new ArgumentOutOfRangeException("value is out of valid range [0-31].");
            }
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes");
            }
            if (bytes.Length != 4)
            {
                throw new ArgumentException("bytes length must be four bytes.");
            }

            _fullscreenTrackId = (bytes[0] & 0x1f) + 0x20;
            _index = value;
            _isPresent = (((bytes[0] >> 7) & 1) == 1);
            _letterboxTrackId = (bytes[2] & 0x1f) + 0x20;
            _panScanTrackId = (bytes[3] & 0x1f) + 0x20;
            _widescreenTrackId = (bytes[1] & 0x1f) + 0x20;
        }

        #endregion
    }
}
