using skyscraper5.UI.StreamAcquisition;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper5.UI.StreamAcquirers
{
	[StreamAcquirer("NULL","Returns only empty frames")]
	internal class NullStreamAcquirer : AbstractStreamAcquirer<object>
	{
		public override object AskUserForHandle(IWin32Window window)
		{
			return new object();
		}

		public override object GetHandle(string arg)
		{
			return null;
		}

		private BlankTransportStream ts;
		public override StreamSource GetStream(object inputHandle)
		{
			if (ts == null)
				ts = new BlankTransportStream();

			return new StreamSourceIoStreamWrapper(ts, null);
		}

		class BlankTransportStream : Stream
		{
			public int _continuityCounter;
			private long _internalPosition;

			public override bool CanRead => true;

			public override bool CanSeek => false;

			public override bool CanWrite => false;

			public override long Length => throw new NotImplementedException();

			public override long Position
			{
				get => _internalPosition;
				set => throw new NotImplementedException();
			}

			public override void Flush()
			{
				throw new NotImplementedException();
			}

			private byte[] buffer;
			public override int Read(byte[] outBuffer, int offset, int count)
			{
				if (count != 188)
					throw new ArgumentOutOfRangeException(nameof(count));

				if (buffer == null)
					buffer = new byte[188];

				buffer[0] = (byte)'G';

				buffer[1] = 0x1f;
				buffer[2] = 0xff;

				buffer[3] = 0;
				buffer[3] <<= 2;
				buffer[3] += 1;
				buffer[3] <<= 2;
				buffer[3] += (byte)_continuityCounter;
				_continuityCounter++;
				if (_continuityCounter > 0xf)
					_continuityCounter = 0;

				Array.Copy(buffer, 0, outBuffer, offset, count);
				return count;
			}

			public override long Seek(long offset, SeekOrigin origin)
			{
				throw new NotImplementedException();
			}

			public override void SetLength(long value)
			{
				throw new NotImplementedException();
			}

			public override void Write(byte[] buffer, int offset, int count)
			{
				throw new NotImplementedException();
			}
		}
	}
}
