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

namespace Lomont.Binary
    {

    /// <summary>
    /// Represent a stream of bytes to be read.
    /// </summary>
    public class ByteStream
        {
        // todo - clean and abstract out better
        // for example, allow construction with List<byte> or byte[], and don't copy - just reference correctly.
        // also allow non-0 start locations, etc., for sparse and sub-location representation

        /// <summary>
        /// The data image
        /// </summary>
        internal List<byte> Data { get; set; }

        /// <summary>
        /// The index into the image
        /// </summary>
        internal ulong DataIndex { get; set; }

        /// <summary>
        /// Get the next byte from the decoding, 
        /// and increment the index.
        /// </summary>
        /// <returns></returns>
        internal byte NextByte()
            {
            return Data[(int)DataIndex++];
            }

        /// <summary>
        /// Number of bytes left unread
        /// </summary>
        internal int BytesLeft
            {
            get { return Data.Count - (int)DataIndex; }
            }

        /// <summary>
        /// Get the byte with the given offset from the current position. 
        /// Does not increment internal index.
        /// </summary>
        /// <returns></returns>
        internal byte DataByte(int offset)
            {
            return Data[(int)DataIndex + offset];
            }

        /// <summary>
        /// Construct the byte stream to read, with an optional start location
        /// </summary>
        /// <param name="data">The data</param>
        /// <param name="startLocation">The start location into the data</param>
        public ByteStream(List<byte> data, ulong startLocation = 0)
            {
            Data = data;
            DataIndex = startLocation;
            }

        }
    }
