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

namespace skyscraper5.UI.StreamAcquisition
{
	internal class StreamSourceIoStreamWrapper : StreamSource
	{
		private Stream wrapped;

		public StreamSourceIoStreamWrapper(Stream wrapped, string name)
		{
			this.wrapped = wrapped;
			this.StreamName = name;
		}

		public string StreamName { get; private set; }

		public override string GetSourceName()
		{
			if (string.IsNullOrEmpty(StreamName))
				return base.GetSourceName();

			return StreamName;
		}

		protected override void StartSourceEx()
		{
			byte[] buffer = new byte[188];
			int result = 0;
			do
			{
				result = wrapped.Read(buffer, 0, buffer.Length);
				if (result == buffer.Length)
				{
					context.IngestSinglePacket(buffer);
				}
				if (stopRequested)
				{
					base.State = StreamSourceState.StoppedByUser;
					return;
				}
			} while (result == 188);
			base.State = StreamSourceState.Finished;
		}
	}
}
